Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C
Alternative
Tip/Trick

How to zero your memory?

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
2 Aug 2011CPOL 21.6K   15
char szTest[128] = { 0 };;)
C++
char szTest[128] = { 0 };

;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Oh yes, I will... :) Pin
Nuri Ismail9-Aug-11 3:58
Nuri Ismail9-Aug-11 3:58 
GeneralRe: Please repost testing results here. It must be interesting. Pin
Alexander Voronin9-Aug-11 3:56
Alexander Voronin9-Aug-11 3:56 
GeneralRe: 'How do you think could it be hit on performance?' Actually ... Pin
Nuri Ismail9-Aug-11 3:45
Nuri Ismail9-Aug-11 3:45 
GeneralRe: IC for now. Thanx for explanation. This behaviour also prese... Pin
Alexander Voronin9-Aug-11 3:20
Alexander Voronin9-Aug-11 3:20 
GeneralRe: Hi Alexander, It's part of C++03 Standard. For example le... Pin
Nuri Ismail9-Aug-11 3:03
Nuri Ismail9-Aug-11 3:03 
Hi Alexander,

It's part of C++03 Standard.

For example lets take a second link from your comment.
Have a look at 5.3.4 -15 (Page 92):
'If the new-initializer is of the form (), the item is value-initialized (8.5);'

The important parts of 8.5 for us are -4 and -5.
From 8.5 -5 (Page 166):
'To value-initialize an object of type T means:
...........
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized'

My intention was not to mislead and if my post is misleading please accept my apologies. I just shared a little-known part of the C++ Standard.
I also not use this feature because this kind of syntax is not supported by old compilers (VC 6 for example) and some of my projects must be compatible with them. But for new projects on new versions of C++ compilers I believe that using this syntax is not a problem. I've tested it with VC9 and VC10 and it just worked fine. I'll test it also with GCC and if i encounter a problem I'll let you know.

Sorry for the long post, I hope now it is clear that my intention is not to mislead or to defend the usability of this feature. I knew it was a part of the standard and I just wanted to share it in this appropriate context. Smile | :)

Best Regards,
Nuri
GeneralRe: Thank you very much Alok. I'm glad you like it! :) Pin
Nuri Ismail9-Aug-11 3:05
Nuri Ismail9-Aug-11 3:05 
GeneralRe: Yes, it is part of C++03. You can try it and it should work... Pin
Nuri Ismail9-Aug-11 3:08
Nuri Ismail9-Aug-11 3:08 
GeneralRe: Where did you get this? Standard (let's take this http://www... Pin
Alexander Voronin8-Aug-11 22:41
Alexander Voronin8-Aug-11 22:41 
GeneralRe: nice thing!... no votes for comment yet, otherwise my 5 for ... Pin
ThatsAlok3-Aug-11 6:33
ThatsAlok3-Aug-11 6:33 
GeneralRe: Really? ...never tried it... guess we learn something new ev... Pin
Albert Holguin3-Aug-11 4:18
professionalAlbert Holguin3-Aug-11 4:18 
Generalanyway ur tips is useful as well Pin
ThatsAlok3-Aug-11 6:32
ThatsAlok3-Aug-11 6:32 
Generalmight be i am talking about zeroing memory during data excha... Pin
ThatsAlok3-Aug-11 6:27
ThatsAlok3-Aug-11 6:27 
GeneralYes, of course. ;) Another one ? The fastet way I know: v... Pin
trotwa2-Aug-11 21:19
trotwa2-Aug-11 21:19 
GeneralReason for my vote of 4 This works too, but only for static ... Pin
Albert Holguin2-Aug-11 11:44
professionalAlbert Holguin2-Aug-11 11:44 
GeneralRe: For default initialization of dynamic sized arrays C++ provi... Pin
Nuri Ismail2-Aug-11 21:52
Nuri Ismail2-Aug-11 21:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.