473,326 Members | 2,108 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,326 software developers and data experts.

Byte alignment?

After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);

int main(int argc, char* argv[])
{
struct sockaddr_in sockaddr;
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?
Nov 15 '05 #1
5 1837
Jim Ward wrote:
After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);

int main(int argc, char* argv[])
{
struct sockaddr_in sockaddr;
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?


Guess: You had bad luck all the years before -- it worked even
though it did not have to. Now it's biting you. With sockaddr
declared on file scope, you get unlucky again.
Maybe struct sockaddr has stricter alignment requirements than
struct sockaddr_in -- and now, for the first time, it goes wrong.

If you want people to be able to help you, give at least the
definitions of the two struct types and the source of accept().
If you try to create a compiling minimal example, you maybe
find out by yourself what the problem is.
Good luck
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
Jim Ward wrote
(in article <11*************@corp.supernews.com>):
After we installed the latest Solaris patch,
109147-37, the following code fails with an
EFAULT error (14);


Then you would be best served to ask about it in
comp.unix.solaris probably instead of here.

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #3

Jim Ward wrote:
<snip>
but if I declare sockaddr as a global, everything works:

struct sockaddr_in sockaddr;

int main(int argc, char* argv[]) <snip> I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?


I just read about this. Allow me a quote from the K&R book.

"In the absence of explicit initialization, external and static
variables are guaranteed to be initialized to zero; automatic and
register variables have undefined (i.e., garbage) initial values." -
K&R, section 4.9.

hth.
~rick

Nov 15 '05 #4
Jim Ward wrote:
but if I declare sockaddr as a global, everything works:
Globals are 0 initialized, locals are not.
struct sockaddr_in sockaddr;

int main(int argc, char* argv[])
{
/* code snipped */
child_sock = accept(serv_sock, (struct sockaddr*) &sockaddr, &addr_size);
/* code snipped */
}

I guess the problem has something to do with
byte alignment, but why would a global be properly
aligned and a local improperly aligned?


It hasn't. You just got lucky :) The most common case is that addr_size
isn't initialized to sizeof(sockaddr_in) *before* calling accept().

Igmar
Nov 15 '05 #5
Igmar Palsenberg <ig***@jdimedia.local> wrote:
It hasn't. You just got lucky :) The most common case is that addr_size
isn't initialized to sizeof(sockaddr_in) *before* calling accept().


Yes, that was the problem! The manpage says you have to do this, but
my 1989 O'Reilly copy of "Using C on the Unix System" leaves this step
out. I need to get a new copy!

Thanks to all who answered!

Jim Ward
Nov 15 '05 #6

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

Similar topics

4
by: Marco | last post by:
Hi I have to read a binary image. Info tell me that it is a binary image without encoding but with RLE. Also i know it is 4 bytes align. What it means ?? How can i read it ?? I red it is for...
4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
14
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write...
11
by: Taran | last post by:
Hi all, I was wondering how does address alignment to x byte boundary is done. For example, if I say "adjust the requested size to be on a 4-byte boundary" or for that matter 8 byte boundary....
1
by: v.venkatesh | last post by:
Hi, For one of our design, we had introduced a local array of structures which contains the following fields : typedef struct { W_CHAR ussdService; W_CHAR ussdCommand; BOOL isTrue; }...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
1
by: Gajendra | last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing for example while using the structur struc double...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
5
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
19
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.