473,657 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using memcpy()

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,R EC_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!!

Nov 15 '05 #1
3 6557
eb************* ***@gmail.com 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,R EC_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
Nov 15 '05 #2
eb************* ***@gmail.com 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,R EC_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.c om, 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.c om, 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_Keit h) ks***@mib.org <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.
Nov 15 '05 #3
eb************* ***@gmail.com 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,R EC_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++
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
17501
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
25
2264
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
10
18206
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation it may seem odd. I am presuming this will copy all data ok but not really sure of the workings and...
5
3735
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a struct into a buffer to put it into a database (BerkeleyDB, to be specific) - what do I do to memcpy the thing back? Code Example: //struct I want to work with
35
12040
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src; char dst; memcpy( dst, src, 1 ); memcpy( NULL, src, 1 );
33
33695
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
19
78808
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
18
2692
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
2
2852
by: ssubbarayan | last post by:
Dear all, I am in the process of implementing pageup/pagedown feature in our consumer electronics project.The idea is to provide feature to the customers to that similar to viewing a single sms message in a mobile device.With in the given view area if the whole message does not fit,we need to provide the ability for users to scroll through the entire message using pageup/pagedown or key up and key down.In our case we have the whole...
9
3387
by: ssubbarayan | last post by:
Dear all, I am in the process of implementing pageup/pagedown feature in our consumer electronics project.The idea is to provide feature to the customers to that similar to viewing a single sms message in a mobile device.With in the given view area if the whole message does not fit,we need to provide the ability for users to scroll through the entire message using pageup/pagedown or key up and key down.In our case we have the whole...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.