Archived

This topic is now archived and is closed to further replies.

Unsuspected

memcpy() memcat()?

Recommended Posts

Unsuspected    122
Is there a memcat() version of memcpy() or something? I want to copy the combined memory of three variables into one variable (different var types). How can i do that?

Share this post


Link to post
Share on other sites
randomZ    163
I don''t know about a memcat() function.

What kinds of variables are you trying to copy from and to? If you''re concatenating character arrays, you can use strcat(). Otherwise, I would probably copy the data of a variable at a time.


My Wonderful Web Site (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Pepe
int i;
float b;
char c;

struct Xxx {
int i;
float b;
char c;
};

Xxx x;

x.i = i;
x.b = b;
x.c = c;


If it was that simple, I wouldn''t have asked. I the vars are totally different and I need a straight memory copy.

quote:

What kinds of variables are you trying to copy from and to?


Im puting a struct in an unsigned char. (it has to be unsigned char because it is directX)

quote:
use three memcpy()''s but use pointer arithmatic(sizeof() comes to mind..) to copy them into right places

Huh? Either I am confused or you dont know what memcpy does.

To normally copy one thing to another, I could write:
memcpy(first, second, sizeof(second));

however, I want to add several things into first; that is my question.

Share this post


Link to post
Share on other sites
Unsuspected    122
quote:
Original post by Pepe
int i;
float b;
char c;

struct Xxx {
int i;
float b;
char c;
};

Xxx x;

x.i = i;
x.b = b;
x.c = c;


If it was that simple, I wouldn''t have asked. I the vars are totally different and I need a straight memory copy.

quote:

What kinds of variables are you trying to copy from and to?


Im puting a struct in an unsigned char. (it has to be unsigned char because it is directX)

quote:
use three memcpy()''s but use pointer arithmatic(sizeof() comes to mind..) to copy them into right places

Huh? Either I am confused or you dont know what memcpy does.

To normally copy one thing to another, I could write:
memcpy(first, second, sizeof(second));

however, I want to add several things into first; that is my question.

-Unsuspected

Share this post


Link to post
Share on other sites
rotos    122
If you need an unsigned char array from a struct, you might be able to take a pointer to the struct and cast it to unsigned char.


struct TheStruct a = {1, 2, 3}
struct TheStruct* b = a;
unsigned char* c = (unsigned char*)b;


This assumes that there is no padding between the various members.

Share this post


Link to post
Share on other sites
Unsuspected    122
quote:
Original post by rotos
If you need an unsigned char array from a struct, you might be able to take a pointer to the struct and cast it to unsigned char.


struct TheStruct a = {1, 2, 3}
struct TheStruct* b = a;
unsigned char* c = (unsigned char*)b;


This assumes that there is no padding between the various members.


That would only put one struct in it, though. I need to copy several structs to the unsigned char.
my unsigned char also isn''t a pointer, fyi.

-Unsuspected

Share this post


Link to post
Share on other sites
Unsuspected    122
Also, it has to be a memory copy; hence, adding the structs algebraicly (sp?) won''t give me the desired results. I need to put in the unsigned char all the structs, one after another.

Share this post


Link to post
Share on other sites
Way Walker    745
If your unsigned char isn''t a pointer, I can only think of two other things.

1) It''s just a single unsigned char, in which case you have one byte to work with, and nobody can help you

2) It''s an array, and you aren''t familiar with the way people talk about arrays in C and C++

I''ll assume the second. So you have something like


#include <string.h>

type1 object1;
type2 object2;
type3 object3;

unsigned char array[sizeof object1 + sizeof object2 + sizeof object3];

memcpy(array, object1, sizeof object1);
memcpy(array + sizeof object1, object2, sizeof object2);
memcpy(array + sizeof object1 + sizeof object2, object3, sizeof object3);


Another option might (?) be


typedef union {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType array[3];

array[0].o1 = object1;
array[1].o2 = object2;
array[2].o3 = object3;

functionYouWantToPassItTo((char *)array);


I''m also not sure why


typedef struct {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType myStuff;

myStuff.o1 = object1;
myStuff.o2 = object2;
myStuff.o3 = object3;

functionYouWantToPassItTo((char *)myStuff);


Wouldn''t work.

Share this post


Link to post
Share on other sites
Unsuspected    122
quote:
Original post by Way Walker
If your unsigned char isn''t a pointer, I can only think of two other things.

1) It''s just a single unsigned char, in which case you have one byte to work with, and nobody can help you

2) It''s an array, and you aren''t familiar with the way people talk about arrays in C and C++



Actually, it is
3) It was the morning and I wasn''t thinking straight. I meant to say that the structs were pointers.

quote:


#include <string.h>

type1 object1;
type2 object2;
type3 object3;

unsigned char array[sizeof object1 + sizeof object2 + sizeof object3];

memcpy(array, object1, sizeof object1);
memcpy(array + sizeof object1, object2, sizeof object2);
memcpy(array + sizeof object1 + sizeof object2, object3, sizeof object3);




I''m not sure how memcpy works; however, if it works the way you show, then that will be fine.

quote:

Another option might (?) be


typedef union {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType array[3];

array[0].o1 = object1;
array[1].o2 = object2;
array[2].o3 = object3;

functionYouWantToPassItTo((char *)array);


I''m also not sure why


typedef struct {
type1 o1;
type2 o2;
type3 o3;
} MyType;

type1 object1;
type2 object2;
type3 object3;

MyType myStuff;

myStuff.o1 = object1;
myStuff.o2 = object2;
myStuff.o3 = object3;

functionYouWantToPassItTo((char *)myStuff);


Wouldn''t work.


Oh, I get what you are saying. That may work; I''ll give it a try and respond when I get home.

-Unsuspected

Share this post


Link to post
Share on other sites