-
February 4th, 2004, 06:16 AM
#1
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...
-
February 4th, 2004, 06:32 AM
#2
Simple answer: no. Such is the beauty of C++
Toot
Some cause happiness wherever they go; others, whenever they go.
-
February 4th, 2004, 06:50 AM
#3
Could you please explaine what exactly going around ?
-
February 4th, 2004, 08:34 AM
#4
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.
-
February 4th, 2004, 08:46 AM
#5
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.
-
February 4th, 2004, 09:00 AM
#6
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.
-
February 5th, 2004, 04:39 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|