using memcpy()

  • Thread starter ebrahimbandookwala
  • Start date
E

ebrahimbandookwala

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!!
 
P

pete

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;
 
K

Keith Thompson

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.
 
E

Emmanuel Delahaye

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++
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top