[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Copying fragmented data to contiguous block

mojumbo

9/24/2008 2:02:00 PM

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?
6 Answers

Paavo Helde

9/24/2008 10:37:00 PM

0

mojumbo <jnbbender@gmail.com> kirjutas:

> 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

You sure that a 2x2 matrix takes 3 floats?

> aMem = new char[sizeToAlloc];
>
> memcpy((void*)aMem, &aTest, lnIncSize);
> memcpy((void*)(aMem+lnIncSize), (&aTest)+(lnIncSize),
> sizeof(float)*3);

memcpy(aMem+lnIncSize, aTest.matrix,
sizeof(float)*3);

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

Actually, your code should copy something from after the aTest object.

The name Send() suggests that the data will go outside of the program. In
this case I hope you are aware that the data serialized in this fashion
can be only reliably read back into an application compiled with the same
compiler, same compiler version, same compiler flags and packing pragmas,
and on the same hardware and OS. I would suggest to use some kind of
external format for serialization instead, either ASCII-based or a binary
format with documented field sizes, endianness and representation.

hth
Paavo




James Kanze

9/25/2008 9:03:00 AM

0

On Sep 24, 4:02 pm, mojumbo <jnbben...@gmail.com> wrote:
> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

mojumbo

9/25/2008 12:36:00 PM

0

On Sep 25, 5:03 am, James Kanze <james.ka...@gmail.com> wrote:
> On Sep 24, 4:02 pm, mojumbo <jnbben...@gmail.com> wrote:
>
> > 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++.

Agree.

> > 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.

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?

James Kanze

9/25/2008 2:01:00 PM

0

On Sep 25, 2:36 pm, mojumbo <jnbben...@gmail.com> wrote:
> On Sep 25, 5:03 am, James Kanze <james.ka...@gmail.com> wrote:
> > > 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.

> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

The Revd

2/18/2014 3:41:00 PM

0

On Tue, 18 Feb 2014 15:04:04 +0100, "Heinrich"
<Heinrich@Ruhrgasnet.de> wrote:

>
>
>"The Revd" <peeling@degenerate.Grik> schreef in bericht
>news:o8p6g9tqcgbb6s0749in3j38qgjtp1fpk0@4ax.com...
>> On Tue, 18 Feb 2014 09:38:57 +0100, "Heinrich"
>> <Heinrich@Ruhrgasnet.de> wrote:
>>
>>>Slyvia Stolz only a little over a year ago completed serving a 3 and 1/2
>>>year prison term in Germany for "holocaust denial." Her "crime" took place
>>>while, as his lawyer, she was defending another "holocaust denier" (Ernst
>>>Zundel) in court. Now 'the Jews' (yes, it's them for sure) are after her
>>>for
>>>daring to speak up once again. Her voice is too legally accurate, too
>>>reasonable, and too persuasive for their comfort. You can see and read
>>>Stolz' speech here. Please do so to educate yourself on the legal
>>>questions
>>>involved.
>>
>> They just don't make Germans like they used to innit [sic].
>> <tsk> [sic]
>
>
>correct it is a wonder that they are still number 1 in the competition for
>the gold medals

Yes, truly amazing. They should be out 'holocausting'?? instead of
skiing and skating!

The Peeler

2/18/2014 4:28:00 PM

0

On Tue, 18 Feb 2014 15:04:04 +0100, Dumb Heini, the heavily medicated Dutch
resident Nazi troll and laughing stock of sci and scj, wrote:

>>>questions
>>>involved.
>>
>> They just don't make Germans like they used to innit [sic].
>> <tsk> [sic]
>
> correct it is a wonder that they are still number 1 in the competition for
> the gold medals

It's a wonder that something as abysmally stupid as you dares to open its
stupid gob at all! LOL

--
Dumb Heini's "queens english" [sic] in action:
"tosepeoplereallyknowwheree te best spot is in london. i find that amazing
for a bunc of illiterate scum"
MID: <52a93da1$0$8531$2e0edba0@news.tweakdsl.nl>