Can't modify a string when running program

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Can't modify a string when running program

ソリューションへジャンプ
509件の閲覧回数
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 解決策
432件の閲覧回数
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 返信
433件の閲覧回数
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