C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
memset arguments Posted by istrasci on 30 Sept 2005 at 10:53 AM
memset has the signature

void *memset(void *s, int c, size_t n);



Anyone know what happens if you call it this way??

memset(NULL, <some number>, <some size>);


I'm wondering if sees the NULL and then knows not to do anything (like a no-op) or if it will crash/something-else because it's trying to access the NULL... Or is the whole situation just undefined...???

I can't find any documentation or examples that tells what happens in this case... Of course I could write a quick test program, but it would be platform-specific, and I want to know if there is a standard definition of this behaviour... My point is, I have a lot of code to zero out a structure tree with lots of pointers, and I'm hoping there's some way that it will be smart and do a no-op... Otherwise, everywhere I could potentially have a NULL pointer, I'm going to have to go add

if (ptr)
    memset(ptr, 0, <whatever size of the struct it's pointing to> );


And I really don't want to have to do that...

TIA
Report
Re: memset arguments Posted by stober on 30 Sept 2005 at 11:04 AM
The standards doesn't say what happens, so I guess it is undefined behavior.

http://www.sics.se/~pd/ISO-C-FDIS.1999-04.pdf

Report
Re: memset arguments Posted by Gregry2 on 30 Sept 2005 at 10:03 PM
try it! ^^ close all ur programs and do it....

I just did, it crashes...

so just be careful wiht it....

yeah
{2}rIng

Report
Re: memset arguments Posted by DB1 on 30 Sept 2005 at 10:34 PM
: memset has the signature
:
:
void *memset(void *s, int c, size_t n);

:
:
: Anyone know what happens if you call it this way??
:
:
memset(NULL, <some number>, <some size>);

:
: I'm wondering if sees the NULL and then knows not to do anything (like a no-op) or if it will crash/something-else because it's trying to access the NULL... Or is the whole situation just undefined...???
:
: I can't find any documentation or examples that tells what happens in this case... Of course I could write a quick test program, but it would be platform-specific, and I want to know if there is a standard definition of this behaviour... My point is, I have a lot of code to zero out a structure tree with lots of pointers, and I'm hoping there's some way that it will be smart and do a no-op... Otherwise, everywhere I could potentially have a NULL pointer, I'm going to have to go add
:
:
if (ptr)
:     memset(ptr, 0, <whatever size of the struct it's pointing to> );

:
: And I really don't want to have to do that...
:
: TIA
:


You should always initialize pointers, and always check them before you use them. Get yourself in the habit of doing that and save yourself lots of time later.



To understand recursive, first you need to understand recursive

Report
Re: memset arguments Posted by AsmGuru62 on 1 Oct 2005 at 4:19 AM
DB1 - it may be the nature of a code to keep some NULLs in some places - NEEDED NULLs.

I think it may be the compiler specific - some compilers may check for NULL inside memset. However, this function very often inlined and such check may not be possible.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.