storing return value not using assignment nor memcpy?

Discussion in 'C++' started by gsyoon, Aug 11, 2005.

  1. gsyoon

    gsyoon Guest

    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.
    gsyoon, Aug 11, 2005
    #1
    1. Advertising

  2. gsyoon

    Alipha Guest

    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?
    Alipha, Aug 11, 2005
    #2
    1. Advertising

  3. gsyoon

    Ram Guest

    > 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
    Ram, Aug 11, 2005
    #3
  4. gsyoon

    gsyoon Guest

    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.
    gsyoon, Aug 11, 2005
    #4
  5. gsyoon

    gsyoon Guest

    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.
    gsyoon, Aug 11, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    461
    Tomás
    May 2, 2006
  2. aiooua

    simple assignment of structs vs. memcpy

    aiooua, Mar 10, 2007, in forum: C Programming
    Replies:
    5
    Views:
    455
    aiooua
    Mar 10, 2007
  3. , India

    structure assignment vs memcpy

    , India, Mar 10, 2007, in forum: C Programming
    Replies:
    2
    Views:
    1,159
    Richard Tobin
    Mar 10, 2007
  4. kuyper

    memcpy() where assignment would do?

    kuyper, Aug 23, 2007, in forum: C Programming
    Replies:
    18
    Views:
    535
    kuyper
    Aug 27, 2007
  5. LAMBEAU Bernard
    Replies:
    1
    Views:
    84
    Markus Schirp
    Jan 8, 2009
Loading...

Share This Page