C/C++ >> Copying Structures
Posted by Driver on 21:40:00 02-12-2002
I was just wondering what is the best way to copy data from one structure to another copy of that structure in C.
I have one table structure that has data in it, and I need to malloc more memory to insert more data, so I have to copy the data over to a temp structure, then malloc additional memory, then copy everything to the orig. structure with the new stuff added.
Posted by fsvara on 23:45:00 02-12-2002
for allocating mroe memory, you could use realloc() - or start with a big enough structure.
Posted by Driver on 23:48:00 02-12-2002
I can't use realloc (its for an assignment which says I can't) and I'm given a structure that can't hold additional data, and I'm suppose to create a temp structure and transfer it to it, while mallocing a larger chunk of memory.
Posted by fsvara on 23:50:00 02-12-2002
if you still want to copy the structure, i think you could use memcpy.. would look like

struct table *destination, *source;
memcpy(destination, source, sizeof(struct table));

edit: call it stupidity, call it cluelessness, call it a typo - i fixed an error


[ This Message was edited by: fsvara on 2002-02-13 18:00 ]
Posted by Driver on 03:23:00 02-13-2002
Thanks!
Posted by robost86 on 17:26:00 02-13-2002
To copy a structure, do like this:

struct mystruct x,y;

memcpy(&y,&x,sizeof(x));
/* This is like x = y */

for structure pointers, do like this:

struct mystruct *x,*y;

memcpy(x,y,sizeof(struct mystruct));


svara is totally wrong
Posted by robost86 on 17:28:00 02-13-2002
Also, Driver, how do you mean that you need to allocate new data? Do you have an array inside the structure?
Posted by Driver on 21:14:00 02-13-2002
Just to make things totally clear, here's my structure:
struct table
{
int rows;
int columns;
int rows_alloc;
struct row column_titles;
struct row *data; /* pointer to rows */
};
struct row
{
char column[4][20];
};

What I'm suppose to do is malloc memory for the table structure, into which stuff is put in (under the data.column). I am only allowed to malloc enough memory for the stuff, no more, no less.
Then they give me more stuff, and I'm suppose to fit it in the same slot, with the orig. stuff still there.
So what I was trying to do was create a temp structure, move the stuff into it, free the orig. struct, and re-malloc more space for it. So I want to copy the stuff from the orig. to the temp and back.
Posted by robost86 on 21:54:00 02-13-2002
I'd recommend a linked list here... What program are you trying to do?
Posted by Driver on 22:08:00 02-13-2002
The program that I'm working on is for my C programming class. Its this stupid database program, and they have their own main file for testing, we're just writing the library file.
You can look it up if you would like:
http://cis2650.cis.uoguelph.ca
Under Assignments, Assignment #2.
Posted by IbYdI on 07:29:00 02-14-2002
I want to thanx to all of you - since the question i asked in the vb section the "copymemory" function is actually the "memcpy" function. thanx again. [addsig]
Posted by justin on 03:36:00 02-19-2002
a = b usually works too...