using memcpy()

Discussion in 'C Programming' started by ebrahimbandookwala@gmail.com, Oct 1, 2005.

  1. Guest

    HI everyone

    I am supposed to pass back a pointer to a struct from a function whos
    definition cannot be changed

    flight_t * get_item(field_t field , void * data)

    the problem I encounter is whe I pass back the address of a local var
    it gives me garbled output at the calling end ( obv !! )

    so I make a new pointer to the struct 'f'

    f = (flight_t *)malloc(sizeof(ret)); // malloc the amount of space
    memcpy(f,&ret,REC_SIZE);
    // then attempt to copy the mem chunk ( ret is an actual struct )
    return f; // and return a pointer to it

    unfortunately this too also doesnt work ... pls help!!
    , Oct 1, 2005
    #1
    1. Advertising

  2. pete Guest

    wrote:
    >
    > HI everyone
    >
    > I am supposed to pass back a pointer to a struct from a function whos
    > definition cannot be changed
    >
    > flight_t * get_item(field_t field , void * data)
    >
    > the problem I encounter is whe I pass back the address of a local var
    > it gives me garbled output at the calling end ( obv !! )
    >
    > so I make a new pointer to the struct 'f'
    >
    > f = (flight_t *)malloc(sizeof(ret)); // malloc the amount of space
    > memcpy(f,&ret,REC_SIZE);
    > // then attempt to copy the mem chunk ( ret is an actual struct )
    > return f; // and return a pointer to it
    >
    > unfortunately this too also doesnt work ... pls help!!


    #include <stdlib.h>
    #include <stdio.h>

    flight_t *f;

    f = malloc(sizeof *f);
    if (f != NULL) {
    *f = ret;
    } else {
    puts("malloc problems");
    }
    return f;

    --
    pete
    pete, Oct 1, 2005
    #2
    1. Advertising

  3. writes:
    > I am supposed to pass back a pointer to a struct from a function whos
    > definition cannot be changed
    >
    > flight_t * get_item(field_t field , void * data)
    >
    > the problem I encounter is whe I pass back the address of a local var
    > it gives me garbled output at the calling end ( obv !! )
    >
    > so I make a new pointer to the struct 'f'
    >
    > f = (flight_t *)malloc(sizeof(ret)); // malloc the amount of space
    > memcpy(f,&ret,REC_SIZE);
    > // then attempt to copy the mem chunk ( ret is an actual struct )
    > return f; // and return a pointer to it
    >
    > unfortunately this too also doesnt work ... pls help!!


    So you have something like this:

    typedef struct {
    ...
    } flight_t;

    and within your function you have;

    flight_t ret;
    flight_t *f;

    (I'm largely guessing here based on the snippets of code you've
    posted. If you had posted your actual code, I wouldn't have to
    guess.)

    Your malloc() call would be better written as:

    f = malloc(sizeof *f);

    The cast is unnecessary and can mask the error of forgetting the
    required "#include <stdlib.h>".

    You need to check the result of the malloc() call; if the result is
    NULL, that means the system was unable to allocate the requested
    memory. (In this case, you can probably just abort the program;
    more sophisticated error recovery is a larger topic.)

    Then you do:

    memcpy(f, &ret, REC_SIZE);

    I'm guessing (again!) that REC_SIZE is the size of your flight_t type.
    If so, just use sizeof(flight_t), or sizeof *f, or sizeof ret. And
    make sure you've remembered the "#include <string.h>" so the compiler
    can see the definition of memcpy().

    But since C allows assignment of structures, you can replace the
    memcpy() call with

    *f = ret;

    (Note that it's now up to the caller to deallocate the memory you
    allocated when it's done with it; failing this, you'll have a memory
    leak.)

    Finally, you tell is that "this too also doesnt work". Though I'm a
    pretty good guesser, this exceeds my abilities.

    You need to tell us what the code does *and* what you expect it to do.
    You should also post a small, complete, compilable program that
    exhibits the problem. Don't try to re-type the code; that's likely to
    introduce errors, and we won't be able to guess which errors are in
    your original code and which ones are transcription errors.
    Copy-and-paste the actual code you fed to the compiler, or read it
    into your newsreader, or whatever method your system supports.

    We don't care much about small spelling and grammatical errors, but
    needless abbreviations (like "obv" for "obviously" and "pls" for
    "please") just make your text more difficult to read.

    And since I see you're posting through groups.google.com, I'll offer
    some preemptive advice. When you post a followup, you need to provide
    enough context from the parent article so your followup makes sense on
    its own. Articles can arrive out of order, if at all, so not everyone
    is going to be able to see the parent article. Google makes posting
    proper followups gratuitously difficult, but possible:

    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Oct 1, 2005
    #3
  4. wrote on 01/10/05 :
    > HI everyone
    >
    > I am supposed to pass back a pointer to a struct from a function whos


    'pass back' ? Do you meant 'return' ?

    > definition cannot be changed
    >
    > flight_t * get_item(field_t field , void * data)
    >
    > the problem I encounter is whe I pass back the address of a local var
    > it gives me garbled output at the calling end ( obv !! )


    Sure. A well known undefined behaviour.

    > so I make a new pointer to the struct 'f'


    No. You allocate a new bloc. The pointer itself is just another
    automatic variable.

    > f = (flight_t *)malloc(sizeof(ret)); // malloc the amount of space
    > memcpy(f,&ret,REC_SIZE);


    Sounds weird... What about:

    flight_t *f = malloc (sizeof *f);

    if (f != NULL)
    {
    *f = ret;
    }

    return f;

    > // then attempt to copy the mem chunk ( ret is an actual struct )


    Done already. What do you meant ?

    > return f; // and return a pointer to it
    >
    > unfortunately this too also doesnt work ... pls help!!


    Please post a complete code. It's all unclear...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
    Emmanuel Delahaye, Oct 1, 2005
    #4
    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. John Edwards
    Replies:
    1
    Views:
    569
    Dave Rahardja
    Aug 7, 2003
  2. spoc
    Replies:
    10
    Views:
    5,087
    Rich Grise
    Sep 7, 2004
  3. gsyoon
    Replies:
    4
    Views:
    381
    gsyoon
    Aug 11, 2005
  4. Marcel Müller

    Re: Using memcpy() on an object

    Marcel Müller, Apr 27, 2010, in forum: C++
    Replies:
    9
    Views:
    1,147
    Thomas J. Gritzan
    Apr 28, 2010
  5. Default User

    Re: Using memcpy() on an object

    Default User, Apr 27, 2010, in forum: C++
    Replies:
    4
    Views:
    863
    Default User
    Apr 28, 2010
Loading...

Share This Page