Dynamic Struct Array allocation within a function
I am trying to allocate an array of structs within a function.
My struct is as follows:
typedef struct{
uint16_t taskNumber;
uint16_t taskType;
double lat;
double lon;
double speed;
uint8_t successCriteria;
uint16_t successValue;
uint8_t nextPoint;
}missionPoint;
In my code I declare a missionPoint-pointer which I then pass into the function that will dynamically allocate it after parsing a file and figuring out how big it needs to be. Currently this is how my code looks:
missionPoint* mission; //declaring the pointer
parseMission(mission);
The parseMission function will then parse a specific file and find out how many missionPoints I need and will then allocate it in the following manner:
mission = (missionPoint*) malloc(n * sizeof(missionPoint));
where n is the parsed number of missionPoints I need.
The problem is that within the function I can see the proper values but not outside of it; once the function returns it's like nothing happened.
I would appreciate your help in making it so that the function modifies the original pointer and I can see the dat开发者_C百科a from outside the function.
You need to pass a reference to the pointer, i.e. a double pointer, because the address itself is going to be modified:
missionPoint *mission;
parseMission(&mission);
The argument of parseMission
should now be of type missionPoint **
instead of missionPoint *
:
*mission = (missionPoint*) malloc(n * sizeof(missionPoint));
This wouldn't be necessary if you only wanted to modify the memory mission
is pointing to, but it cannot be avoided since you are assigning a new value to the pointer itself.
Also note that casting the return value of malloc
is not necessary in C.
Pass a reference (i.e. pointer) to the mission
pointer rather than the value of the mission
pointer itself. So the declaration of your function would look like:
void parseMission(missionPoint** pointer_ref);
rather than:
void parseMission(missionPoint* pointer_val);
then pass the address of mission
(i.e., &mission
) as the argument value for parseMission
.
Think that a pointer in C is just a integer, storing some memory address.
Think that malloc
allocates some memory and returns you the address of this memory (a new pointer).
Understand that this snippet will print "2", regadless of what the called function does with the passed value.
int a = 2;
doSomething(a);
printf("%d\n",a);
Once you understand all this, you can predict what this code will produce, and why you need to pass the "reference to the pointer" (pointer to the pointer) instead of of the pointer itself (value of the pointer) to your allocator function.
void remalloc(char * p1) {
printf("pointer before remalloc (inside function): %X\n",(unsigned int)(p1));
free(p1);
p1 = (char*)malloc(200000);
printf("pointer after remalloc (inside function): %X\n",(unsigned int)(p1));
}
int main(){
char * p;
printf("pointer before malloc: %X\n",(unsigned int)(p));
p = (char*)malloc(10);
printf("pointer after malloc: %X\n",(unsigned int)(p));
remalloc(p);
printf("pointer after remalloc: %X\n",(unsigned int)(p));
}
精彩评论