Click to See Complete Forum and Search --> : C variable protection
Riley
06-10-2002, 08:38 AM
Is there someone of protecting your variables from being appended after you put the valu you want in them? In a CGI script I've written somehow variables are overwritten or appended even though I make sure to add the NULL character after the end of the string...
debiandude
06-10-2002, 09:53 AM
const char blarg
Strogian
06-10-2002, 06:18 PM
Yeah that'll do it. But if your variables are being modified "for some reason," I think a better solution would probably be to fix whatever is modifying them.
l01yuk
06-11-2002, 02:58 AM
Um, I haven't really used C for CGI but are you doing:
arr[LAST_ELEMENT]=NULL;
because if so this just adds something that is interpreted as part of the string, not the end of it.
You should be doing:
arr[LAST_ELEMENT]='\0';
to show the end of the string.
What I suspect is not that something is appending the string but that the string is never terminated properly so functions don't know where the end of the string is and keep reading chars until the next '\0' which could be anywhere.
Strogian
06-11-2002, 09:31 AM
Aren't NULL and '\0' (usually) the same? NULL is defined as 0, and '\0' is just the same thing as 0.
Energon
06-11-2002, 09:36 AM
I don't think that's necessarily defined as always true. I've seen it recommended that you use 0 instead of NULL just in case NULL is defined as a special value and not 0.
Riley
06-11-2002, 09:57 AM
Okay, I was using '\0' anyway and I printed out the string right after doing that and it is fine and stops at the right spot. But at the end of the program it has somehow changed...
Energon
06-11-2002, 12:07 PM
If you can catch the process, try attaching ddd to it and watching the variable, or make a function that you can call every so often to print the value and narrow down where the change the happening.