tags:

views:

9059

answers:

10

Hello,

I thought by setting the first element to a null would clear the entire contents of a char array.

char my_custom_data[40] = "Hello!";
my_custom_data[0] = '\0';

However, this only sets the first element to null.

or

my_custom_data[0] = 0;

rather than use memset, I thought the 2 examples above should clear all the data.

Many thanks for any advice,

+2  A: 

You should use memset. Setting just the first element won't work, you need to set all elements - if not, how could you set only the first element to 0?

Michael
memset shouldn't be used over readability: http://stackoverflow.com/questions/453432/difference-in-initalizing-and-zeroing-an-array-in-c-c/455137#455137
Johann Gerell
+22  A: 

It depends on how you want to view the array. If you are viewing the array as a series of char's then the only way to clear out the data is to touch every entry. memset is probably the most effective way to achieve this.

On the other hand if you are choosing to view this as a C/C++ null terminated string, setting the first byte to 0 will effectively clear the string.

JaredPar
lol I thoght the same thing when I saw that +1
hhafez
They key word in the answer is 'effectively'. Only the first element is begin set to 0 and the rest still have undefined values but if you are treating the array as a null terminated string and the first element is null, then the string is considered empty.
Arnold Spence
indeed, this is the answer folks.
Johannes Schaub - litb
@caparcode, exactly. That's why it's very important to understand how the array is being used.
JaredPar
Yes, that is what I should have said in my first post. The char is a terminated string. so either these will do that trick. char[0] = '\0'; or char[0] = 0. I am not sure but I heard that using '\0' is better for using null terminated strings.
robUK
@robUK, yes you are correct. Technically '\0' is equal to 0 (in ascii) but you should use '\0' because it makes your intention clear
Mark Testa
About "most effective" - please see http://stackoverflow.com/questions/453432/difference-in-initalizing-and-zeroing-an-array-in-c-c/455137#455137
Johann Gerell
you do understand that in ANSI C (atleast) the array[] = {0}; construct only works when you define an array and when you try to do it later on you get a compile error!
hhafez
+3  A: 

Nope. All you are doing is setting the first value to '\0' or 0.

If you are working with null terminated strings, then in the first example, you'll get behavior that mimics what you expect, however the memory is still set.

If you want to clear the memory without using memset, use a for loop.

Alan
I say no on the for loop. Try not to write your own "improved" (and usually not) library functions. In fact, memset and memcpy are rather special are often inlined into custom machine code for the CPU based on what is known about data alignment and length.
Zan Lynx
@Zan the OP doesn't want to use memset (perhaps he's embedded and doesn't have it available). But yes, memset is usually highly optimal, and likely faster than a for loop.
Adam Hawes
True, however he didn't want to use memset, so I suggested a for loop.
Alan
+1  A: 

Writing a null character to the first character does just that. If you treat it as a string, code obeying the null termination character will treat it as a null string, but that is not the same as clearing the data. If you want to actually clear the data you'll need to use memset.

tvanfosson
+5  A: 

An array in C is just a memory location, so indeed, your my_custom_data[0] = '\0'; assignment simply sets the first element to zero and leaves the other elements intact.

If you want to clear all the elements of the array, you'll have to visit each element. That is what memset is for:

memset(&arr[0], 0, sizeof(arr));

This is generally the fastest way to take care of this. If you can use C++, consider std::fill instead:

char *begin = &arr;
char *end = begin + sizeof(arr);
std::fill(begin, end, 0);
John Feminella
I believe the second version should be:std::fill( arr, arr+ sizeof(arr)/sizeof(arr[0]), 0 );
David Rodríguez - dribeas
Clarification: don't use sizeof with fill because you'll get in trouble later with arrays of int, long, double or what have you.
Zan Lynx
I prefer: std::fill(
Zan Lynx
Zan Lynx, that's undefined behavior. you cannot do or if you have the length somewhere std::fill(arr, arr + arr_len, 0); assuming an array of char
Johannes Schaub - litb
It is valid only in C . although the question clearly targetted C (another guy added a C++ tag, i've no clue why), the std::fill shows C++ affinity :)
Johannes Schaub - litb
About "fastest" - http://stackoverflow.com/questions/453432/difference-in-initalizing-and-zeroing-an-array-in-c-c/455137#455137
Johann Gerell
@John, you forgot a "" instead (probably you just forgot to remove the "[0]" ?). wanted to add/remove as least characters as possible :)
Johannes Schaub - litb
@litb; thanks, fixed!
John Feminella
+1  A: 

Why not use memset()? That's how to do it.

Setting the first element leaves the rest of the memory untouched, but str functions will treat the data as empty.

John Fricker
Don't use memset over readability: http://stackoverflow.com/questions/453432/difference-in-initalizing-and-zeroing-an-array-in-c-c/455137#455137
Johann Gerell
+3  A: 

Why would you think setting a single element would clear the entire array? In C, especially, little ever happens without the programmer explicitly programming it. If you set the first element to zero (or any value), then you have done exactly that, and nothing more.

When initializing you can set an array to zero:

char mcd[40] = {0}; /* sets the whole array */

Otherwise, I don't know any technique other than memset, or something similar.

abelenky
A: 

I thought by setting the first element to a null would clear the entire contents of a char array.

That is not correct as you discovered

However, this only sets the first element to null.

Exactly!

You need to use memset to clear all the data, it is not sufficient to set one of the entries to null.

However, if setting an element of the array to null means something special (for example when using a null terminating string in) it might be sufficient to set the first element to null. That way any user of the array will understand that it is empty even though the array still includes the old chars in memory

hhafez
Don't use "memset" over readability: http://stackoverflow.com/questions/453432/difference-in-initalizing-and-zeroing-an-array-in-c-c/455137#455137
Johann Gerell
A: 

memset(my_custom_data, 0, sizeof(my_custom_data));

or

memset(my_custom_data, 0, strlen(my_custom_data));

A: 

set the first element to NULL. printing the char array will give you nothing back.