How to Determine size of array (non-char) from its Pointer?

This is a discussion on How to Determine size of array (non-char) from its Pointer? within the C Programming forums, part of the General Programming Boards category; ya.. I know that.. its because of the page size thats allocated by the OS.. everytime when the process requests ...

  1. #16
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    ya.. I know that..
    its because of the page size thats allocated by the OS..
    everytime when the process requests for memory it will allocated in multiples of page sizes..
    so even though we make malloc call for 100bytes and we access something like 101byte dosent give segfault..
    I believe malloc (or any DMA calls) only keeps record of the mem it is left to allocate (i.e pagesize - allocated) the next time if there is any malloc call..
    so that it can make out if it has to request system for more pages or if the current page allocated by OS is siffucient
    well.. I think if I wanted some thing like runtime_sizeof() then I have to handle all the array elements additions and deletions from the templates in the background..

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    To my understanding, any function that begins with a _ is compiler specific.

    As for another way to know the size of your array, you could always write your own malloc() & free() functions (preferably with different names) and always allocate 4 more bytes than you asked for, then store the size in the first 4 bytes, and return a pointer to (address + 4)...

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    To my understanding, any function that begins with a _ is compiler specific.

    As for another way to know the size of your array, you could always write your own malloc() & free() functions (preferably with different names) and always allocate 4 more bytes than you asked for, then store the size in the first 4 bytes, and return a pointer to (address + 4)...
    [Pedant mode]
    I would suggest adding a "sizeof(size_t)" bytes before the actual data. This is 4 on a 32-bit machine, but 8 on a 64-bit system, so it will automatically be able to cope with larger data storage that you could encounter in a system with larger address range.

    [And of course it will only work for functions that actually allocate through your private allocation routine].

    --
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  3. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 03:21 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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