467,878 Members | 1,245 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,878 developers. It's quick & easy.

storing return value not using assignment nor memcpy?


hi, all.

I'm trying to make a "framework" to store the return value of a
function to a global memory.

My first attempt was

1)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
previously_allocated_mem = func() ;
...
}

However, there are some classes that override their
assignment operator and making it non-public.
(using copy constructors don't help much as they can
be overridden and made private).

So, I tried to memcpy as

2)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
memcpy ( previously_allocated_mem, (void*) (&(func())), sizeof
(CLASS_A) ) ;
...
}
2) worked fine for gcc. However, for Solaris CC,
it caused a compile error like
"Error: The "&" operator can only be applied to a variable or other
l-value."

How can I make a general framework to store the return value of
function
regardless of their operators being overridden and made non-public
and the compilers used?

Any comments are welcomed.
Thanks in advance.

Gwang Sik Yoon.

Aug 11 '05 #1
  • viewed: 1664
Share:
4 Replies

gsyoon wrote:
hi, all.

I'm trying to make a "framework" to store the return value of a
function to a global memory.

My first attempt was

1)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
previously_allocated_mem = func() ;
...
}

However, there are some classes that override their
assignment operator and making it non-public.
(using copy constructors don't help much as they can
be overridden and made private).

So, I tried to memcpy as

2)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
memcpy ( previously_allocated_mem, (void*) (&(func())), sizeof
(CLASS_A) ) ;
...
}
memcpying classes is a huge no-no. you're making a copy without going
through "the proper channels" (invoking the copy constructor) and so
you're bypassing the constructor code that will perform a *proper*
copy. And so, the destructors will end up trying to free the same
memory, or fun stuff like that.


2) worked fine for gcc. However, for Solaris CC,
it caused a compile error like
"Error: The "&" operator can only be applied to a variable or other
l-value."

How can I make a general framework to store the return value of
function
regardless of their operators being overridden and made non-public
and the compilers used?

Any comments are welcomed.
Thanks in advance.

Gwang Sik Yoon.


personally, I don't see how such a general framework would be useful,
and why you'd need it to work on types that don't have an accessible
operator= (you can impose at least a couple minimal requirements, can't
you?). And to top it off, you stuff it into a global variable.

anyway, if the function is returning by value, then it would be a
requirement that the type has a copy constructor.

p = std::auto_ptr<T>(new T(func)); // perhaps or something

if the function returns a reference, then you'd be able to get a
pointer to the object and hold onto that perhaps? of course, hard to
say when the pointed-to object would get destroyed and your pointer
would become invalid.

So now you have two different scenarios you'd have to code for. perhaps
use template specialization to execute one set of code if the function
returns a value, and the other if the function returns a reference?

Aug 11 '05 #2
Ram
> I'm trying to make a "framework" to store the return value of a
function to a global memory.


I fail to understand what such a framework would be useful for. You
anyway need to refer to the global memory to retrieve your value. Are
you trying to save some space on local variables? Having return value
in a global memory is not a good idea. You might be in trouble if such
a code is used in a multithreaded application.

-Ramashish

Aug 11 '05 #3
Thanks Alipha,

(This is my first reply to any posting with google's news interface and
I'm a little bit worried if it would appear easy to read. ;))

Alipha wrote:
gsyoon wrote:
hi, all.

I'm trying to make a "framework" to store the return value of a
function to a global memory.

My first attempt was

1)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
previously_allocated_mem = func() ;
...
}

However, there are some classes that override their
assignment operator and making it non-public.
(using copy constructors don't help much as they can
be overridden and made private).

So, I tried to memcpy as

2)
void store_to_global( char * type_name ) {
if ( strcmp( type_name, "CLASS_A") )
memcpy ( previously_allocated_mem, (void*) (&(func())), sizeof
(CLASS_A) ) ;
...
}
memcpying classes is a huge no-no. you're making a copy without going
through "the proper channels" (invoking the copy constructor) and so
you're bypassing the constructor code that will perform a *proper*
copy. And so, the destructors will end up trying to free the same
memory, or fun stuff like that.


I do know that copying an object has many possible hazards. However, my
top-priority mission is to store the returned object by the function
programmed by anonymous programmers so that I can take a look into as
many details about the object as possible. :(


2) worked fine for gcc. However, for Solaris CC,
it caused a compile error like
"Error: The "&" operator can only be applied to a variable or other
l-value."

How can I make a general framework to store the return value of
function
regardless of their operators being overridden and made non-public
and the compilers used?

Any comments are welcomed.
Thanks in advance.

Gwang Sik Yoon.


personally, I don't see how such a general framework would be useful,
and why you'd need it to work on types that don't have an accessible
operator= (you can impose at least a couple minimal requirements, can't
you?). And to top it off, you stuff it into a global variable.


I have to handle that case when an perverted programmer writes a
function that returns a class that have no such operator. Though
perverted, I cannot deny him/her as my customer :(
If there's no other way, I guess, I have to follow your advice to
impose restrictions. But I have to try all that I can do, haven't I? :)

(global variable is not really what I use. It's introduced to make it
simple.)

anyway, if the function is returning by value, then it would be a
requirement that the type has a copy constructor.

p = std::auto_ptr<T>(new T(func)); // perhaps or something

if the function returns a reference, then you'd be able to get a
pointer to the object and hold onto that perhaps? of course, hard to
say when the pointed-to object would get destroyed and your pointer
would become invalid.

So now you have two different scenarios you'd have to code for. perhaps
use template specialization to execute one set of code if the function
returns a value, and the other if the function returns a reference?


Thanks for your advice. I really appreciate it.
But I hope there might be simpler way to do what I want to do.

Aug 11 '05 #4


The term global variable was introduced to make the description of the
problem simple.
but I guess it did more harm than good.
Actually, it's a dynamically allocated memory.

I'm saving the return value of a function for later inspection after
the scope of
the returned value.

Anyway, thanks for your advice Ramashish.

Aug 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Alfonso Morra | last post: by
18 posts views Thread by Pedro Pinto | last post: by
6 posts views Thread by James H. Newman | last post: by
13 posts views Thread by Josip | last post: by
127 posts views Thread by sanjay.vasudevan | last post: by
reply views Thread by Calvin Spealman | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.