473,769 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_soc k, (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_soc k, (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 1866
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_soc k, (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_soc k, (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.supernew s.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.solar is 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_soc k, (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
2946
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 portability whit different microprocessor. is it right ?? How can i perform my 4 bytes in rle decode function when i have to check first byte (length byte) to understand what i have to do with other bytes
4
17693
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 b;
14
1958
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 C code, especially design a structure, I pay attention to the order and size of member variables. Because, my boss always says that all variables should be aligned by 4byte boundary, if not a data abort will be occurred on a specific machine such...
11
3783
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. How is this adjustment/alignment done? I goolged around alot and wans't able to find how is it done, all it said was what is byte alignment and byte padding.
1
2080
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; } NSussdStruct;
12
8127
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 raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
1
3180
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 a char b char c } in a 8 byte packing the sizeof structure is 16. Could anyone explain.
20
3530
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 *)a2; // now a1=a2, a1=a2, etc.
5
3470
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 the int as:
19
4141
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
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10199
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9979
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,...
0
8861
agi2029
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...
0
6661
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
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...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
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
3
2810
bsmnconsultancy
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...

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.