Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,611 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 929 people online right now. Registration is fast and FREE... Join Now!




Zeroing memory

 
Reply to this topicStart new topic

Zeroing memory

Tom9729
post 16 Jul, 2008 - 05:19 PM
Post #1


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,447



Thanked 10 times

Dream Kudos: 325
My Contributions


If I have a pointer to a structure, can I use memset(..) to zero all of the variables in the structure?

It works in my code, I just want to know if it's a bad practice or something.

Ex.
CODE

typedef struct
{
        char* string;
        int integer;
        float real;
} example_t;

example_t* example;

example = malloc(sizeof(example_t));
memset(example, 0, sizeof(example_t));


Thanks! icon_up.gif
User is online!Profile CardPM

Go to the top of the page

Cerolobo
post 16 Jul, 2008 - 05:23 PM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 31 times
My Contributions


No, that is usually a very good practice.

Using memset() results in less code, plus it will automatically handle all newer variables. Well, assuming you want them to be set to 0.

Once you get into classes, it becomes iffy, due to virtual pointers.
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 16 Jul, 2008 - 05:40 PM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 338



Thanked 9 times

Dream Kudos: 100
My Contributions


I concur, this is generally a good practice.

There are numerous versions of this too in the Windows API with

ZeroMemory()
NdisZeroMemory(),
RtlZeroMemory()

though more than likely they are just wrappers around memset
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 17 Jul, 2008 - 03:50 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Makes sense to me. It's all zero bytes under the hood, after all.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 17 Jul, 2008 - 09:52 PM
Post #5


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


I'd like to point out as an FYI, that you should not use ZeroMemory() in cases where you are trying to deallocate a buffer which is never read before it goes out of scope.

Take a look at this sample code:
cpp
WCHAR szPassword[MAX_PATH];

// Retrieve the password
if (GetPasswordFromUser(szPassword, MAX_PATH))
UsePassword(szPassword);
// Clear the password from memory
ZeroMemory(szPassword, sizeof(szPassword));


In this case the compiler could optimize the call because the szPassword buffer is not read from before it goes out of scope. The password would remain on the application stack where it could be captured in a crash dump or probed by a malicious application.

In such situations, the SecureZeroMemory() function is recommended. Use it the same way as ZeroMemory(), it accepts the same parameters.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:35AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month