Hi,
I initialized the values of the folowing arrays:
char userInput[MAXSIZE];
char* cmdv[20];
during the execution of my program and I want to reset them so that they can be reused as new.
What's the best way to do this?
Thanks,
Canadian
Hi,
I initialized the values of the folowing arrays:
char userInput[MAXSIZE];
char* cmdv[20];
during the execution of my program and I want to reset them so that they can be reused as new.
What's the best way to do this?
Thanks,
Canadian
Depends on what you mean by reset.
You can use a loop to set all elements to a new value, or you can use ZeroMemory or memset to fill the arrays with a specific value (or 0 in case of ZeroMemory).
> char * cmdv[20];
I'd use a loop setting each pointer to NULL, that way if the pointers pointed to any DMA'd memory you could free it.
> char userInput[MAXSIZE];
memset(userInput, 0, sizeof(userInput));
Last edited by zacs7; 11-12-2007 at 02:17 AM.
For strings, yes, but if you copy other data, then there's no 0 or NULL when the data ends which might be a good thing. It's always safer to zero the whole buffer.
I have my own stupid reason for these clearing buffer..
when we allocate memory to a variable.. and dont initialize it to anything it will be garbage value..
This is something like the same situation, so you treat it as garbage value even if it is exactly not a garbage.. (garbage as it is not useful now, not garbage as its you who generated in your code..)
anyway.. the method for bookkeeping the array sizes and limits is only by keeping a separate variable, and strings are an exception.. if you want to reset your array.. just tell the bookkeeper that it is reset, and do nothing..
in fact it really shouldnt matter if you put 0, or NULL or anything.. anyway you wont visit that part of the array by keeping track of what is of our interest by some other variable or a field in the array itself.. (linked lists are an exception, as their condition is to have a null pointer in the end of the list)
C's Motto: who cares what it means? I just compile it!!