Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: NULL and HEAP in C

  1. #1
    Join Date
    Aug 2009
    Location
    Mumbai
    Beans
    275
    Distro
    Ubuntu 12.04 Precise Pangolin

    NULL and HEAP in C

    Hi.

    Iv'e done most of my programming in Java, and I'm new to C.

    I was wondering...

    1.What is a NULL value in C? in Java, we often have variables that do not point to something as NULL (string s; s is null here, so I can check that in Java. How do I do so in C??). The closest analogy I could make is that of an uninitialized pointer in C, but that has some garbage and not NULL.

    2.Is there a HEAP in C as well? Which objects are stored in it? I think most of OOP objects are stored in a HEAP and not the program Stack.

    thanks

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: NULL and HEAP in C

    1. NULL is a macro that expands to a null pointer constant. A null pointer constant can be either an integer constant with value 0, or such a constant cast to a void*.

    2. Memory is allocated on the heap when you allocate it with malloc, calloc or realloc. I wouldn't swear on it but I think it's the only case where it is allocated on the heap. If you just declare a variable, the memory for it is allocated on the stack.
    Last edited by Bachstelze; August 3rd, 2011 at 08:59 PM.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: NULL and HEAP in C

    1. NULL == 0 == '\0'
    2. malloc() family of functions allocate memory on the heap and return a pointer to it
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  4. #4
    Join Date
    Dec 2008
    Location
    Deep Woods of PA
    Beans
    699
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: NULL and HEAP in C

    Quote Originally Posted by shashanksingh View Post
    Hi.

    Iv'e done most of my programming in Java, and I'm new to C.

    I was wondering...

    1.What is a NULL value in C?
    NULL == 0

    in Java, we often have variables that do not point to something as NULL (string s; s is null here, so I can check that in Java. How do I do so in C??). The closest analogy I could make is that of an uninitialized pointer in C, but that has some garbage and not NULL.
    A lot of languages denote the concept of NOTHING as NULL, unfortunately C does not really have that given what it was designed to do. The uninitialized pointer in C is not NULL it is initialized to the value currently in memory used for pointer. So the closest analogy to the concept you are describing in Java or Perl is:
    Code:
    char *s = NULL;

    2.Is there a HEAP in C as well? Which objects are stored in it? I think most of OOP objects are stored in a HEAP and not the program Stack.

    thanks
    There is HEAP in C as well. Every time you allocate memory directly using "malloc", "calloc" that memory allocation is on the heap. As far as most of OOP objects being stored on the HEAP you are wrong. The objects are equaly likely to be stored on HEAP as they are in STACK... And actually more objects are on the STACK then there are on the HEAP.
    Regards,

    Karlson

  5. #5
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: NULL and HEAP in C

    Quote Originally Posted by karlson View Post
    NULL == 0
    The question was not what it compares equal to, but what it is. (Then again, "a NULL value" is nonsensical, so hard to tell what the question was exactly...)
    「明後日の夕方には帰ってるからね。」


  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: NULL and HEAP in C

    Also "heap" and "stack" lowercase please, they are not acronyms or macros.
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    May 2006
    Beans
    1,790

    Re: NULL and HEAP in C

    Quote Originally Posted by Bachstelze View Post
    If you just declare a variable, the memory for it is allocated on the stack.
    Only within functions (and then when 'static' is not used), not outside them.

  8. #8
    Join Date
    Jun 2007
    Location
    Canada
    Beans
    370

    Re: NULL and HEAP in C

    Quote Originally Posted by karlson View Post
    NULL == 0
    Not always. That's *normally* the case, but in terms of the actual value, the C standard does not specify that NULL must be zero. It only states that the integer value 0, when cast to a pointer, will be NULL. It does not state however that when NULL is cast back to an integer that the result must be 0.

    The actual memory address pointed to by NULL could be any unavailable address. In practice this is usually 0, but you shouldn't rely on that.

    In C there's also a second null, the '\0' character, used to terminate strings. However, the string null, and the NULL pointer are not the same thing, despite using the same name.
    GCS/O d+(-@) s: a-->? C(++) UL P+ L+++@ E@
    W++$ N++ !o K++ w(++) !O M(-) !V PS+(++)
    PE-() Y+ PGP++ t++(+++@)* 5++ X++@ R+++@
    tv+ b++(+++) DI++ D+ G+ e++>++++ h- r y?

  9. #9
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: NULL and HEAP in C

    Quote Originally Posted by ve4cib View Post
    Not always. That's *normally* the case, but in terms of the actual value, the C standard does not specify that NULL must be zero. It only states that the integer value 0, when cast to a pointer, will be NULL. It does not state however that when NULL is cast back to an integer that the result must be 0.
    Your last statement is true, but the catch is that there is no conversion involved when doing this comparison, and the internal representation of a null pointer is completely irrelevant.

    http://c-faq.com/null/ptrtest.html

    A null pointer is guaranteed to compare equal to 0, regardless of its internal representation.
    Last edited by Bachstelze; August 4th, 2011 at 12:51 AM.
    「明後日の夕方には帰ってるからね。」


  10. #10
    Join Date
    Jun 2007
    Location
    Canada
    Beans
    370

    Re: NULL and HEAP in C

    Quote Originally Posted by Bachstelze View Post
    Your last statement is true, but the catch is that there is no conversion involved when doing this comparison, and the internal representation of a null pointer is completely irrelevant.

    http://c-faq.com/null/ptrtest.html

    A null pointer is guaranteed to compare equal to 0, regardless of its internal representation.
    EDIT: I was speaking more about the internal representation than the comparison anyway.
    Last edited by ve4cib; August 4th, 2011 at 01:02 AM.
    GCS/O d+(-@) s: a-->? C(++) UL P+ L+++@ E@
    W++$ N++ !o K++ w(++) !O M(-) !V PS+(++)
    PE-() Y+ PGP++ t++(+++@)* 5++ X++@ R+++@
    tv+ b++(+++) DI++ D+ G+ e++>++++ h- r y?

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •