Copying fragmented data to contiguous block

M

mojumbo

I am supposed to be utilizing a function which sends data given a
message size and a void* (Typical C solution):

Send(unsigned int MsgSize, void* Data);

The problem is my structure is fragmented since I am utilizing a
pointer to hold allocated memory. What I mean is:

struct tsBob
{
int a;
char b;
bool c;

float* matrix;
}

Obviously matrix is constructed on the fly (heap). I need to place
the structure and the matrix data in contiguous memory in order to use
the Send function.

My attempt:

char* aMem;
int lnIncSize = sizeof(tsBob);
tsBob aTest;

sizeToAlloc = sizeof(tsBob) + sizeof(float)*3; // Let's assume a 2x2
matrix
aMem = new char[sizeToAlloc];

memcpy((void*)aMem, &aTest, lnIncSize);
memcpy((void*)(aMem+lnIncSize), (&aTest)+(lnIncSize),
sizeof(float)*3);

Doesn't work. The second memcpy ALWAYS copies the address of
aTest.matrix.

ANY IDEAS?
 
J

James Kanze

I am supposed to be utilizing a function which sends data
given a message size and a void* (Typical C solution):
Send(unsigned int MsgSize, void* Data);

It's actually a reasonable solution for C++ as well, although
char* or unsigned char* for the data would be more realistic
with regards to what is expected. Both in C and in C++.
The problem is my structure is fragmented since I am utilizing
a pointer to hold allocated memory. What I mean is:
struct tsBob
{
int a;
char b;
bool c;
float* matrix;
}
Obviously matrix is constructed on the fly (heap). I need to
place the structure and the matrix data in contiguous memory
in order to use the Send function.

You probably need more than that. Presumably, the Send function
sends the data somewhere, and that somewhere expects a specific
format. You have to respect that format.
My attempt:
char* aMem;
int lnIncSize = sizeof(tsBob);
tsBob aTest;
sizeToAlloc = sizeof(tsBob) + sizeof(float)*3; // Let's assume a 2x2
matrix
aMem = new char[sizeToAlloc];
memcpy((void*)aMem, &aTest, lnIncSize);
memcpy((void*)(aMem+lnIncSize), (&aTest)+(lnIncSize),
sizeof(float)*3);
Doesn't work. The second memcpy ALWAYS copies the address of
aTest.matrix.

Already, the first one has just copied the internal bit image of
the data. Which probably won't work, due to a number of
considerations. Before going any further, you have to specify
the format which the data should have; functions like Send,
which take a void* (or a char* or an unsigned char*) require
that you pre-format the data to the desired format.
 
M

mojumbo

It's actually a reasonable solution for C++ as well, although
char* or unsigned char* for the data would be more realistic
with regards to what is expected. Both in C and in C++.
Agree.


You probably need more than that. Presumably, the Send function
sends the data somewhere, and that somewhere expects a specific
format. You have to respect that format.

I'm working in a VERY tightly closed environment and I'm guaranteed
same compiler,
OS, architecture, etc.
My attempt:
char* aMem;
int lnIncSize = sizeof(tsBob);
tsBob aTest;
sizeToAlloc = sizeof(tsBob) + sizeof(float)*3; // Let's assume a 2x2
matrix
aMem = new char[sizeToAlloc];
memcpy((void*)aMem, &aTest, lnIncSize);
memcpy((void*)(aMem+lnIncSize), (&aTest)+(lnIncSize),
sizeof(float)*3);
Doesn't work. The second memcpy ALWAYS copies the address of
aTest.matrix.

Already, the first one has just copied the internal bit image of
the data. Which probably won't work, due to a number of
considerations. Before going any further, you have to specify
the format which the data should have; functions like Send,
which take a void* (or a char* or an unsigned char*) require
that you pre-format the data to the desired format.

Pre-format the data? That is the purpose of the struct no? or maybe
I'm misunderstanding
your comment.
Also why wouldn't a copy of the internal bit image work?
 
J

James Kanze

I'm working in a VERY tightly closed environment and I'm
guaranteed same compiler, OS, architecture, etc.

Same compiler version, same compile options? You'll never
upgrade the system? (Or you can throw out the data if you do.)
My attempt:
char* aMem;
int lnIncSize = sizeof(tsBob);
tsBob aTest;
sizeToAlloc = sizeof(tsBob) + sizeof(float)*3; // Let's assume a 2x2
matrix
aMem = new char[sizeToAlloc];
memcpy((void*)aMem, &aTest, lnIncSize);
memcpy((void*)(aMem+lnIncSize), (&aTest)+(lnIncSize),
sizeof(float)*3);
Doesn't work. The second memcpy ALWAYS copies the address of
aTest.matrix.
Already, the first one has just copied the internal bit image of
the data. Which probably won't work, due to a number of
considerations. Before going any further, you have to specify
the format which the data should have; functions like Send,
which take a void* (or a char* or an unsigned char*) require
that you pre-format the data to the desired format.
Pre-format the data? That is the purpose of the struct no?

A struct doesn't have anything to do with formatting.
or maybe I'm misunderstanding your comment. Also why wouldn't
a copy of the internal bit image work?

Because it's not a defined format. It changes with the compiler
version or the options used to invoke the compiler. It will
change the day you upgrade the system.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top