Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Structure

Status
Not open for further replies.
Dec 15, 2005
8
CA
Hi there,

I'm doing some C++ in Visual Studio 2005 and I'm looking at memory while debugging. However, I don't really know how to "decode" it or just don't know how much memory the data type takes.
When I look at the data address, I get a bunch of hex, of course, but I can only know how much memory it took if I delete the object (so I see the changes). I know there must be a way of knowing the total memory taken by the object only by looking at the hex data, however I don't know how. I've been looking on the Internet but I haven't found anything that would describe me how memory is placed (byte per byte).
Does anyone know a place or has an answer to that question? How can I take a footprint of memory used?

Thanks a lot lot!

-Jeremie
 
Did you try the sizeof operator?
sizeof will tell you the size of all the stack portion of an object; if it dynamically allocates memory for stuff it will only show as 4 bytes for the pointer:
Code:
#include <iostream>
#include <cstring>

class CTest
{
public:
	int		Num;

	void Set( const char* str )
	{
		if ( Str != NULL )
		{
			delete [] Str;
			Str = NULL;
		}

		Str = new char[strlen(str) + 1];
		strcpy( Str, str );
	}

	CTest()
	: Str(NULL) {}

	~CTest()
	{
		delete [] Str;
	}

private:
	char*	Str;
};

using namespace std;

int main()
{
	CTest str;
	cout << "size is: " << sizeof(str) << endl;	// size is 8.

	str.Set( "Hello World" );
	cout << "size is: " << sizeof(str) << endl;	// size is still 8.

	return 0;
}
 
Hi there,

Actually I was looking for something that would let you know, even if there are dynamically allocated members how much (total) would this object be worth. But I guess this is not possible, if I'm right.

Thanks though,

-Jérémie
 
You'd need to write your own size() member function for that.
 
But if debuger is informed what object type pointer points to it shows all members correctly - what actually the problem then?
 
But if debuger is informed what object type pointer points to it shows all members correctly - what actually the problem then?

And why shouldn't he show it? object variable is a pointer to memory, where the object is kept. For simple data type members, he shows the values. And for pointers, the debugger checks the memory that the pointer points to, and prints out the data members there according to the pointer type.

I know there must be a way of knowing the total memory taken by the object only by looking at the hex data

If the object does not have dynamic data allocated for some of its members, then all of objects data is kept within one place in memory. If the object has allocated pointers, then sure it won't all be in one place, as the allocated memory theoretically can be thousands of pages away from object definition. You need to check the place the pointer points into to check what hex values are there.

And finally, before you start reading memory dumps, read about byte ordering, data type sizes and pointer stuff. That'll make you wonder for less things ;)

------------------
When you do it, do it right.
 
Remember too that almost everything written here does not apply to Visual Studio's garbage collected heap, where things can be moved about. It's not merely garbage-collected, it's also defragmented. Therefore a dump of a chunk of memory now might not look the same a second later.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top