Basically I'm trying to do here is put the uint32_t seq_num into the
first 4 bytes of the buf. But every time I do this, it wouldn't get
copied in to the buf at all. Can anyone point out the problem. Thanks a
lot.
char buf[packet_size];
uint32_t seq_num = 0;
uint32_t packet_num = 0;
while((nread = read(fd, buf+8, packet_size-8)) 0) {
packet_num++;
seq_num = htonl(packet_nu m); /* htonl: used to convert the
actual packet_num into network numbers*/
memcpy(buf, &seq_num, 4);
.....
.....
} 6 4453
In article <11************ *********@m73g2 000cwd.googlegr oups.com>,
danu <da****@gmail.c omwrote:
>Basically I'm trying to do here is put the uint32_t seq_num into the first 4 bytes of the buf. But every time I do this, it wouldn't get copied in to the buf at all. Can anyone point out the problem. Thanks a lot.
>char buf[packet_size]; uint32_t seq_num = 0;
uint32_t packet_num = 0;
while((nread = read(fd, buf+8, packet_size-8)) 0) {
packet_num++;
seq_num = htonl(packet_nu m); /* htonl: used to convert the actual packet_num into network numbers*/
htonl() takes as its argument an unsigned long, which might not be
the same size as uint32_t.
memcpy(buf, &seq_num, 4);
uint32_t will be 32 bits if a 32 bit unsigned type exists (and should be
an error otherwise I believe), but a 32 bit unsigned type is not
necessarily 4 bytes long. Suppose for example you are on a DSP in
which char is 32 bits; sizeof(char) is promised to be 1 by the standard,
so on such a system, memcpy() of 4 bytes would be copying 4*32 bits.
>.... .... }
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Thanks Walter.
But in my man pages: uint32_t htonl(uint32_t hostlong);
anyways, this shouldn't be the problem. All I want to do is store an
unsigned integer into the first 4 bytes of a char array:
int main () {
char buf[512];
unsigned int temp;
temp = 25115555;
memcpy(buf, &temp, 4) ;
}
shouldn't this memcpy statement suppose to stick that temp value in to
the first 4 bytes of the buf?
anyhelp would be appreciated.
thanks.
Walter Roberson wrote:
In article <11************ *********@m73g2 000cwd.googlegr oups.com>,
danu <da****@gmail.c omwrote:
Basically I'm trying to do here is put the uint32_t seq_num into the
first 4 bytes of the buf. But every time I do this, it wouldn't get
copied in to the buf at all. Can anyone point out the problem. Thanks a
lot.
char buf[packet_size];
uint32_t seq_num = 0;
uint32_t packet_num = 0;
while((nread = read(fd, buf+8, packet_size-8)) 0) {
packet_num++;
seq_num = htonl(packet_nu m); /* htonl: used to convert the
actual packet_num into network numbers*/
htonl() takes as its argument an unsigned long, which might not be
the same size as uint32_t.
memcpy(buf, &seq_num, 4);
uint32_t will be 32 bits if a 32 bit unsigned type exists (and should be
an error otherwise I believe), but a 32 bit unsigned type is not
necessarily 4 bytes long. Suppose for example you are on a DSP in
which char is 32 bits; sizeof(char) is promised to be 1 by the standard,
so on such a system, memcpy() of 4 bytes would be copying 4*32 bits.
....
....
}
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
In article <11************ *********@h48g2 000cwc.googlegr oups.com>,
danu <da****@gmail.c omwrote:
>But in my man pages: uint32_t htonl(uint32_t hostlong);
Hmmm, that isn't what my man page says, but I see it isn't defined
in POSIX.1; uint32_t is what opengroup.org has documented for Unix 97.
>anyways, this shouldn't be the problem. All I want to do is store an unsigned integer into the first 4 bytes of a char array:
>int main () {
char buf[512];
unsigned int temp;
temp = 25115555;
memcpy(buf, &temp, 4) ;
}
>shouldn't this memcpy statement suppose to stick that temp value in to the first 4 bytes of the buf?
It does in my test, on a system that happens to use 32 bit int.
On a completely different system that I tried, it was easiest to
modify to unsigned char to get my test output to work:
printf( "%02X.%02X.%02X .%02X\n", buf[0], buf[1], buf[2], buf[3] );
With signed chars, the first version output
../tmp 1002./tht
FFFFFFA3.3B.7F. 01
because of the sign extension as the signed char was converted to
int (by the default argument promotions.)
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
danu wrote:
Basically I'm trying to do here is put the uint32_t seq_num into the
first 4 bytes of the buf. But every time I do this, it wouldn't get
copied in to the buf at all. Can anyone point out the problem. Thanks a
lot.
char buf[packet_size];
uint32_t seq_num = 0;
uint32_t packet_num = 0;
while((nread = read(fd, buf+8, packet_size-8)) 0) {
packet_num++;
seq_num = htonl(packet_nu m); /* htonl: used to convert the
actual packet_num into network numbers*/
memcpy(buf, &seq_num, 4);
....
....
}
Are you sure? How do you know?
(I presume packet_size is #defined to some number larger than 8)
What's your environment?
(BTW I would probably use sizeof(seq_num) rather than 4).
--
Bill Medland
danu wrote:
>
int main () {
char buf[512];
unsigned int temp;
temp = 25115555;
memcpy(buf, &temp, 4) ;
}
shouldn't this memcpy statement suppose to stick that temp value in to
the first 4 bytes of the buf?
If something along these lines doesn't work (assuming sizeof(unsigned
int) is 4) then you have a very broken compiler or environment. How are
you checking whether or not it works?
It needn't work in this particular example since there's no C-defined
way in which you could tell whether or not it had worked. The compiler
could optimize out the memcpy() in this case.
Walter Roberson wrote:
In article <11************ *********@h48g2 000cwc.googlegr oups.com>,
danu <da****@gmail.c omwrote:
But in my man pages: uint32_t htonl(uint32_t hostlong);
Hmmm, that isn't what my man page says, but I see it isn't defined
in POSIX.1; uint32_t is what opengroup.org has documented for Unix 97.
<OTIt's defined in the current POSIX standard (2004) as the OP
states. It got there from X/Open. </OT> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ronny |
last post by:
Can anyone tell me why the following code works fine using an array.
<snip>
double xVal; // array
mxArray *X = NULL; //MatLab mxArrays
|
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 work with
|
by: Case |
last post by:
#define SIZE 100
#define USE_MEMCPY
int main(void)
{
char a;
char b;
int n;
/* code 'filling' a */
|
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
---
|
by: Peter Pichler |
last post by:
A colleague encountered an interesting problem. Suppose we have a C
function like this:
void WRITE_THING(void* addr, THING t)
{
memcpy(addr, &t, sizeof t);
}
to copy a THING to any byte address (which might not be aligned for a
THING, hence not doing *(THING*)addr = t).
| |
by: ebrahimbandookwala |
last post by:
HI everyone
I am supposed to pass back a pointer to a struct from a function whos
definition cannot be changed
flight_t * get_item(field_t field , void * data)
the problem I encounter is whe I pass back the address of a local var
it gives me garbled output at the calling end ( obv !! )
|
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1) What's the difference between these 3 statements:
(i) memcpy(&b, &KoefD, n); // this works somewhere in my code
|
by: naren |
last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy()
some where i read for short strings strcpy is faster and for large
strings memcpy is faster..
in strcpy() there is a single pass over the data...but in memcpy()
there are 2 passes..one is for strlen() and another is for copying..
but how memcpy will be faster for large strings?
Can anybody please clear my doubt
|
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 pointed to by
the source pointer is volatile and not the pointer itself?
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |