Skip to main content
GameDev.net gamedev.net
🔒 Locked

Differences with dynamic and static memory

Started by Sfpiano Sep 3, 2003 at 7:41 PM 34 replies 2.4k views
Original Post
Sfpiano
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? //----------------------------------------------------------------------------------------------------------------- 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."
C-Junkie
C-Junkie
what problems?
falkone
falkone
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
Disclaimer: "I am in no way qualified to present advice n any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)" o
C-Junkie
C-Junkie
actually, in this situation he''d prolly just access the array normally.

array[10] = 10; works fine either way.
Sfpiano
Sfpiano
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."
//------------------------------------------------------------------------------------------------------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
Sfpiano
anyone?
//------------------------------------------------------------------------------------------------------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."
fizban75
fizban75
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.
Pipo DeClown
Pipo DeClown
Initialize it with NULL. Any difference? And delete with [] ofcuz!!

.lick
C-Junkie
C-Junkie
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
Sfpiano
Sfpiano
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]
//------------------------------------------------------------------------------------------------------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."
Cosmic314
Cosmic314
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]
JohnBolton
JohnBolton
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Sfpiano
Sfpiano
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."
//------------------------------------------------------------------------------------------------------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."
Pikestriker
Pikestriker
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];

Richard DickersonPikestriker Games "Kids you tried hard and failed miserably, lesson learned never try" - Homer Simpson
Sfpiano
Sfpiano
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."
//------------------------------------------------------------------------------------------------------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."
Pikestriker
Pikestriker
What does min = ?
and
iNodeID = ?

[edited by - Pikestriker on September 4, 2003 9:12:39 PM]
Richard DickersonPikestriker Games "Kids you tried hard and failed miserably, lesson learned never try" - Homer Simpson
Thrump
Thrump
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.
Ro_Akira
Ro_Akira
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];
};

instead of

struct myStruct
{
short *s_pIndices;
};

?

Are you doing anything like sizeof (myStruct), or sizeof (data.s_pIndices)?
Sfpiano
Sfpiano
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."
//------------------------------------------------------------------------------------------------------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."
Ro_Akira
Ro_Akira
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.