|
Re: C coders, what method do you use to clear a character array?
11/19/08 11:59 AM
|
|
|
> > Do you blow away the address, nt caring about the data? > > > > Do you blow away the data in memory? > > > > Do you allocate and de allocate? > > > > my preference: > > > > for(int i = 0; i > sizeof(CharArray); i++){ CharArray = '/0';} > > > > > > Posting in the 'bin as I'm expecting this thread to stray a bit... > > > try memset() > > http://www.codecogs.com/reference/c/string.h/memset.php
the equivalent would be:
memset(CharArray, 0, sizeof(CharArray));
You can't deallocate/allocate arrays & sizeof only does what you want for arrays & not pointers. If CharArray was a pointer then sizeof(CharArray) would return the size of the pointer ( sizeof(*CharArray) would return the size of what the pointer points at ). To use pointers you might want to use calloc ( malloc doesn't guarantee that the memory will be cleared at all ) & then if you later wanted to clear it you need to keep the size that you allocated and pass that into memset instead.
|
|