473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memcpy()

hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& sent)
{
unsigned int n=0;
for (n=0;n<sent.siz e/max_pkg_data;++ n)
{
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_p kg_data;
// tu wpisujesz swoj kod do obsługi binary
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
}
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent. size-((n)*max_pkg_da ta);
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));

return;
}
--cut here--

Especcialy:
memcpy((void*)n Pkg.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 3179
ishmael4 wrote:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& sent)
{
unsigned int n=0;
for (n=0;n<sent.siz e/max_pkg_data;++ n)
{
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_p kg_data;
// tu wpisujesz swoj kod do obsługi binary
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
}
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent. size-((n)*max_pkg_da ta);
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));

return;
}
--cut here--

Especcialy:
memcpy((void*)n Pkg.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=50 00;

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*********@su n.com> napisal w wiadomosci
news:dg******** **@news1brm.Cen tral.Sun.COM...


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

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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********@gma il.com> napisal w wiadomosci
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
ishmael4 wrote:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& sent)
{
unsigned int n=0;
for (n=0;n<sent.siz e/max_pkg_data;++ n)
{
pkg nPkg;

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

return;
}
--cut here--

Especcialy:

memcpy((void*)n Pkg.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.siz e/max_pkg_data;++ n)
guarantees me that its larger than 5000?

ishamel4
Nov 15 '05 #5

ishmael4 wrote:
Uzytkownik "David Resnick" <ln********@gma il.com> napisal w wiadomosci
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
ishmael4 wrote:
hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& sent)
{
unsigned int n=0;
for (n=0;n<sent.siz e/max_pkg_data;++ n)
{
pkg nPkg;

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

return;
}
--cut here--

Especcialy:

memcpy((void*)n Pkg.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.siz e/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(bina ry /* 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.dat a, 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(&nPk g, 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.dat a, 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(&nPk g, sizeof(pkg));
#endif

}

int main(void)
{
binary f;
SendBinary(&f);
return 0;
}
[OP's post continued]
--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& sent)
{
unsigned int n=0;
for (n=0;n<sent.siz e/max_pkg_data;++ n)
{
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=max_p kg_data;
// tu wpisujesz swoj kod do obsługi binary
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
}
pkg nPkg;
memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
nPkg.info=0;
nPkg.size=sent. size-((n)*max_pkg_da ta);
Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));

return;
}
--cut here--

Especcialy:
memcpy((void*)n Pkg.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*********@su n.com> napisal w wiadomosci
news:dg******** **@news1brm.Cen tral.Sun.COM...

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

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& 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*)n Pkg.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*********@su n.com> napisal w wiadomosci
news:dg******** **@news1brm.Cen tral.Sun.COM...


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

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

hello!

i've go two structures:

--cut here--
const unsigned int max_pkg_data=50 00;

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(bina ry& 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(bina ry& 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

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

Similar topics

13
17490
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 library function? That is, can I be sure that every c++ standard library has this function? Or is there a c++ alternative to it?
16
7639
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
7512
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 user-defined types. Now my confusion is here - a simple instantiation of the standard copy algorithm can be quite slow compared to the older memcpy...
5
3722
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 struct into a buffer to put it into a database (BerkeleyDB, to be specific) - what do I do to memcpy the thing back? Code Example: //struct I want to...
35
12018
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; char dst; memcpy( dst, src, 1 ); memcpy( NULL, src, 1 );
16
15765
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 <string.h> void *memcpy(void *dest, const void *src, size_t n);
33
33667
by: Case | last post by:
#define SIZE 100 #define USE_MEMCPY int main(void) { char a; char b; int n; /* code 'filling' a */
6
2915
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 use '&' operator while using memcpy....i have code that use '&' and the code that call memcpy without '&' like is the following same Quest1 ---
18
21483
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 memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data...
18
2675
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5354
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.