Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

memset arguments

istrasciistrasci Posts: 107Member
memset has the signature

[code]void *memset(void *s, int c, size_t n);[/code]


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

[code]memset([size=3][blue]NULL[/blue][/size], , );[/code]

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

[code][red]if (ptr)[/red]
memset(ptr, 0, );[/code]

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

TIA

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    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

  • Gregry2Gregry2 Posts: 607Member
    try it! ^^ close all ur programs and do it....

    I just did, it crashes...

    so just be careful wiht it....

    yeah
    {2}rIng

  • DB1DB1 Posts: 1,142Member
    : memset has the signature
    :
    : [code]void *memset(void *s, int c, size_t n);[/code]
    :
    :
    : Anyone know what happens if you call it this way??
    :
    : [code]memset([size=3][blue]NULL[/blue][/size], , );[/code]
    :
    : 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
    :
    : [code][red]if (ptr)[/red]
    : memset(ptr, 0, );[/code]
    :
    : 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.



    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]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.[/blue]
Sign In or Register to comment.