Thread: Pointer alloc size question

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Question Pointer alloc size question

    I realized something that I can't figure out: how do we obtain the length of some dynamic array allocated with a pointer? Considering that any pointer gives a "sizeof()" of 4 bytes, and that these 4 bytes represent an address, then where is the allocation's length stored?

    As with the following, operators "new" and "delete" will know how much space they have to allocate, and how much space they will have to deallocate:

    Code:
    int *ptr;
    ptr = new int[100];
    delete[] ptr; //The function will know that "ptr" points to an array of 100 ints...
    So where can we find out the information that actually tells the allocated size? I know that before "new-ing" it would be a good idea to store the length in a variable if we later want to use that length, but where is the info? Could it be possible that only the OS knows how much space that ptr uses?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You can't. Hence the reason you can go out of the bounds of an array. It's up to the programmer to mark the size of a particular array. As for the delete[] operator. I believe it stores the amount allocated somewhere when a call to new[] is made, or perhaps it allocates an extra null byte so delete[] knows where to stop. I honestly couldn't tell you, you could probably tell by looking at the assembly.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by mikahell
    I know that before "new-ing" it would be a good idea to store the length in a variable if we later want to use that length, but where is the info?
    Yes. Remember how much you ask for. Implementation specifics are then irrelevant.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    new[] and delete[] can store that size information however they choose. SOME (and note, this is just SOME) store the size as 4 bytes before the beginning of the array.

    However, you should really not be passing arrays to functions anyway. Use std::vector instead, and use .size().
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 11:47 AM
  2. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  5. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM