CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2004
    Posts
    216

    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!

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    ... 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


  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    ...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++...

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    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).

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    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"...
    **** **** **** **** **/**

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    new T[ N ] produces an array of T and therefore returns T*

    so new obj * [ N ] should return obj **

  8. #8
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    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*'.
    **** **** **** **** **/**

  9. #9
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    My return type is not correct, I check what is it.
    **** **** **** **** **/**

  10. #10
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    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
  •  





Click Here to Expand Forum to Full Width

Featured