adv and disadv of the two following data structures:
-
Hi i've got these two data structures: obj *pObj1 = new obj[10000]; obj **pobj2 = new (*obj)[10000]; for (int i=0;i<10000;++i); pobj2[i]=new obj; which are the advantages and disadvantages from each one of them? i know it's an exam question, but it seems to be interesting. i can't think of many.. first, i think the first one is easier to use, but the second one is more like ordered. i'll really appreciate your help!! thanks!
-
Hi i've got these two data structures: obj *pObj1 = new obj[10000]; obj **pobj2 = new (*obj)[10000]; for (int i=0;i<10000;++i); pobj2[i]=new obj; which are the advantages and disadvantages from each one of them? i know it's an exam question, but it seems to be interesting. i can't think of many.. first, i think the first one is easier to use, but the second one is more like ordered. i'll really appreciate your help!! thanks!
1. The first is much easier to understand for most people 2. The second is useful when you might have to use a 2D array. 3. The first method allocates memory for the objects in one big allocation. If there is not enough contiguous memory left it will fail. The second method is good when memory is fragmented as it allocates individual chunks of memory for each object. 4. The second is useful if two items in the array must actually point to the same object - in the first case, each array element is unique. Those are a few to get started
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Hi i've got these two data structures: obj *pObj1 = new obj[10000]; obj **pobj2 = new (*obj)[10000]; for (int i=0;i<10000;++i); pobj2[i]=new obj; which are the advantages and disadvantages from each one of them? i know it's an exam question, but it seems to be interesting. i can't think of many.. first, i think the first one is easier to use, but the second one is more like ordered. i'll really appreciate your help!! thanks!
- static: you can't change the size of your array. 2) dynamic you can create and destroy your array using new and delete. This comes in quite handy when you need a flexible array example of the second for a 2 dimensional array:
//make a temporary array temp = new CString*[nrofrows]; for(int y = 0; y < nrofrows; y++){ temp[y] = new CString[nrofcols]; for(int z = 0; z < nrofcols; z++){ temp[y][z] = ""; } //end for } //end for
AND (VERY VERY IMPORTANT!)//delete the temporary array for(short x = 0; x < nrofrows; x++){ delete []temp[x]; } //end for delete []temp; temp = NULL; //so you don not call that address by mistake and there is something else behind it.
hope this helps. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
- static: you can't change the size of your array. 2) dynamic you can create and destroy your array using new and delete. This comes in quite handy when you need a flexible array example of the second for a 2 dimensional array:
-
Hi i've got these two data structures: obj *pObj1 = new obj[10000]; obj **pobj2 = new (*obj)[10000]; for (int i=0;i<10000;++i); pobj2[i]=new obj; which are the advantages and disadvantages from each one of them? i know it's an exam question, but it seems to be interesting. i can't think of many.. first, i think the first one is easier to use, but the second one is more like ordered. i'll really appreciate your help!! thanks!
-
Hi i've got these two data structures: obj *pObj1 = new obj[10000]; obj **pobj2 = new (*obj)[10000]; for (int i=0;i<10000;++i); pobj2[i]=new obj; which are the advantages and disadvantages from each one of them? i know it's an exam question, but it seems to be interesting. i can't think of many.. first, i think the first one is easier to use, but the second one is more like ordered. i'll really appreciate your help!! thanks!
The first consumes
10000 * sizeof(obj)
bytes of the heap while the second consumes10000 * sizeof(obj*) + 10000 * sizeof(obj)
bytes of the heap. That could be a 40000-byte disadvantage. It all depends on the application, however.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen