-
July 19th, 2004, 10:25 PM
#1
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.. firstly, i think the first one is easier to use, but the second one is more like ordered.
i'll really appreciate your help!!
thanks!
-
July 19th, 2004, 10:40 PM
#2
I have made some corrections to your code so that it can compile and run.
Code:
obj *pObj1 = new obj [10000];
obj **pobj2 = new obj *[10000];
for (int i=0;i<10000;++i)
pobj2[i]=new obj;
IMO, although the result from both codes are the same, it is still better to choose the simpler one as it is more readable. Hence easier to maintain or debug.
-
July 20th, 2004, 03:20 AM
#3
... and uses about 40,000 fewer bytes of memory (assuming 4-byte pointers).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 20th, 2004, 04:40 AM
#4
...and is much more safe in regard to memory leaks etc.
Nevertheless, I do not know what kind of exam it was, however, the best method would be...
Code:
#include <vector>
std::vector<obj> Objects(10000);
Granted...it will only work in C++...
-
July 20th, 2004, 04:58 AM
#5
so will what he is doing. In C you'd have to use malloc.
Whether to have an array/vector of objects or of pointers depends on the nature of "obj". Using a vector of objects should be done when they are cheap to construct, cheap and safe (and legal) to copy and cheap and safe (and legal) to assign. Otherwise use a vector of pointers, possibly reference-counted ones (i.e. boost::shared_ptr).
-
July 21st, 2004, 01:22 PM
#6
This is an interesting question if we change the original line:
Code:
obj **pobj2 = new (obj *)[10000]; //syntax error
into:
Code:
obj *pobj2 = new (obj *)[10000];
The latter creates an array of 10000 obj's, and returns a "pointer to an array of 10000 obj's"...
**** **** **** **** **/**
-
July 21st, 2004, 03:59 PM
#7
new T[ N ] produces an array of T and therefore returns T*
so new obj * [ N ] should return obj **
-
July 21st, 2004, 04:04 PM
#8
NMTop40,
Its not a common usage, but there is a "creature" as the OP declared:
new (*obj)[10000];
That "creature" is a pointer to a block of elements (NOT the address of the first element, but as one piece).
That decleration returns 'obj*'.
**** **** **** **** **/**
-
July 21st, 2004, 04:09 PM
#9
My return type is not correct, I check what is it.
**** **** **** **** **/**
-
July 21st, 2004, 04:21 PM
#10
OK, sorry for the previous posts. I was sure for some reason that operator new
can construct the following "pointer to block", apparently it can't:
int (*p)[100]; // ==> a valid pointer to a block of 100 integers.
where sizeof(*p) is 100*sizeof(int)
Last edited by Guysl; July 21st, 2004 at 04:31 PM.
**** **** **** **** **/**
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|