Differences with dynamic and static memory

Started by
34 comments, last by Sfpiano 20 years, 7 months ago
quote:Original post by Sfpiano
If I use this:
short array[6*256];
Everything works out fine.

But if I do this:
short* array;
array = new short[6*256];
I get problems.

What''s the difference?


You''re allocating a very large array. It''s likely that it cannot be assigned. A test maybe whether to check if array == NULL. If it is, then you may be getting an access violation because you are trying to use unassigned memory in the free store.

Advertisement
quote:Original post by Mathematix
quote:Original post by Sfpiano
If I use this:
short array[6*256];
Everything works out fine.

But if I do this:
short* array;
array = new short[6*256];
I get problems.

What's the difference?


You're allocating a very large array. It's likely that it cannot be assigned. A test maybe whether to check if array == NULL. If it is, then you may be getting an access violation because you are trying to use unassigned memory in the free store.



I'd put it another way...

You're allocating a very small array. It's more than likely that it can be allocated. I've met many a larger sized array in my (short) time.

6 * 256 * sizeof (short)
6 * 256 * 2 = 3072 bytes

If he can't allocate ~3k, I'd be worried about what the program's doing (or what machine it's running on, or both)!

[edited by - Ro_Akira on September 5, 2003 7:28:05 PM]
To elaborate on Akira's point, remember that memory is allocated as a single chunk -- the length of an array doesn't matter, only the total byte size. So if we have a 32-byte struct called Vertex3D, then:

new Vertex3D [8];

is exactly the same as:

new float [64];

Both statements allocate 256 bytes of memory via one call to malloc(). Also, huge amounts of dynamic memory can be allocated -- it's only static arrays that can cause problems if they're too large.

~CGameProgrammer( );



[edited by - CGameProgrammer on September 5, 2003 8:32:30 PM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Thanks Ro_Akira, that did it.

//------------------------------------------------------------------------------------------------------
The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you''re the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Sfpiano,

Good stuff! I thought we''d lost you on this thread (a day''s a long time in forum-time)! I presume you used the block of code I wrote as I guide then. If you did, just one thing, this line:

memcpy(pIndexData, pNodeList[iNodeID].GetIndices (), pNodeList[iNodeID].GetSize ());

should probably be changed to this:

memcpy(pIndexData, pNodeList[iNodeID].GetIndices (), pNodeList[iNodeID].GetSize () * sizeof(*(pNodeList[iNodeID].GetIndices ())));

Note the use of sizeof, just to confuse ya! Seriously though, GetSize () should probably be called GetNumberOfIndices () (or better yet, rename the current GetIndices () function GetRawIndexBuffer () or the like, and rename GetSize () as GetIndices ()).

The reason is because the current GetSize () function returns the number of indices in the buffer, as opposed to the number of bytes. This is what CreateIndexBuffer, Lock, and Unlock want, so that''s ok. But memcpy always deals in the number of bytes. The sizeof(*(pNodeList[iNodeID].GetIndices ())) of the new code should return 2 for the size of a short. And the beauty is, if the class is ever changed to use "long"s instead of "shorts", sizeof will automatically get the size of a long (which is usually four bytes)!

Just to make sure, par example:

short v;
short *p;

sizeof (v) // will return 2 (bytes)
sizeof (p) // will return 4 (bytes). Pointers are always 4 bytes/32 bits on 32-bit machines).
sizeof (*p) // will return 2 (bytes). We''re getting the size of the variable type that''s being pointed to, as opposed to getting the size of the pointer.

It''d be nice if you could report back if the above ''fix'' really fixed anything, or if they broke beacuse of it.
I thought you left something out by mistake in your previous post, so I just changed to sizeof(short)*iNumIndices;

//------------------------------------------------------------------------------------------------------
The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you''re the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."

This topic is closed to new replies.

Advertisement