473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
4 1942

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
6
by: James H. Newman | last post by:
I have the following: typedef unsigned char T ; T y = { 0x22, 0x33 } ; I now define unsigned short x ;
13
by: Josip | last post by:
I'm trying to limit a value stored by object (either int or float): class Limited(object): def __init__(self, value, min, max): self.min, self.max = min, max self.n = value def...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
3
by: darren | last post by:
Hi there Im working on an assignment that has me store data in a buffer to be sent over the network. I'm ignorant about how C++ stores data in an array, and types in general. If i declare an...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
0
by: Calvin Spealman | last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote: As was pointed out already, this is a basic misunderstanding of assignment, which is common with people learning Python. To your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.