Thread: Calloc Pointer to Array, Find Num Elements Post-Initialization

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Calloc Pointer to Array, Find Num Elements Post-Initialization

    This is more of a theory question than a specific question to a problem.

    Let's say I have dynamically allocated an array on the heap:

    Code:
    int numitems;
    double *ptr;
    scanf("%d",&numitems);
    ptr = (double*) calloc(numitems,sizeof(double));
    Now, without knowing the value of numitems, one can figure out the number of declared elements in an array with:

    Code:
    num_elements = sizeof(array_name)/sizeof(array_name[0])
    (or they can divide by sizeof(array_type) )

    However, when working with pointers, I have not been able to find a method to do the same thing.

    Code:
    sizeof(*ptr);
    returns 4, the size of the memory address and
    Code:
    sizeof(ptr);
    returns 8, the size of a double

    How can one find the number of elements in a dynamically allocated array with the use of pointers?

    The reason I'm asking this is because I had been working on a semester project for my programming class in college. Coming from my c++ beginnings, this was a very frustrating problem, since in c++ you can just use .length() to figure this out. In my project, I had multiple functions being called in other functions. While the original function did not need to know the number of elements in the array, the various functions being called did. Therefore, I initially set out to solve this problem using a sizeof() technique rather than adding a parameter to functions that did not need to know the number of elements. After no luck, I finally just added the extra parameter to the functions.

    While I know just adding the extra parameter is most likely the easiest method, I am still curious as to how one can find the size of the whole array a pointer is pointingto, rather than a single element, without access to the array itself. It has to be able to be done, since the size of the array must be stored somewhere in order for
    Code:
    free(ptr)
    to work correctly

    I'm sorry for the long post, and I extremely appreciate any help or hints.

    Austin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't find out how much memory was allocated, simply from looking at the pointer.

    > since the size of the array must be stored somewhere
    Yes, it is stored somewhere.
    The problem for you is there is no portable API which allows you to extract that value.

    > since in c++ you can just use .length() to figure this out.
    But only on a container like std::vector which provides a portable mechanism for storing additional information such as this, along with a method for retrieving it.

    Oh, and read this - http://c-faq.com/malloc/calloc.html
    regarding your use of calloc when allocating space for doubles.

    Also, read the FAQ on why casting malloc/calloc in a C program is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't. Just remember how much memory you allocate. Pass that argument around to functions that need to know the block size.

    If you want to go into code that isn't portable I believe Microsoft has a function called _msize() or something similar that can give you the size of a dynamic block of memory. This won't work for all compilers on all systems, though.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you need to remember the size of the buffer, then remember it, i.e. in a variable somewhere. If this seems like a pain, then put the buffer and the size together in a struct.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no portable way of doing this. Some compilers (or runtime libraries) have extensions to allow the size to be extracted from the "malloc header".

    My suggestion would be that "you remember what you done", in other words, you pass along a size to the functions that need to know how many elements it is.

    By the way ".length()" only works because vectors, etc, store the length. If you want to do the same in C, you can use a struct like this:
    Code:
    struct double_vector
    {
        size_t length;
        double *data;
    };
    --
    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.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    thank you for the quick replies

    I had the struct idea, and I don't remember exactly why I didn't end up going through with it.

    I ended up using the variable solution, but I just wanted to know if there was an alternative method. As for the calloc and malloc usage, I just followed the instructions of the professor, but I will certainly check out the provided links and look into _msize().

    Thanks again for the information,
    Austin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM