Thread: malloc (0)

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    malloc (0)

    Hey,

    I'm doing some weird things (mix of C/C++), and I'd like to know if I was allowed to use malloc(0):

    Code:
    #include <stdlib.h>
     
    int main()
    {
         int *i = malloc (0);
         free(i);
     
         return 0;
    }
    Or if Windows may crash after that...
    I succeeded making Windows crash with my weird programs once

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't crash Windows with non-kernel mode apps... It will crash your application, sure, but nothing else.
    Malloc(0) if I'm not mistaken is allowed, but may give unexpected results (such as an allocation of 0 bytes). Best if you don't do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    I did it a few times (crashed windows), running some SDL application of my own right after windows started. (blue screen of death... I saw it on Vista!)

    I'm totally OK with allowing 0 bytes, i'm just wondering if free(ptr) will behave normally on a 0-byte allocated pointer, and if i may have a memory leak.

    Sorry for my bad english :-/
    Last edited by coyotte508; 05-11-2008 at 08:21 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by coyotte508 View Post
    I did it a few times (crashed windows), running some SDL application of my own right after windows started.
    Probably bad drivers or you did something wrong that confused the drivers.

    I'm totally OK with allowing 0 bytes, i'm just wondering if free(ptr) will behave normally on a 0-byte allocated pointer, and if i may have a memory leak.
    Yes, free will work, but you probably won't be able to use your allocated memory for anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    7
    ok thanks a lot.

    It'd be long to explain why in some cases i may allocate 0 bytes.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't believe there's any good reason for it.
    Perhaps you should explain your situation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As it stands, there is nothing wrong with your initial program.

    Dereferencing the pointer would be a bad thing to do (malloc(0) is allowed to return NULL).

    If there is any crashing in the program, then it's more likely to be some other problem unrelated to this. Cause and effect are separate things when it comes to memory corruption, and all you've seen so far is an effect (not a cause).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    7
    Edit:
    Thanks Salem for your answer, thanks to you I'm sure there are no probs.
    There is no crashing, but i needed to be sure.
    Last edited by coyotte508; 05-11-2008 at 08:46 AM. Reason: Thanking salem

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    also if you don't free, you'll probably have a memory leak due to the record keeping in the CRT.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    You can't crash Windows with non-kernel mode apps... It will crash your application, sure, but nothing else.
    Oh I wouldn't be too sure of that... there's always a way.

    Hell I even managed to get a complete crash on Linux once, and I wasn't doing anything out of the ordinary.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That seems kind of weird.
    What exactly did you do?
    Operating systems don't crash right out of the blur from a user mode application...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The easiest way to crash Linux is to use Wine to execute a buggy Windows application.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wine seems to be doing a little more than just user mode stuff...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by iMalc View Post
    Hell I even managed to get a complete crash on Linux once, and I wasn't doing anything out of the ordinary.
    Were you running as root?

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Wine seems to be doing a little more than just user mode stuff...
    Yes, I'm sure it does.

    I just wouldn't say that you can't crash Windows with a "user-mode" application. You never know what could happen.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM