#### Archived

This topic is now archived and is closed to further replies.

# Differences with dynamic and static memory

## Recommended Posts

Sfpiano    126
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? //----------------------------------------------------------------------------------------------------------------- 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."

C-Junkie    1099
what problems?

##### Share on other sites
falkone    444
Probably just that you''re using a pointer instead of a regular variable... if you want the value at the memory location contained in the pointer (the space you allocated), you need to use an * before the pointer name... and stuff

##### Share on other sites
C-Junkie    1099
actually, in this situation he''d prolly just access the array normally.

array[10] = 10; works fine either way.

##### Share on other sites
Sfpiano    126
That''s what I thought might be it, but the array is inside of a struct. If list is an array of the structs, I know you can''t do *list[0].array[0], but how would you do it? And my wierd error causes my computer to reboot whenever I try to access the data in the index buffer which was was passed through array[].

//-----------------------------------------------------------------------------------------------------------------
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    126
anyone?

##### Share on other sites
fizban75    130
I think you need to give more information, such as exactly what your problem is and the code that goes along with it. Otherwise, no one will be able to help you out.

##### Share on other sites
Pipo DeClown    804
Initialize it with NULL. Any difference? And delete with [] ofcuz!!

.lick

##### Share on other sites
C-Junkie    1099
quote:
Original post by Sfpiano
If list is an array of the structs, I know you can''t do *list[0].array[0]

struct x { char *y; };

struct x myx[100];
myx[0].y = new char[100];
myx[0].y[0] = 0; // set the first char of the first struct to 0

##### Share on other sites
Sfpiano    126
in my struct:
short* s_pIndices;

In my program:
myStruct* data;data = (QT_NODE*)malloc(sizeof(QT_NODE)*iNumNodes);data[ i ].s_pIndices = new short[6*min*min];nArrayIndex=0;for(i=0; i<5; i++){for(j=0; j<5; j++){pNodeList[iNodeID].s_pIndices[nArrayIndex]=((i+1) * 256)+j;nArrayIndex++;}}

//-----------------------------------------------------------------------------------------------------------------
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."

[edited by - Sfpiano on September 4, 2003 4:53:03 PM]

##### Share on other sites
Cosmic314    2002
Mixing malloc and new calls may hose the memory manager. Use one or the other but not both in the same program.

[MKH: Mixing malloc and delete or new and free can hose the memory manager, using new and malloc in the same program must work.]

[edited by - Magmai Kai Holmlor on September 5, 2003 7:12:05 AM]

##### Share on other sites
JohnBolton    1372
Paraphrasing:

"I''m mixing arrays and pointers and its not working. Can anyone tell me why?"

"Anyone? Can''t anyone read my mind?"

"Here is some code that has several real and potential problems, and still does nothing to clarify my situation."

Seriously, Sfpiano, I bet that if you were able to explain exactly what the problem was, you would have no problem solving it.

##### Share on other sites
Sfpiano    126
You think? See if I knew exactly what the problem was I would tell you. But since I don''t know exactly what the problem is, I can''t tell you. All I can tel you is what I''m doing. That is teh code I use to setup my index buffer. Everything works fine until I try to draw anything out using DrawIndexedPrimitive(...). However, if the indice array is not dynamic, there aren''t any problems. Therefore there must be a problem with the assignment code.

//------------------------------------------------------------------------------------------------------
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."

##### Share on other sites
Pikestriker    122
you are using a for loop to allocate the space for each node right?

eg.

for (int i = 0; i < iNumNodes; i++)
data[ i ].s_pIndices = new short[6*min*min];

##### Share on other sites
Sfpiano    126
Yes.

//------------------------------------------------------------------------------------------------------
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."

##### Share on other sites
Pikestriker    122
What does min = ?
and
iNodeID = ?

[edited by - Pikestriker on September 4, 2003 9:12:39 PM]

##### Share on other sites
Thrump    169
quote:
Original post by Cosmic314
Mixing malloc and new calls may hose the memory manager. Use one or the other but not both in the same program.

I''ve been told it''s ok to use malloc and new in the same program, just don''t use ''free'' on something you created with ''new'' and don''t ''delete'' something you created with malloc.

##### Share on other sites
Ro_Akira    212
I assume the symptom of the problem is it crashing using DrawIndexedPrimitive.

Possible causes might be indices refering to no-existant vertices, or specifing a range indices that don''t match with your SetIndices call. (Like seleting 10 as the start index, when there''s only 9 of ''em).

This is a wild guessing, but...

Are you saying it works if you have

struct myStruct
{
short s_pIndices[6 * 256];
};

struct myStruct
{
short *s_pIndices;
};

?

Are you doing anything like sizeof (myStruct), or sizeof (data.s_pIndices)?

##### Share on other sites
Sfpiano    126
Here''s how I create and define teh index buffer:
pDevice->CreateIndexBuffer(sizeof(*pNodeList[iNodeID].s_pIndices), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, 									D3DPOOL_DEFAULT, &pNodeList[iNodeID].pIB, NULL);			VOID* pIndexData=0;		pNodeList[iNodeID].pIB->Lock(0, sizeof(*pNodeList[iNodeID].s_pIndices), &pIndexData, 0);		memcpy(pIndexData, &pNodeList[iNodeID].s_pIndices, sizeof(*pNodeList[iNodeID].s_pIndices));		pNodeList[iNodeID].pIB->Unlock();

//------------------------------------------------------------------------------------------------------
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."

##### Share on other sites
Ro_Akira    212
Unless I'm gone mad, sizeof (*(pNodeList[iNodeID].s_pIndices)) ( added the extra brackets for clarity), won't work. sizeof works out the size of a data structure at compile time.

Since pNodeList[iNodeID].s_pIndices is a pointer, sizeof will probably be returing 4.

[EDIT]
Actually, since it's sizeof (*(pNodeList[iNodeID].s_pIndices)), and not sizeof (pNodeList[iNodeID].s_pIndices) , I'm not sure what it will return.
[/EDIT]

When you declare it not as a pointer but as an array, the compiler, and thus sizeof, knows the size of the array, and it'll return the expected size.

Solution: Keep the size/number of indices in another variable in your structure.

But you probably knew all this already, and just didn't notice the problem.

Cheers,
Ro_Akira

[edited by - Ro_Akira on September 4, 2003 10:18:40 PM]

##### Share on other sites
Sfpiano    126
But the whole point of my thing is to not have the size be known until it''s passed in the constructor, otherwise I wouldn''t have this problem.

//------------------------------------------------------------------------------------------------------
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."

##### Share on other sites
Ro_Akira    212
You're saying to me that you don't want the size to be know at compile time

e.g.

array = new [size_I_want_to_pass_into_constructor];

Yes?

Fine. Just make sure you store "size" somewhere, so you can do things like this:

memcpy (pIndexData, &(pNodeList[iNodeID].s_pIndices), size);

as opposed to using sizeof. You can't use sizeof to get the size of dynamic arrays. Ever.

[EDIT]
Or to be more accurate, dynamically allocated arrays.
[/EDIT]

[edited by - Ro_Akira on September 4, 2003 10:43:51 PM]

##### Share on other sites
Pikestriker    122
What he means is have the size in the structure with the pointer
int Size;

so when you go through the constructor you can update this variable as well as allocate the memory needed for the array. This way in your call you can use

sizeof (struct) * struct.Size

Hope that helps

##### Share on other sites
Ro_Akira    212
Pikestriker,

The code Sfpiano has shown so far, has involved using sizeof in an attempt to get the size of a dynamically allocated array, and this is wrong.

You were right up until this line:

sizeof (struct) * struct.Size

He shouldn't be using sizeof at all, at least in the lines of code shown up to now.

i.e.

// Incidently, you're confusing me with your mix of structs and constructors! I hope this is kinda what your doing...class myStruct{public:    myStruct (long new_size)    {        size_of_s_pIndices = size;        s_pIndices = new short [size_of_s_pIndices];    }    short *GetIndices () const { return s_pIndices; }    long  GetSize ()           { return size_of_s_pIndices; }    Direct3D8IndexBuffer *GetD3DIndexBuffer () { return pIB; }    // Etc.private:    short *s_pIndices;    long  size_of_s_pIndices; // Named just to make it obvious what it's for    Direct3D8IndexBuffer *pIB; // Direct3D8IndexBuffer is made up from the top of my head. Sfpiano is using this with CreateIndexBuffer etc. and I'm guessing this is what it is    myStruct () {}};myStruct *pNodeList;// Code etc.pDevice->CreateIndexBuffer (pNodeList[iNodeID].GetSize (), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &pNodeList[iNodeID].GetD3DIndexBuffer (), NULL);VOID* pIndexData=0;pNodeList[iNodeID].GetD3DIndexBuffer ()->Lock (0, pNodeList[iNodeID].GetSize (), &pIndexData, 0);memcpy(pIndexData, pNodeList[iNodeID].GetIndices (), pNodeList[iNodeID].GetSize ());pNodeList[iNodeID].GetD3DIndexBuffer ()->Unlock();// Code etc.

[edited by - Ro_Akira on September 5, 2003 1:59:12 AM]

[edited by - Ro_Akira on September 5, 2003 1:59:57 AM]

##### Share on other sites
To add yet another voice, sizeof is definitely your problem. This is a typical C mistake, actually. sizeof() is evaluated at compile time, never at runtime, and it returns the size of a type. If you say sizeof(*pointer) it returns the size of the element that pointer points to. Makes sense, right? And sizeof(pointer) always returns 4 since a pointer is a 32-bit integer.

In other words, for a type T, sizeof(*(T*)) returns sizeof(T) obviously.

Stupidly, and this is moronic language design, sizeof() on static arrays returns the size of the entire array. It''s confusing and unnecessary. It should return 4 and be done with it.

~CGameProgrammer( );