N
NvrBst
static char* DataThatCanChange[4] = {
"Default S1",
"Default S2",
"Default S3",
"Default S4:"
};
I have the above. Sometimes the Default value is changed during the
program, and I can change it doing the following:
//myCharStar is Equal "New String"
DataThatCanChange[2] = (char*)malloc((strlen(myCharStar) + 1) * sizeof
(char));
My question is how do I clean up the old value? When I do "free
(DataThatCanChange[2]);" (before chaning) it throws an exception:
*** glibc detected *** /home/administrator/eclipse/workspace/test/
Debug/test: double free or corruption (out): 0x080497a8 ***
I'm kind of assuming that the compiler is doing something with the
strings (pooling them or something), and I'm not suppose to free them,
or compiler is freeing it automatically after I change the value?
However, I still need to clean up the 2nd value if I changed it a 3rd
time?
I'm using Ubuntu with GCC 4.3.2, and latest Eclipse. Sorry if there
is something very obvious I'm missing. Thanks in advance.
Note: I can think of two other ways to work around this.
A) Initialize the "DataThatCanChange" manually at the start of the
program with malloc.
B) Set a size limit for the Values, IE "char[32]", and just don't free
it.
I'm wondering if there is some kind of C99 Syntax that will let me
initialize the array statically (like the example), but also let me
change the char* when I need to update the value; and what is the
right way to clean-up in this case.
"Default S1",
"Default S2",
"Default S3",
"Default S4:"
};
I have the above. Sometimes the Default value is changed during the
program, and I can change it doing the following:
//myCharStar is Equal "New String"
DataThatCanChange[2] = (char*)malloc((strlen(myCharStar) + 1) * sizeof
(char));
My question is how do I clean up the old value? When I do "free
(DataThatCanChange[2]);" (before chaning) it throws an exception:
*** glibc detected *** /home/administrator/eclipse/workspace/test/
Debug/test: double free or corruption (out): 0x080497a8 ***
I'm kind of assuming that the compiler is doing something with the
strings (pooling them or something), and I'm not suppose to free them,
or compiler is freeing it automatically after I change the value?
However, I still need to clean up the 2nd value if I changed it a 3rd
time?
I'm using Ubuntu with GCC 4.3.2, and latest Eclipse. Sorry if there
is something very obvious I'm missing. Thanks in advance.
Note: I can think of two other ways to work around this.
A) Initialize the "DataThatCanChange" manually at the start of the
program with malloc.
B) Set a size limit for the Values, IE "char[32]", and just don't free
it.
I'm wondering if there is some kind of C99 Syntax that will let me
initialize the array statically (like the example), but also let me
change the char* when I need to update the value; and what is the
right way to clean-up in this case.