L
lancer6238
Hi,
I keep getting segmentation fault when I try to free a particular
array variable.
"state" is defined as a 4-byte data type, and "NextState" is a global
variable. The value of state_size is 116994. Here is a portion of the
relevant code:
state **NextState = NULL; // in the main program file
/* In another file #include'd in the main program file */
static void Read(int *state_size)
{
...
NextState = (state **)malloc(sizeof(state*)*(*state_size));
for (i = 0 ; i < *state_size ; i++)
NextState = (state *)malloc(sizeof(state)*(258));
...
}
void FreeV(int state_size)
{
...
for (i = 0 ; i < state_size ; i++)
{
printf("%d %p %d\n", i, NextState, NextState); // For
checking purposes
free(NextState);
}
free(NextState);
}
Everytime I get to NextState[63], I get a segmentation fault. I've
check that the pointer to NextState[63] in FreeV() points to the same
address of NextState[63] created in Read(), and the value in NextState
is valid.
So why do I still get the segmentation fault at that particular point?
Thank you.
I keep getting segmentation fault when I try to free a particular
array variable.
"state" is defined as a 4-byte data type, and "NextState" is a global
variable. The value of state_size is 116994. Here is a portion of the
relevant code:
state **NextState = NULL; // in the main program file
/* In another file #include'd in the main program file */
static void Read(int *state_size)
{
...
NextState = (state **)malloc(sizeof(state*)*(*state_size));
for (i = 0 ; i < *state_size ; i++)
NextState = (state *)malloc(sizeof(state)*(258));
...
}
void FreeV(int state_size)
{
...
for (i = 0 ; i < state_size ; i++)
{
printf("%d %p %d\n", i, NextState, NextState); // For
checking purposes
free(NextState);
}
free(NextState);
}
Everytime I get to NextState[63], I get a segmentation fault. I've
check that the pointer to NextState[63] in FreeV() points to the same
address of NextState[63] created in Read(), and the value in NextState
is valid.
So why do I still get the segmentation fault at that particular point?
Thank you.