Thread: number of elements in pointer

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    number of elements in pointer

    Hey guys, here's a newbie question:

    suppose I have a ptr to an int, and I allocate memory to it dynamically.

    after all the allocations are done, how can I get the number of ints (elements) in my array?

    i feel like I can do this with some fancy sizeof stuff, instead of maintaining a counter..


    Thanks for the help!!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You can't.

    You have to maintain a counter.

    If you want someone to keep the counter for you, use a vector.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well there is sizeof foo / sizeof foo[0]; but that expression only works for arrays that are in scope. So don't use it after you pass foo to a function, the type is different.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well usually you can do something like:
    Code:
    int *ptrArray = NULL;
    int nNumberOfInts = 20;
    unsigned totalSize = nNumberOfInts * sizeof(int);
    ptrArray = (int*) malloc(totalSize);
    // ptrArray now points to a block of memory that is
    // number of elements * the size of an int
    // so
    printf("Total size of block of %d integers should be: %d\n", nNumberOfInts, totalSize);
    printf( "Or, sizeof(ptrArray[0] * nNumberOfInts = %d\n", sizeof(ptrArray[0]) * nNumberOfInts);
    Output:
    Code:
    jeff@jeff-gate:~/dev/arraysize$ ./arraysize_test 
    arraysize_test 1.0
    Testing: arraysize
    Total size of block of 20 integers should be: 80
    Or, sizeof(ptrArray[0] * nNumberOfInts = 80
    jeff@jeff-gate:~/dev/arraysize$
    Guess it works
    Last edited by jeffcobb; 05-11-2010 at 11:51 PM.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    And it won't work for dynamic arrays.

    sizeof() is a compile-time operator.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    269
    I see. So I can do that with arrays (the sizeof stuff), but not pointers.

    Thanks for the help guys! you are all really fast!
    Last edited by dayalsoap; 05-11-2010 at 11:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM