Operating System - HP-UX
1833782 Members
2615 Online
110063 Solutions
New Discussion

Re: C++ question - copy contructor call (aCC/HP)

 
SOLVED
Go to solution

C++ question - copy contructor call (aCC/HP)

Here is an extract of code which we are having a problem with:
OurClass::instance()->handleException(OurException(params));

Our exception object is created to be passed to the handleException
function. On Windows, Solaris & AIX this creates an exception object and
passes that object to the handleException function.

On HP (using aCC) the object is created, then a copy of that object is
passed to handleException.

This doesn't seem entirely unreasonable, if a little pointless. Is this just
the way they have chosen to implement the spec, or is it wrong? Is it just a
case of poor optimisation?

Is there any way to optimise this out? Or any way around it?

Thanks,
Richard.
5 REPLIES 5
Chris De Angelis
Frequent Advisor
Solution

Re: C++ question - copy contructor call (aCC/HP)

Based on your description, I'm not sure why on HP-UX *only* you would have a copy of this exception object passed to a function that takes it as an argument. In C++, the default behavior is to create a copy of the object being passed as an argument to some function, i.e., call-by-value. If you do not want this to occur, you may modify your "handleException" function to have the exception object parameter passed-by-reference.

So if your handleException() function is declared something like this:

void handleException(OurException e)

change it to this:

void handleException(OurException &e)

and recompile the program. The call to this function will no longer result in a copy of the parameter being made; instead it will be passed by reference, meaning that the parameter is an implicit pointer.

Of course, there may be side effects to this depending on the rest of the program code, but in general it's probably OK as long as you can assure the called function "handleException" can safely act on the actual object passed in rather than a copy of it.

Good luck -
Chris
Chris De Angelis
Frequent Advisor

Re: C++ question - copy contructor call (aCC/HP)

Richard,

I just thought of some possible reasons why you might observe this behavior only on HP-UX while it is probably occurring on the other platforms anyway:

- If you're running this under a debugger, it may not show you that the copy constructor is called if it is inlined or if it is optimized code on those platforms.

Well, OK, just one reason, but something like this may be going on depending on what you're doing to make you say that it only seems to be happening on HP-UX. Like I said, based on your description, it sounds like the exection on HP-UX is running properly.

Chris

Re: C++ question - copy contructor call (aCC/HP)

Thanks Chris, your response is appreciated.

Your suggestion of passing by reference is a good one, but as you suggest there is a down side to the idea.

This code is in a piece of middleware and the handleException function is written by our users in an instance of one of our classes. It isn't possible to just change this kind of information as it is already in use by our customers.

This product is availible on 6 platforms, and HP is probably the least used of them, which is probably why it has taken so long for this to appear.

When it has been tested on these other platforms it has been done under the same conditions, and I put extra trace points into the code to in order to diagnose this, so I didn't need to use a debugger. I was able to see that on HP we went into the copy constructor, but not on any of the other platforms.

The source file I had to modify is platform independant, so the exact same file was compiled on the different platforms to test what was happening.

I agree this probably isn't a bug with the compiler, it doesn't seem unreasonable what it is doing. I suspect that this call to the copy constructor is being optimized out on the other platforms. As we are creating this Exception object in the call to the function, there really isn't a lot of point copying it.

Is there any way to force this to be optimized out?

Thanks,
Richard.
Chris De Angelis
Frequent Advisor

Re: C++ question - copy contructor call (aCC/HP)

Hi Richard,

If you can't modify this source code, I'm not sure what you can do to eliminate the call to the copy constructor. But a more fundamental question: why do you really want to get rid of this call to the copy constructor upon calling "handleException"? Is this called so often or taking so long to execute that it is really slowing the program down? Have you seen this as a result of profiling the program at runtime?

- Chris

Re: C++ question - copy contructor call (aCC/HP)

Hello Chris,

Sorry about the delay in my reply, the forum mailer didn't let me know I had another.

The reason we want to get rid of the call to the copy constructor is that the copy constructor doesn't do a complete copy. The Exception object contains a void* and we didn't want to just copy this pointer, as that would be mixing a deep and shallow copy. We can't copy the object that the pointer points to, because we don't know what it is.

Ideally we are looking for a way to fix this without having to rewrite too much.

Suggestions have included some way to track what the object is, so we know its size and can copy it or keeping track of the number of pointers to the object, to ensure the object isn't freed until all pointers are out of use.

None of these solutions have been very practical in our circumstances, and would require more redesign than we would like. Especially considering this is just to make it work on HP.

Richard.