Like Tree2Likes
  • 1 Post By Elkvis
  • 1 Post By phantomotap

Is there a way to check the size of a dynamically allocated object?

This is a discussion on Is there a way to check the size of a dynamically allocated object? within the C++ Programming forums, part of the General Programming Boards category; Since you can't use sizeof with a dynamically allocated object, I'm wondering if there's possibly a RTTI function that could ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Is there a way to check the size of a dynamically allocated object?

    Since you can't use sizeof with a dynamically allocated object, I'm wondering if there's possibly a RTTI function that could return this for me? Or is it not possible?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Keep track of the size as you allocate dynamically.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Or you could use smart containers like std::string and std::vector, which track the size for you.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    The problem is I'm modding a dll and don't have access to the exe code, so I can't track the size of objects whose classes are defined in the exe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Objects of those classes should have some way of tracking the size, so use that. It should be similiar to Salem's suggestion of containers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,144
    unless the pointer is just a typedef for a void*, you could get the size from sizeof(*myPointer).
    laserlight likes this.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    Oh, good point. If you are just talking about a single object, then yeah.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,810
    If you are just talking about a single object, then yeah.
    [...]single object of the real object type[...]

    If the object is of some derived class that will not help.

    Soma
    laserlight likes this.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Okay, thanks for the suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-18-2010, 07:52 AM
  2. Replies: 2
    Last Post: 11-28-2008, 04:15 PM
  3. Dynamically allocated size
    By maverickbu in forum C++ Programming
    Replies: 12
    Last Post: 06-26-2007, 01:16 PM
  4. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  5. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM

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