Hi,
I'm having problems using pointers. Even the simplest thing:
Uint16 *x;
*x =5;
does not work. In the watch window for variable *x it displays:
memory map prevented read of target memory at 0xEA689946@Data. What do I need to do?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I'm having problems using pointers. Even the simplest thing:
Uint16 *x;
*x =5;
does not work. In the watch window for variable *x it displays:
memory map prevented read of target memory at 0xEA689946@Data. What do I need to do?
Hi,
You need to give the pointer X a value before you fill the memory location pointed by it.
Right now you want to fill the memory location pointed by X, but you did not say where should be that location...
If you do not give X a value (initialize the pointer), it can point to whatever was in the X variable and that can fall in a non- mapped area...
Hi,
As far as assigning next available address is concerned, you can increment tje address where variable 'x' is stored by 1 and store the new variable in that address.
To make sure that the new variable say int y does not get written to the location where X is defined, you can use the #pragma directive to explicitly define the memory locations where you want your variables to be stored.
the #pragma directive overrides compiler based options and helps you assign variables to specific memory locations as per your requirement. You can then define this specific memory location in your .cmd file i.e. linker file (assuming you are using Code Composer Studio since you mentioned using watch window in your earlier post)
Regards,
Sid
I still don't get why I have to worry about this. When I define int x; I never have to think where code composer defines the variable x and whether it is in the memory map or not because it does its job correctly everytime. How come when I define a pointer it can't do the same?
Ates,
int x; and int*x; are two different things.
They are similar in allocation. When you have int x; or int *x; in your code, the linker will allocate a space (of size int for the first and of size memory address for the second) in a valid place in memory (according to the linker map file, obviously). You can assign a value to it by simply using the equal (=) operator, since there is memory space reserved to it. However the similarities end here.
When you assign a value to the variable x, you are writing a numeric value to the integer but an address to the pointer. Therefore when using the dereference (*) operator *x = 5; you are actually telling the compiler to store the value (5) in the memory address previously stored in the variable x. If x contains a random value, you are actually trying to write the value to a random point in your memory space, either valid or invalid.
That's the reason why you need to initialize the pointer variable before assigning a value to it.
There are several other nuances and details about this extensive subject. Please check around on the internet about pointers - a particularly useful page is located here:
http://www.cplusplus.com/doc/tutorial/pointers/
Hope this helps,
Rafael
For the record, and for the future benefit of those searching through the forum, I also encountered this error when I improperly declared some variables...
I wanted to create a struct and declare it (using keyword 'extern') in a header file, then define it in the corresponding source file.
In config.h:
struct my_struct {
int some;
unsigned long variables;
char in;
unsigned int here;
};
extern struct my_struct array_of_my_structs[];
In conifg.c:
struct my_struct array_of_my_structs[3];
The problem was that I dimensioned the array in the source file, but did not dimension it at declaration time. Once I matched the definition to the declaration, the debugger was able to access the variables no problem.
Best Regards,
David R.