Determine array length in heap?

This is a discussion on Determine array length in heap? within the C Programming forums, part of the General Programming Boards category; Hi, I am wondering if it is possible to determine how much space has been allocated for a particular pointer ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    48

    Determine array length in heap?

    Hi,

    I am wondering if it is possible to determine how much space has been allocated for a particular pointer so I can determine how many elements are in its range.

    The same method used for an array on the stack doesn't seem to work:
    Code:
    unsigned int             *R_offd_i;
    
    posix_memalign((void *)&R_offd_i, 16,  4 * sizeof(*R_offd_i));
    
    printf("In Main: sizeof(R_offd_i) / sizeof(*R_offd_i) = %d / %d\n", sizeof(R_offd_i), sizeof(R_offd_i[0]));
    The print statement prints " 4 / 4" which makes sense, but is not what I want.

    I will address the obvious point and that is that the size is known when I allocate the space. I am passing R_offd_i to a function and that function needs to know how big it is and passing the size as a parameter is, well, going to make my function look like a complete mess so I am hoping to find another way.

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BdON003 View Post
    passing the size as a parameter is, well, going to make my function look like a complete mess
    The answer is no, which is perhaps why it is common to pass a length as a function parameter.

    Another option if the program is not threaded is to use a global variable Len, which you can set before the call. This only makes sense if you have a number of functions all of which use a "length" or "size" variable for passing something. But also it would be a little maverick. Len, a little maverick multi-purpose variable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Nerds. I hoping there was a 'but' coming after that first statement.

    My problem is that my function has 15 such pointers and I need to know the size of them all and they can all have a unique size. What do you call a large group of "little maverick multi-purpose variables"? A Flock? Horde?

    Looking at the code, some pointers are not even malloc'd in the calling function but are just the lvalue of another function call. For instance:
    Code:
    unsigned int             *R_offd_i;
    
    R_offd_i = someOtherFunction();
    
    myFunction(R_offd_i, <little tiny mavericks>,...);
    Any suggestions on how to go about knowing the size of space allocated to R_offd_i inside myFunction without making global variables that will span many files for each pointer? I am trying to not modify the code outside of myFunction, though I may need to relax that constraint and just hope not to modify code outside of this calling function.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BdON003 View Post
    Nerds.

    My problem is that my function has 15 such pointers and I need to know the size of them all and they can all have a unique size. What do you call a large group of "little maverick multi-purpose variables"? A Flock? Horde?
    Now you're on to something that is almost acceptable.
    Code:
    int *Sizes;
    You can malloc and realloc Sizes as an array (so now it has Sizes[2], Sizes[6], etc).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Thanks for the input. I know what must be done now. I need to just trace back every pointer that I need to where it is initialized and determine the best way to track its size.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I would suggest using a structure, or perhaps something like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 02:52 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 05:53 PM
  4. How to init an array dynamically on the heap?
    By karantsch in forum C Programming
    Replies: 6
    Last Post: 01-25-2003, 01:07 AM
  5. 2-dem array
    By alika in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 11:59 AM

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