Thread: Stdlib or Limits

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    18

    Question Stdlib or Limits

    Sorry if this question sounds a bit retarded, still new to C programming.

    Why is malloc necessary if the same result can be obtained by simply using sizeof, declared in the header file limits.h, rather than malloc in stdlib.h?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,556
    Quote Originally Posted by ItzBlue
    Why is malloc necessary if the same result can be obtained by simply using sizeof, declared in the header file limits.h, rather than malloc in stdlib.h?
    malloc and sizeof do very different things. sizeof is an operator; it is not declared in any header file. Perhaps you could show code to demonstrate your thoughts?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    18
    Oh, figured. Thanks for clearing it up.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    sizeof would tell you the size of a thing in bytes. malloc() takes a specific number of bytes and allocates them on the heap.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdlib help!!
    By clique in forum C Programming
    Replies: 5
    Last Post: 10-08-2008, 10:57 AM
  2. Compiler won't take #<stdlib.h>
    By omnificient in forum C Programming
    Replies: 5
    Last Post: 12-11-2007, 02:36 PM
  3. Stdlib header help
    By MaGaIn in forum C++ Programming
    Replies: 8
    Last Post: 09-16-2007, 07:02 AM
  4. stdlib.h system()
    By pktcperlc++java in forum C Programming
    Replies: 8
    Last Post: 01-27-2005, 05:02 PM
  5. stdlib.g/system() <- basic questions.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-11-2002, 07:31 AM