473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

memcpy()

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

Nov 15 '05 #1
14 3166
ishmael4 wrote:
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

Nov 15 '05 #2


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.

--
Er*********@sun.com

Nov 15 '05 #3
Uzytkownik "Eric Sosman" <er*********@sun.com> napisal w wiadomosci
news:dg**********@news1brm.Central.Sun.COM...


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.

--
Er*********@sun.com


why do you think its c++?

ishmael4
Nov 15 '05 #4

Uzytkownik "David Resnick" <ln********@gmail.com> napisal w wiadomosci
news:11**********************@g47g2000cwa.googlegr oups.com...
ishmael4 wrote:
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
Nov 15 '05 #5

ishmael4 wrote:
Uzytkownik "David Resnick" <ln********@gmail.com> napisal w wiadomosci
news:11**********************@g47g2000cwa.googlegr oups.com...
ishmael4 wrote:
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

Nov 15 '05 #6
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 <news:comp.lang.c++>

[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?

Nov 15 '05 #7


ishmael4 wrote On 09/22/05 13:05,:
Uzytkownik "Eric Sosman" <er*********@sun.com> napisal w wiadomosci
news:dg**********@news1brm.Central.Sun.COM...

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.

--
Er*********@sun.com

Nov 15 '05 #8
Eric Sosman wrote:
ishmael4 wrote:
"Eric Sosman":
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.


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.

Nov 15 '05 #9
Uzytkownik "Eric Sosman" <er*********@sun.com> napisal w wiadomosci
news:dg**********@news1brm.Central.Sun.COM...


ishmael4 wrote On 09/22/05 13:05,:
Uzytkownik "Eric Sosman" <er*********@sun.com> napisal w wiadomosci
news:dg**********@news1brm.Central.Sun.COM...

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?
--
Er*********@sun.com


--
ishmael4
Nov 15 '05 #10
Uzytkownik "Old Wolf" <ol*****@inspire.net.nz> napisal w wiadomosci
news:11*********************@g43g2000cwa.googlegro ups.com...
Eric Sosman wrote:
ishmael4 wrote:
"Eric Sosman":
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.


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

Nov 15 '05 #11
ishmael4 wrote:
Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?


(char) *(x + y)

--
pete
Nov 15 '05 #12
ishmael4 wrote:

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?

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #13
ishmael4 wrote:

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

Nov 15 '05 #14
ishmael4 wrote:
Uzytkownik "Eric Sosman" <er*********@sun.com> 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: <TI***************@newsread3.news.atl.earthlink.ne t>
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?

Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
16
by: Delali Dzirasa | last post by:
I would have a number packed with its hex representation of the integer below is some sample code of what is being done. int value = 20; //in hex it is 0x14 AddData (value); .. .. ..
6
by: Samee Zahur | last post by:
Hi all, I'm a little confused - my guess is memcpy is no longer (or perhaps never was) a standard c++ function, since it has very little type check into it - and can potentially create havoc for...
5
by: manya | last post by:
Ok, it's been a while since I've done the whole memcpy stuff with C++ and I'm having a hard time remembering everything. I hope, however, that you can help me with my problem. I memcpy a...
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
16
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include...
33
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
by: myhotline | last post by:
hi all im very confused about using memcpy and i have three questions....memcpy takes a pointer to src and a pointer to dest and copies src to destination...but im very confuzed about when to...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.