Jump to content
  • Advertisement
Sign in to follow this  
LittleFreak

How much memory do pointers use?

This topic is 4115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to write a mesh system to avoid redundent memory but I think I might be causing some redundent pointer usage.

typedef struct KMESH
{
	float			x, y, z;
	float			rX, rY, rZ;

        KMESHLIST*              parent;
	KMESH*			next;
	
} KMESH;

typedef struct KMESHLIST
{
	LPCSTR			filename;
	void*			meshData;
	KTEXTURELIST*	        texList;
	DWORD			numMaterials;

	KMESH*			instanceList;
	KMESHLIST*		next;
	
} KMESHLIST;


perty much its a binary tree where all left values are meshs and all right values are the instances of those meshes. So if I go to load a mesh that I have already loaded it just creates anougher instance of the mesh. Now what I want to know is by linking back to the parent mesh is this causing redundent data. Or do pointers take up so little data it doesnt matter?

Share this post


Link to post
Share on other sites
Advertisement
The size of a pointer is platform dependant, but on a 32-bit system they are only 4 bytes (not enough to matter, basically).

Share this post


Link to post
Share on other sites
One parent pointer is 4 bytes; maybe 8. 1000+ instances is 4k+ maybe 8k+. I dunno, is 4k a lot of memory for the computer you're running? I doubt even modern phones would have problems with 4k.

Share this post


Link to post
Share on other sites
ok I should be fine... Im just trying to cut out as much overhead as possiable im trying to make a shell for DirectX and dont want to slow it down.

Share this post


Link to post
Share on other sites
First, don't start by optimizing ... in the end, when the program is written and working, if you only ever end up needing the next pointer, not the parent pointer, then remove it ... until then it is far easier to assume this dramatically useful pointer will earn its keep.

The beautiful thing about statically typed OO programs is you can put simple things in classes, write the program, then remove (comment out) things you think are not in use ... and if you are wrong the compiler will tell you so ... and where.

The beautiful thing about modern computers, its not the small stuff that matters anymore (4 bytes wasted per struct), its the big things, like core design decisions, algorithm choices, and fundamental data structures used.

The beautiful thing about modern tools is, the profiler will tell you exactly where most of your time is being spent, so you can optimize where it counts IN A REAL PROGRAM, not where some academic thinks it might matter someday.

good luck

Share this post


Link to post
Share on other sites

Well, memory certainly becomes an issue at some point and careful consideration to the data structures must be maintained. Especially true this is, for instance, in applications dealing with large amounts of data like databases, imaging etc.

You could write somethnig like sizeof(void*) to find out the size of pointer in your system (almosta universally the same as sizeof(int)). Also, if you are writing structures and really thinking about optimisations, you should pay close attention to the data types you use, their paddins and also the order in which you declare them. It may very well be that the members of your struct are laid out in the same order as you have declared them.

Here are a couple of links explaining things in more detail, including padding and the importance of declaration order.
Wikipedia: Data structure aligment
MSDN: Structure Aligment Examples
Optimizing C and C++ code by EventHelix
and finally resource from AMD also Optimizing Your C/C++ Applications, Part 2

I don't know then if the compiler is allowed to change the order of the fields defined in structures. If they are, it most likely has to be declared explicitly by the user. As it happens to be, paddings are in general machine architecture (and occasionally compiler) spesific and sometimes code has to be shared. In that case, it is vital that if a struct is being shared, bot sides expect the same aligment.

Share this post


Link to post
Share on other sites
Quote:
Original post by Naurava kulkuri
You could write somethnig like sizeof(void*) to find out the size of pointer in your system (almosta universally the same as sizeof(int)).


That's not true. On 64 bit architectures, int will tend to be 4 bytes and pointers will be 8 bytes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!