By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

How do I copy a structure using memcpy?

P: 1
I have a problem that involves the use of a structure and pointers (not allowed to use arrays). I have to add a record using pointers and memory allocation and then eventually delete records. I do not know how to use memcpy to copy the structure itself to add to the program.

I have a structure:
struct childrensBook //Structure of books using pointers instead of arrays
{
char *title; //use of pointers to store memory for title
char *author; //use of pointers to store memory for author
char *publisher; //use of pointers to store memory for publisher
int copyright;
double price;
};

int moveptr; //moveptr would be used to move to the next structure.

struct childrensBook *book1 = (struct childrensBook *) malloc(sizeof(struct childrensBook)); //Structure of book #1

book1->title = (char *)malloc(100); //allows memmory of 100 characters
book1->author = (char *)malloc(100); //allows memmory of 100 characters
book1->publisher = (char *)malloc(100); //allows memmory of 100 characters
book1->copyright = 1997;
book1->price = 7.99;

memcpy(book1->title, "We're Going on a Bear Hunt", 26);
memcpy(book1->author, "Michael Rosen", 13);
memcpy(book1->publisher, "Little Simon", 12);
book1->copyright = 1989;
book1->price = 7.99;
book1++;

Then I have an "add" function that I am trying to create where it will copy the structure. This is what I can't figure out.

static int addRecord(struct childrensBook book1, char *title, char *author, char *publisher, int copyright, double price)
{
int headptr = book1;
memcpy(book1, sizeof(struct childrensBook));


}
Sep 30 '15 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Making copies of struct variables is fraught with disaster. It looks like you are coding in C so you will miss the protections of C++ but you can code them your self.

First, write a function to copy a string.
Find out how long your string is and allocate that number of bytes +1 for the null terminator. Do not waste memory by assuming 100 bytes will be enough. There should never be any hard-coded limits in your code.

Expand|Select|Wrap|Line Numbers
  1. char* str = "Hello world!";
  2. char* newstr = 0;    
  3.  
  4. newstr = malloc(strlen(str) + 1);
  5. strcpy(newstr, str);
  6.  
Write this as a function and pass in the pointers to your struct members. You should be able to call this function for every one of your struct string members.

Second, is your destination already allocated? Because if it is and you copy a new string, the orginal string's address will be overwritten and access to that string is lost. A memory leak this would be.

So make some rules, if your member pointer is 0 then you may safely use the pointer as a destination. Otherwise, you must free the pointer before you malloc new bytes.

If you do this, then be sure to set your member pointers to 0 when you create the struct variable.

The feature can be easily added to your copy function.

Third, before you do anything in this copy function, check to see if the source pointer and the destination pointer are the same. If they are, just return. This would mean the struct is being copied onto itself and if you delete before copying, you would delete your own data. A crash will follow quickly.

Fourth, if memcpy can do all this, then use it. Otherwise, I would avoid all these mem... functions.
Sep 30 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.