malloc pointer question..

This is a discussion on malloc pointer question.. within the C Programming forums, part of the General Programming Boards category; i cant understand this line Code: ptr = malloc(number*sizeof(int)); i know that if we have enough memory malloc returns void ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    malloc pointer question..

    i cant understand this line
    Code:
    ptr = malloc(number*sizeof(int));
    i know that if we have enough memory malloc returns void pointer
    and if not then null pointer
    but here by protocol i need to have an address something like

    ptr=&x


    there is no address on the right side

    ??
    Last edited by transgalactic2; 10-21-2008 at 06:28 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    malloc returns an address in itself. You do not need &.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The & symbol means the address of the variable. So in your case it will mean that store to p the address of x.

    What malloc does is finds you a piece of memory. Then returns a pointer to the beginning of the memory. THAT pointer that is returns, the void pointer, has the memory address stored to it.

    Remember that pointers have values that represent memory addresses.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok so in void pointer case he will return an address to the starting of the empty section in the memory.
    what will he return in the null pointer case(when there is not enough free space)
    ??

    in this line there must be an address on the right side

    Code:
    ptr = malloc(number*sizeof(int));

    there is no address to return
    Last edited by transgalactic2; 10-21-2008 at 06:28 AM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    NULL is also a pointer.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by transgalactic2
    what will he return in the null pointer case(when there is not enough free space)
    Obviously, malloc returns a null pointer in "the null pointer case".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the address of null pointer?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is no such a thing. The value of a NULL pointer is (almost always) the address 0.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    NULL means nothing. Consequently NULL pointer means pointing to nothing. That is, the pointer returned by the malloc is pointing no where (This is true if the malloc failed to allocate space in the memory!!!). Hence its always a good practice to check if the malloc returned NULL.

    Hence when you declare a pointer, in most code you initialise it NULL. What you are trying to say there is the pointer is pointer no where explicitly to the compiler to avoid confusion.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    what is the address of null pointer?
    Zero (or something equivalent to zero - in general we do not need to worry about the value of NULL, but comparing a pointer with zero should give the same result as comparing with NULL).

    [Although if I may be pedantic, the address of a NULL pointer is undefined, the VALUE of the pointer is zero, just like the address of the returned value when malloc succeeds is undefined, but the address value that is returned represents memory that you can use in your code - you STORE the address value in a pointer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by tabstop
    NULL is also a pointer.
    Strictly speaking, NULL is a null pointer constant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The address of the pointer would be &ptr. Very defined. Also, a pointer that is equal to NULL (indicating invalid) is NOT necessarily guaranteed to be a zero. Now that's pedantic. See how annoying it is to be pedantic? Not directed at matsp in particular. Just the cranky long timers here.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    The address of the pointer would be &ptr. Very defined. Also, a pointer that is equal to NULL (indicating invalid) is NOT necessarily guaranteed to be a zero. Now that's pedantic. See how annoying it is to be pedantic? Not directed at matsp in particular. Just the cranky long timers here.
    More pedantry: But there is no address of the RETURN value from the function, there is indeed an address of the local variable where the result is eventually stored. NULL is indeed not guaranteed to be zero, but comparing to zero and comparing to NULL should have the same effect in a compiler that follows the C standard. If the compiler doesn't follow the standard, then all bets are off.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by nonoob
    Also, a pointer that is equal to NULL (indicating invalid) is NOT necessarily guaranteed to be a zero. Now that's pedantic.
    Pedantic? That's debatable. A pointer is never directly compared to a null pointer constant since a null pointer constant is converted to a null pointer for the comparison. A null pointer is not invalid, just not dereferenceable. NULL itself is guaranteed to have the value of zero, though whether that zero is 0 or (void*)0 is implementation defined.
    Last edited by laserlight; 10-21-2008 at 07:11 AM. Reason: singular -> not dereferenceable, since null pointers can be compared to other null pointers
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 04:20 AM
  4. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:36 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21