memcpy()

I

ishmael4

hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
} pkg;

typedef struct binary_ {
char* buffer;
int size;
} binary;
--cut here--

and i cant understand, why isnt this function working:

--cut here--
void SendBinary(binary& sent)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_pkg_data;
// tu wpisujesz swoj kod do obs³ugi binary
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent.size-((n)*max_pkg_data);
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

Especcialy:
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);

Gives me an Acces Violation Message? Why is it so? Anyone could help me?
 
D

David Resnick

ishmael4 said:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
} pkg;

typedef struct binary_ {
char* buffer;
int size;
} binary;
--cut here--

and i cant understand, why isnt this function working:

--cut here--
void SendBinary(binary& sent)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_pkg_data;
// tu wpisujesz swoj kod do obs³ugi binary
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent.size-((n)*max_pkg_data);
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

Especcialy:
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);

Gives me an Acces Violation Message? Why is it so? Anyone could help me?

What makes you think that your buffer is an exactl multiple of
max_pkg_data in size? For example, suppose that buffer is 4 bytes.
You will copy those 4 bytes, but also the next 4996 bytes, which you
ought not be reading.

-David
 
E

Eric Sosman

ishmael4 wrote On 09/22/05 12:47,:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
[...]

comp.lang.c++ is down the hall to your left, just
past the vending machines.
 
I

ishmael4

Uzytkownik "Eric Sosman said:
ishmael4 wrote On 09/22/05 12:47,:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
[...]

comp.lang.c++ is down the hall to your left, just
past the vending machines.

why do you think its c++?

ishmael4
 
I

ishmael4

Uzytkownik "David Resnick" <[email protected]> napisal w wiadomosci
ishmael4 said:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
} pkg;

typedef struct binary_ {
char* buffer;
int size;
} binary;
--cut here--

and i cant understand, why isnt this function working:

--cut here--
void SendBinary(binary& sent)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;

memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_pkg_data;
// tu wpisujesz swoj kod do obs³ugi binary
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent.size-((n)*max_pkg_data);
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

Especcialy:

memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);

Gives me an Acces Violation Message? Why is it so? Anyone could help me?
--
ishmael4

What makes you think that your buffer is an exactl multiple of
max_pkg_data in size? For example, suppose that buffer is 4 bytes.
You will copy those 4 bytes, but also the next 4996 bytes, which you
ought not be reading.

-David

for (n=0;n<sent.size/max_pkg_data;++n)
guarantees me that its larger than 5000?

ishamel4
 
D

David Resnick

ishmael4 said:
Uzytkownik "David Resnick" <[email protected]> napisal w wiadomosci
ishmael4 said:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
} pkg;

typedef struct binary_ {
char* buffer;
int size;
} binary;
--cut here--

and i cant understand, why isnt this function working:

--cut here--
void SendBinary(binary& sent)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;

memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_pkg_data;
// tu wpisujesz swoj kod do obs³ugi binary
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent.size-((n)*max_pkg_data);
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

Especcialy:

memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);

Gives me an Acces Violation Message? Why is it so? Anyone could help me?
--
ishmael4

What makes you think that your buffer is an exactl multiple of
max_pkg_data in size? For example, suppose that buffer is 4 bytes.
You will copy those 4 bytes, but also the next 4996 bytes, which you
ought not be reading.

-David

for (n=0;n<sent.size/max_pkg_data;++n)
guarantees me that its larger than 5000?

ishamel4

The one after the loop sends the "remainder". That one is the problem
I think. You only want to copy the actual number of bytes (which is
less
than 5000). By copying the full 5000, you are reading where you may
not
go.

-David
 
M

Martin Ambuhl

ishmael4 wrote what appears to be C++ (so obfuscated from the first):
hello!

i've go two structures:
[...]
Since you have posted to comp.lang.c, I suppose you have a C question.
I have provided an attempt at rewriting you code into C. Could you
check that it actually conforms to your intent, modifying it so it does,
and ask your question in that context? If you meant to actually ask
about code in the byzantine language C++, that abomination has its own
newsgroup <
[attempted rewrite]
#include <string.h>

#if 0
/* mha: The line */
const unsigned int max_pkg_data = 5000;
/* mha: does not create a constant. Since the typedef pkg outside
of a function requires a compile-time constant for the size of
'data', this has been replaced. */
#endif
#define MAX_PKG_DATA 5000

typedef struct
{
short int info;
short int size;
char data[MAX_PKG_DATA];
} pkg;

typedef struct
{
char *buffer;
int size;
} binary;

void SendBinary(binary /* mha: C++-ism '&' replaced, uses of
'sent' fixed, pointless casts
removed */ * sent)
{
unsigned int n = 0;
for (n = 0; n < (unsigned) sent->size / MAX_PKG_DATA; ++n) {
pkg nPkg;
/* mha: the following had a char (buff[n * MAX_PKG_DATA]) as the
source for the copy. Changed */
memcpy(nPkg.data, sent->buffer + (n * MAX_PKG_DATA),
MAX_PKG_DATA);
nPkg.info = 0;
nPkg.size = MAX_PKG_DATA;
#if 0
/* mha: no information about 'Form1' was supplied. Line
suppressed */
Form1->CC1->Socket->SendBuf(&nPkg, sizeof(pkg));
#endif
}
pkg nPkg;
/* mha: the following had a char (buff[n * MAX_PKG_DATA]) as the
source for the copy. Changed */
memcpy(nPkg.data, sent->buffer + (n * MAX_PKG_DATA), MAX_PKG_DATA);
nPkg.info = 0;
nPkg.size = sent->size - (n * MAX_PKG_DATA);
#if 0
/* mha: no information about 'Form1' was supplied. Line suppressed */
Form1->CC1->Socket->SendBuf(&nPkg, sizeof(pkg));
#endif

}

int main(void)
{
binary f;
SendBinary(&f);
return 0;
}


[OP's post continued]
--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
} pkg;

typedef struct binary_ {
char* buffer;
int size;
} binary;
--cut here--

and i cant understand, why isnt this function working:

--cut here--
void SendBinary(binary& sent)
{
unsigned int n=0;
for (n=0;n<sent.size/max_pkg_data;++n)
{
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_pkg_data;
// tu wpisujesz swoj kod do obs³ugi binary
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));
}
pkg nPkg;
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent.size-((n)*max_pkg_data);
Form1->CC1->Socket->SendBuf((void*)&nPkg,sizeof(pkg));

return;
}
--cut here--

Especcialy:
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);

Gives me an Acces Violation Message? Why is it so? Anyone could help me?
 
E

Eric Sosman

ishmael4 wrote On 09/22/05 13:05,:
ishmael4 wrote On 09/22/05 12:47,:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
[...]

comp.lang.c++ is down the hall to your left, just
past the vending machines.

why do you think its c++?

It isn't C, and it certainly looks like some of the
bits of C++ I've seen. You're right, though: it might
not actually be C++, but rather a snippet of code in
Ada or Babbage or ... or Yoda or Zuniga. I apologize
for jumping to a conclusion: non-C-ness does not imply
C++-ness.

The non-C-ness, however, is evident. A C compiler
object to the line just before my elision, because in
C a `const' variable does not qualify as a constant
expression. A C compiler would also object to this
subsequent line

void SendBinary(binary& sent)

.... and I admit it's this second offense that caught my
eye first, causing me to re-examine the earlier lines
more closely, which is when I spotted the first problem.

Whatever your code is, it's not C.
 
O

Old Wolf

Eric said:
It isn't C, and it certainly looks like some of the
bits of C++ I've seen.

Your instincts were correct. I'm not sure why the OP
might pretend it was not C++ ...

FWIW I think the OP's problem stems from the line:
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],
max_pkg_data);

because sent.buffer[X] is a char, and he's casting a char to a
pointer. He probably meant: (void *)&sent.buffer[n*max_pkg_data].

A good reason to avoid stupid casting.

BTW, I haven't bothered to check if the memcpy is actually doing
something valid and non-overlapping.
 
I

ishmael4

Uzytkownik "Eric Sosman said:
ishmael4 wrote On 09/22/05 13:05,:
ishmael4 wrote On 09/22/05 12:47,:

hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=5000;

typedef struct pkg_ {
short int info;
short int size;
char data[max_pkg_data];
[...]

comp.lang.c++ is down the hall to your left, just
past the vending machines.

why do you think its c++?

It isn't C, and it certainly looks like some of the
bits of C++ I've seen. You're right, though: it might
not actually be C++, but rather a snippet of code in
Ada or Babbage or ... or Yoda or Zuniga. I apologize
for jumping to a conclusion: non-C-ness does not imply
C++-ness.

The non-C-ness, however, is evident. A C compiler
object to the line just before my elision, because in
C a `const' variable does not qualify as a constant
expression. A C compiler would also object to this
subsequent line

void SendBinary(binary& sent)

... and I admit it's this second offense that caught my
eye first, causing me to re-examine the earlier lines
more closely, which is when I spotted the first problem.

Whatever your code is, it's not C.

U needn't have used so many words. "> void SendBinary(binary& sent)" as an
example is enough. Well I admit using C++, but this certain line:
"memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);"
is in pure C, isnt it? This is the major problem. So what do I do? Shall i
start a new topic?
 
I

ishmael4

Uzytkownik "Old Wolf said:
Eric said:
It isn't C, and it certainly looks like some of the
bits of C++ I've seen.

Your instincts were correct. I'm not sure why the OP
might pretend it was not C++ ...

FWIW I think the OP's problem stems from the line:
memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],
max_pkg_data);

because sent.buffer[X] is a char, and he's casting a char to a
pointer. He probably meant: (void *)&sent.buffer[n*max_pkg_data].

Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?
 
E

Eric Sosman

ishmael4 said:
U needn't have used so many words.

U, hwvr, shd use mor ltrs.
"> void SendBinary(binary& sent)" as an
example is enough. Well I admit using C++, but this certain line:
"memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);"
is in pure C, isnt it?

No, it's pure C++ because it's part of a C++ program.
Standing by itself it's nothing at all: it will be rejected
by a C compiler, by a C++ compiler, by a COBOL compiler, by
awk, perl, MCR, and COMMAND.COM. A program in C (C++, ...)
cannot be minced quite so finely and still retain its
linguistic identity: DNA is no longer DNA once you separate
the atoms from each other. Thought question: is the fragment
`X = 42' part of a C program or part of a BASIC program?
This is the major problem. So what do I do? Shall i
start a new topic?

Yes, but in comp.lang.c++. I'll grant you that there are
many similarities between C and C++ and that in many cases
both languages will give you the same answer. But in some
cases the two languages differ in important and non-obvious
ways, and an answer that's correct for C may be nonsense in
a C++ program. Since the problem has already baffled you, it
follows that you are particularly UNqualified to judge whether
the differences between the languages are involved in it.

Italian is just a variation on Latin, and so is Spanish,
right? If you're having trouble translating a tricky passage
from "Inferno," should you seek help from Marco or from Miguel?
 
O

Old Wolf

ishmael4 said:
Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?

Yes you are wrong. x[y] means *(x+y). x+y is a pointer (points
'y' chars past where 'x' is pointing). *(x+y) is what you get when
you dereference that pointer. x[y] is a nicer way of writing *(x+y).

Note that x+y means the same as (char*)x+y, since x is already a
char* .
 
M

Martin Ambuhl

ishmael4 said:
Uzytkownik "Eric Sosman" <[email protected]> napisal w wiadomosci
U needn't have used so many words. "> void SendBinary(binary& sent)" as an
example is enough. Well I admit using C++, but this certain line:
"memcpy((void*)nPkg.data,(void*)sent.buffer[n*max_pkg_data],max_pkg_data);"
is in pure C, isnt it? This is the major problem. So what do I do? Shall i
start a new topic?

Did you not check my answer in
Message-ID: <[email protected]>
Date: Thu, 22 Sep 2005 17:19:47 GMT?

Look at the way the arguments to memcpy are changed. You are
inappropriately using a char (coerced with a cast) as a (void *).

Your present message is dated
Date: Fri, 23 Sep 2005 10:56:48 +0200
You have had plenty of time even with very bad propagation conditions.

If you ignore the answers given you, why should anyone bother trying to
help you?
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top