Don't you have to recast to non const inorder to free.
From what I remember in C, you only free what you allocate with calloc/malloc. Did you allocate memory? If not then just set the pointer to this struct to zero and be done with it. If you made these pointers global then this will not work because you are using singletons, and is considered bad practice. Remember globals are not on the stack they are defined, so make certain no to make const pointers global unless you intend to use it through the life of the entire program. If you did allocate memory then recast to non const and then free.
Personally I would rethink the logic and come up with a system of internal checks and ballances that prevent modification of pointers without using const.
freeing pointer to const
[quote name='ultramailman' timestamp='1353143134' post='5001719']
I am not sure on the "not writable" and "read only" part. I think const is just used to tell the compiler "Hey compiler, I don't want this value modified by my code. If you see my code modify this value, treat that as an error!". So it's more like a compile-time error-checking tool to allow the programmer catch his/her own logic mistakes. Well, that's only my guess, I haven't bothered to verify it yet.
I'm not quite sure about the history, but don't forget that C was invented to write OS and system software.
So it's possible that a pointer in C is pointing to a block of memory in the ROM, which is really read only.
Of course you are correct that "const" is used for compile time check, what I want to say is that a const pointer may really point to something that's not writable.
[/quote]
I see. When you say ROM, what is the degree of the ROM-ness? Is it some region of ROM on the RAM or is it some "harder" kind of ROMs like the EEPROM in which the BIOS reside in? My question probably sounds stupid, but I am not very knowledgeable on this ;o
Don't you have to recast to non const inorder to free.
From what I remember in C, you only free what you allocate with calloc/malloc. Did you allocate memory? If not then just set the pointer to this struct to zero and be done with it. If you made these pointers global then this will not work because you are using singletons, and is considered bad practice. Remember globals are not on the stack they are defined, so make certain no to make const pointers global unless you intend to use it through the life of the entire program. If you did allocate memory then recast to non const and then free.
Personally I would rethink the logic and come up with a system of internal checks and ballances that prevent modification of pointers without using const.
Oh yes, they are all malloced. I make sure to do a copy of whatever is passed in, so key and val will point to malloced memory. That way I won't accidently free them while the table still has a pointer to them. And no globals are involved here, although I guess anything that is malloced is global, in a way.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement