CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2001
    Location
    Turkey
    Posts
    173

    Is this So Costly ?

    Hi,

    There is function like
    MyFunction( T* apElements, int anCount );
    As it can be seen it accepts an array as argument..

    In my code, I have a pointer to array ( MyObject** ),
    and I need to pass this array to the MyFinction...

    If I write like followings in my code, does it becomes a costly operation? I mean, I do de-reference 'a pointer to array' to 'an array'; and the size of array is very large( e.g. 100.000 ); In this case how does 'de-referencing' behave ? Does it dereference all thousands of objects or something else ?

    CMain::OnOk()
    {
    MyObject** pMyObjs;
    // create it...
    // initialize them..
    //...
    MyFunction( *pMyObject, 100000 ); // is this de-referencing costly?
    }

    Thanks...

  2. #2
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212
    Simple answer: no. Such is the beauty of C++

    Toot
    Some cause happiness wherever they go; others, whenever they go.

  3. #3
    Join Date
    Sep 2001
    Location
    Turkey
    Posts
    173
    Could you please explaine what exactly going around ?

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Is this So Costly ?

    Originally posted by the one I mean, I do de-reference 'a pointer to array' to 'an array'; and the size of array is very large( e.g. 100.000 );
    Not quite. T* as a type is just a pointer to T, it's not an array of T. So you have a T**, which is a pointer to a pointer of T, you dereference it and this will become only a pointer to T.

    So the dereference operation only works on the pointer and hence it is quite fast.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Sep 2001
    Location
    Turkey
    Posts
    173
    Thanks Yves M...

    I want to be sure about another thing,

    I always use (*) as an array instead of ([]) like the example above.

    ( by saying 'as an array', I mean, I keep starting point of sets... )

    Does this usage holds some risks?
    Last edited by the one; February 4th, 2004 at 08:59 AM.

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Well, the difference is that with * you can have dynamic arrays at runtime, while you cannot do that with []. The only real danger is that you will have memory leaks or memory overwrites, but that's a common problem for c-style arrays.

    If you are writing in C++, I strongly suggest you have a look at std::vector. A good place to start for this is Gabriel's introduction to vector
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Nov 2002
    Location
    Devon, UK
    Posts
    212
    That's a great article that I will certainly be advising everyone to read. I absolutely agree: STL is both generic and fantastic, but it does have 2 major drawbacks: the documentation is dreadful, hard to find and hard to read; and the code is incredibly hard to read and debug. But that's the price you pay performance, I guess. Of course, you always have a good source of advice...
    Some cause happiness wherever they go; others, whenever they go.

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