Thread: Dynamically allocated size

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    14

    Dynamically allocated size

    Is there a way to determine the size of an object/variable that you have dynamically allocated memory to? For example:

    Code:
    int foo() {
        int x;
        return sizeof(x);
    }
    returns 4, or the size of an int.

    Code:
    int foo() {
        char* data = new char[128];
        int size = ???;
        delete {} data;
        return size;
    }
    Is there a way to determine the size of the above dynamically allocated character array?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    To use new char[128] implies that you know that the size is 128.

    I would say that the solution is to keep track of the size of the dynamic array with another variable.

    Oh, and it is delete[] data, not delete{} data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    14
    Quote Originally Posted by laserlight
    I would say that the solution is to keep track of the size of the dynamic array with another variable.
    That's what I've always done. just wondering if there was some voodoo I wasn't aware of. Sorry bout the typo.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    That said, I would prefer std::string or std::vector<char> if I can.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    size_t _msize( void *memblock );
    Don't quote me on that... ...seriously

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by Brad0407 View Post
    Code:
    size_t _msize( void *memblock );
    I think this gives some good reasons not to use _msize.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'll let the function speak for itself:
    Code:
    #include <iostream>
    #include <malloc.h>
    
    int foo() {
        char* data = new char[128];
        int size = _msize(data);
        delete [] data;
        return size;
    }
    
    int main()
    {
    	std::cout << "foo() returns " << foo() << "\n";
    	return 0;
    }
    If you have a better idea, let me know.
    Last edited by Brad0407; 06-26-2007 at 11:13 AM.
    Don't quote me on that... ...seriously

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Try not to have plain pointers in the code. If, for some reason you don't think you can use a vector or any other standard container, put the pointer in a class that also has the size member.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by maverickbu View Post
    That's what I've always done. just wondering if there was some voodoo I wasn't aware of. Sorry bout the typo.
    By interrogating the size of the array and dividing it by the size of the type of data it holds you can use;

    Code:
    int size = sizeof(data)/sizeof(char);

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    When you have a better idea, let me know.
    Code:
    #include <iostream>
    #include <vector>
    
    int foo() {
        std::vector<char> data(128);
        return data.size();
    }
    
    int main()
    {
    	std::cout << "foo() returns " << foo() << "\n";
    	return 0;
    }
    Or even:
    Code:
    #include <iostream>
    
    int foo() {
        return 128;
    }
    
    int main()
    {
    	std::cout << "foo() returns " << foo() << "\n";
    	return 0;
    }
    The point is that it is non-standard and non-portable, and consequently may not return the size as expected for this purpose.

    By interrogating the size of the array and dividing it by the size of the type of data it holds you can use;
    In this case it would just give the size of the pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by indigo0086 View Post
    By interrogating the size of the array and dividing it by the size of the type of data it holds you can use;

    Code:
    int size = sizeof(data)/sizeof(char);
    size will always be 4 as long as pointers are stored in 32 bits of memory and characters in 8 bits.

    >> The point is that it is non-standard and non-portable, and consequently may not return
    >> the size as expected for this purpose.

    Amen
    Don't quote me on that... ...seriously

  12. #12
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Ah, missed the vectors thing. Either way vectors are better.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There are two more problems with _msize, aside from the obvious (it's compiler specific):
    1) new[] might be overloaded and not call malloc at all.
    2) new[] might allocate some additional bookkeeping memory that malloc (and thus _msize) is not aware of. The result would be that the result of _msize might not be evenly divisible by the item size, or the division may result in a higher item count than is actually available.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  2. Run-time error with dynamically allocated 3-D array
    By OceanDesigner in forum C Programming
    Replies: 2
    Last Post: 10-21-2005, 02:29 PM
  3. Dynamically allocated arrays
    By axe786 in forum C Programming
    Replies: 6
    Last Post: 12-03-2004, 01:41 AM
  4. Arrays that can change size dynamically
    By nickname_changed in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 09:15 AM