Can't modify a string when running program

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Can't modify a string when running program

跳至解决方案
605 次查看
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

标签 (1)
0 项奖励
回复
1 解答
528 次查看
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

在原帖中查看解决方案

1 回复
529 次查看
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