Can't modify a string when running program

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can't modify a string when running program

Jump to solution
427 Views
juanignaciotroi
Contributor III

Hi. Using the FRDM KE02 for developing a project (using CWIDE + processor expert), the following problem came up:

 

The program consists of the following: when pressing a button, the last pressed button, is saved in the variable (unsigned char last_pressed_button). That works correctly. What I want to do is to save this pressed buttons in a string (char * pressed_buttons). The problem is that when I want save the pressed buton (using: pressed_buttons[i] = last_pressed_button) the MCU misteriously stops running.

 

What am I doing something wrong?

 

Thank you

Labels (1)
0 Kudos
1 Solution
350 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Juan Troisi:

From your description it seems you are not initializing that pointer (char* pressed_buttons) and from CodeWarrior startup it is likely that it is pointing to address 0, which is Flash memory and cannot be written to with a simple assignment, which results in a fault. Even initializing the pointer would not be such a good idea, as you are relying on it pointing to available space, but you could end up overwriting other variables, stack, etc.

You are better allocating memory for a defined size array, like this:

char pressed_buttons[SIZE]; // Size depends on how long you require the string to be.

or reserving some space in the linker file only for the string, but this may be an overkill.

Regards!

Jorge Gonzalez

View solution in original post

1 Reply
351 Views
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Juan Troisi:

From your description it seems you are not initializing that pointer (char* pressed_buttons) and from CodeWarrior startup it is likely that it is pointing to address 0, which is Flash memory and cannot be written to with a simple assignment, which results in a fault. Even initializing the pointer would not be such a good idea, as you are relying on it pointing to available space, but you could end up overwriting other variables, stack, etc.

You are better allocating memory for a defined size array, like this:

char pressed_buttons[SIZE]; // Size depends on how long you require the string to be.

or reserving some space in the linker file only for the string, but this may be an overkill.

Regards!

Jorge Gonzalez