Link to home
Start Free TrialLog in
Avatar of priyanka999kamlekar
priyanka999kamlekar

asked on

Memset 0 or 0x00 ?

I have a query on Memset, which one we need to use 0x00 or 0

Ex:
char myname[512]; //created a character array and wants to memset it
//Is there any difference in the below lines:
memset (&myname, 0, sizeof(myname));
memset (&myname, 0x00, sizeof(myname));  //some are saying we should use only this. 0x00 will be replaces the string with NULL.

I feel both are same. 0x00 will be converted into int as memset takes 2nd argument as int.

I have seen the visual studio also used both. so where we need to use these two types of memsets? 0x00 in which situation and 0 in which situation.?

 C:\program files\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlcoll.h(2948):      
memset( ppBins, 0, nBins*sizeof( CNode* ) );
  C:\program files\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlcoll.h(3554):      
memset(m_pNil, 0x00, sizeof(CNode));

Please clarify in detail.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi priyanka999kamlekar,

They are the same.  Zero is zero and C provides a number of ways to represent zero.  

Feel free to use whichever form makes the most sense to you, just try and be consistent.  Some people find that using '\0' to show a character of 0 is self-documenting.  Others find that that always using 0 regardless of the type is more meaningful.

Regardless, C will cast it to the correct length.

Kent
As said in previous comment, you can use either  0x00 or 0.
 0x00 is hexadecimal representation of 0.but value wise these are same.
To make code readable we really choose what to use.

Moreover. you posted it in c++ tag, so I can tell one more win32 API, which performs the same thing
ZeroMemory()
memset() is C library function.
Hi priyanka999kamlekar,

just as addition (and allthough I guess you already know about): for using such an array as string with common C/C++ string functions/classes it's not needed to set every char of the string to 0 to indicate an empty string, it's enough to set the first char to 0 with something like i.e.:

  char myname[512];
  myname[0] = '\0';

Beside this you can initialize the char array with all chars in it set to 0 automatically where you declare it i.e. like this:

  char myname[512] = {};

ZOPPO
In C++, we can't write initialize arr[523]={} in constructor. and also in header also it is not possible.
// char myname[512] = {};
This only works for local variables.
So if we want to initialize data member arr[] in constructor, we have to use memset() or zeroMemory(). and also only arr[0]='\0' is not enough:)

More over, if you create object as follows, then automatically all elements of int array will be initialized to ZERO.

MyClassr* p = new MyClass();

But if use cppchecker or something similar to find out the warning,style,performance errors, then it'll complain if all data members are not initialized in constructor.
So ultimately we have use memset() or ZeroMemory() ...
Avatar of priyanka999kamlekar
priyanka999kamlekar

ASKER

I am confused.
I am coding it in C only.
I have a structure which contain all type of data types even strucures too.
typedef struct tStruct
{
    char name[40];
    int id;
    double sal;
    long x,
    short y;

    Struct my_model
   {
        char name[40];
        int id;
       double size;
       long a;
       short b;
     }model;

}TSTRUCT;

Now can i do it this way.
TSTRUCT tempData = {};

or do i need to use memset.

TSTRUCT tempData;
memset(&tempData, 0, sizeof(tempData));

Can you tell me when to use memset in this case and if i have pointer variables how this memset will behave.?
Please clarify.
You should use memset, or provide enough initializers to set all of the elements to zero.

typedef struct
{
  int i1;
  int i2;
} Example;

Example X = {0};
Example Y = {0, 0};

In the short example above, X.i1 gets initialized to zero, X.i2 does not get initialized.  Y.i1 and Y.i2 get initialized.

Note that X.i2 will be zero if the variable is create in the globals block (not within a function) as all items in the globals block default to zero.

A common practice is to initialize structure objects with memset.  It's fast and easy, and if programming changes ever result in the structure being changed, initialization is still automatic.

  memset (X, 0, sizeof (Example));


Good Luck,
Kent
Using memset() you are telling to fill ZEROs(In this case) from starting address to a particular range. So it's a good discision.

>>>> if i have pointer variables how this memset will behave.?
Pointers will become NULL.which is required during initialization.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Zoppo,

My "memory" and habits go all the way back to before the C90 standards.  In modern compilers, (C99) initialization is a lot friendlier

From the Standard:

[6.7.8.21] If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

So you're correct.  Static storage defaults to 0, so what you describe and test is exactly what the standard says it should be.

Thanks for keeping me honest (and smarter)!
Kent
Hi Kdo,

thanks to you too for posting the appropriate standard here. To be honest searching/reading/finding specs is one of my weaknesses :-/

ZOPPO
Thanks...  I certainly didn't remember that.  :)  Google search is our friend.  Sometimes....

I really should reread the entire standard.  An awful lot is different from the C90 standard and it's a completely different world from the pre-C90 days.  But then, I don't know how to read that much dry material and stay awake!  It's a tough read!!!

Kent
@ID: 39899890:

TSTRUCT t1 = { 0 };
This initialization works during declaring if we are initializing members. But think about constructor. Inside class declaration we can't initialize a member.we have to do it in class defination and more appropriately in constructor. So inside constructor we can't write t1={};
It doesn't make any scence.
Utiately we have to use memset to do the initialization in a single shot. Else each member have to be initiaized separetely which is some what more unnecessary work.

TSTRUCT t1 = { 0 }; also can be written as TSTRUCT t1 = {  };