Passing by reference of by value?

This is a discussion on Passing by reference of by value? within the C Programming forums, part of the General Programming Boards category; but if malloc were to allocate memory at an address that is to big for the pointer in question to ...

  1. #31
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    but if malloc were to allocate memory at an address that is to big for the pointer in question to hold then you'd get very weird results
    C standard to the rescue again! From 7.20.3p1:
    The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated
    This forces malloc() to return acceptable values.

  2. #32
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Hmm, so that would mean that the size of the pointer returned from malloc is the size of the smallest type of pointer on said system?

  3. #33
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    Hmm, so that would mean that the size of the pointer returned from malloc is the size of the smallest type of pointer on said system?
    It all depends on how many of the bits of a pointer are used to actually store relevant data. I would expect the opposite, though: since any pointer type has to be convertible to void* and back, having void* be at least as large as the largest pointer would facilitate this. malloc() would simply have to know to return values within the range of the smallest pointer type.

    Of course, if one assumes that all pointer values would fit into the smallest pointer type, then void* surely could be the smallest type, and the extra space in larger pointers would simply be unused. This would be a fairly ridiculous system, but not disallowed as far as I can tell.

    Realistically you can expect all pointers to be the same size, and probably the same representation. If they're not, the implementation must ensure that the guarantees of the standard are upheld. As mentioned in the C FAQ, the real difference tends to be how char* (and thus void*) is represented versus other pointer types. But even if they're the same size, the different representation would have caused problems for the original code: it assumed void* was the same as a struct pointer.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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