memcpy()

I

ishmael4

I dont know what to do with this function. It separates a block of binary
data into [max_pkg_data] blocks, and then deals with the rest of binary (les
than 5000 bytes).
Here we go:

--cut here--
void SendBinary(binary& sent, int status)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
nPkg.info=0;
nPkg.size=max_pkg_data;
//debug info//
String a(nPkg.data);
int i=a.Length(); //(2)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=status;
nPkg.size=sent.size-((n)*max_pkg_data);
//debug info//
String a(nPkg.data);
int i=a.Length(); // (3)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

BUT after executing (1), debug info (2) gives me size 5003. So 3 more bytes
are copied into nPkg.data. I dont know why, esspecially that after memcpy()
after the loop is executed, (3) gives me right size. And if I put:

memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3); //(1)

it (2) gives me the exact size, 4997 bytes. Why?
Please help me.
 
J

John Harrison

ishmael4 said:
I dont know what to do with this function. It separates a block of binary
data into [max_pkg_data] blocks, and then deals with the rest of binary (les
than 5000 bytes).
Here we go:

--cut here--
void SendBinary(binary& sent, int status)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
nPkg.info=0;
nPkg.size=max_pkg_data;
//debug info//
String a(nPkg.data);
int i=a.Length(); //(2)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);

This should be

memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],sent.size-((n)*max_pkg_data);
nPkg.info=status;
nPkg.size=sent.size-((n)*max_pkg_data);
//debug info//
String a(nPkg.data);
int i=a.Length(); // (3)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

BUT after executing (1), debug info (2) gives me size 5003. So 3 more bytes
are copied into nPkg.data. I dont know why, esspecially that after memcpy()
after the loop is executed, (3) gives me right size. And if I put:

memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3); //(1)

it (2) gives me the exact size, 4997 bytes. Why?
Please help me.

There is no such C++ type as String, perhaps you meant string?

string expects as its constructor parameter a null terminated sequence
of bytes. You have not given it that so any length you get from the
string is not going to be reliable, it could be longer or it could be
shorter.

Maybe String is the same as string, maybe String is a mistype for
string, who knows.

In any case the error is in your debugging code not in your real code.

john
 
I

ishmael4

Uzytkownik "John Harrison said:
ishmael4 said:
I dont know what to do with this function. It separates a block of binary
data into [max_pkg_data] blocks, and then deals with the rest of binary
(les than 5000 bytes).
Here we go:

--cut here--
void SendBinary(binary& sent, int status)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
nPkg.info=0;
nPkg.size=max_pkg_data;
//debug info//
String a(nPkg.data);
int i=a.Length(); //(2)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);

This should be

memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],sent.size-((n)*max_pkg_data);

i dont think so. if sent.size is 10023 and max_pk_data is 5000
it would copy 10023-5000=5023 bytes.
In second, 23 bytes, and so on.

sent might not be a good name for this variable, it means the binary to
send.
nPkg.info=status;
nPkg.size=sent.size-((n)*max_pkg_data);
//debug info//
String a(nPkg.data);
int i=a.Length(); // (3)
//debug info//
Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

BUT after executing (1), debug info (2) gives me size 5003. So 3 more
bytes are copied into nPkg.data. I dont know why, esspecially that after
memcpy() after the loop is executed, (3) gives me right size. And if I
put:

memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3);
//(1)

it (2) gives me the exact size, 4997 bytes. Why?
Please help me.

There is no such C++ type as String, perhaps you meant string?

string expects as its constructor parameter a null terminated sequence of
bytes. You have not given it that so any length you get from the string is
not going to be reliable, it could be longer or it could be shorter.

Maybe String is the same as string, maybe String is a mistype for string,
who knows.

AnsiString, which is used by BCB6.
In any case the error is in your debugging code not in your real code.

john

ishmael4
 
M

Mike Wahler

ishmael4 said:
AnsiString, which is used by BCB6.

I strongly suggest you use standard C++ library types (in this
case 'std::string'), then everyone knows what is what. There's
really no reason to use a nonstandard type such as 'AnsiString',
and such entities are not topical here anyway.


-Mike
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top