lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] Problem With dns.c Using 32-Bit Compilers


From: Jonathan Larmour
Subject: Re: [lwip-users] Problem With dns.c Using 32-Bit Compilers
Date: Fri, 29 Aug 2008 00:22:02 +0100
User-agent: Mozilla Thunderbird 1.0.8-1.1.fc3.4.legacy (X11/20060515)

Alain M. wrote:

Jonathan Larmour escreveu:

char dns_answer[10];
#define dns_answer_type  (*((u16_t*)dns_answer))


Unfortunately, the above is still not portable. That may well cause a mis-aligned access if dns_answer is not already aligned, and being char that isn't guaranteed.

I believe that for byte access it should never get an error, do you know of an exception?

Suppose an architecture requires 16-bit values to be aligned to 16-bits - this is common. dns_answer, being a char array, is only required to be 8-bit aligned, and thus can be at an odd address. Therefore you can get a misalignment processor exception on that architecture if trying to load it through a u16_t*.

Some people may never see a problem as it can depend on what came before dns_answer in memory - if it was preceded by e.g. a u16_t, then as it happens it would work out because dns_answer would end up always starting at an at least 16-bit aligned address. e.g.

u16_t x;
char dns_answer[10];

On many systems, the above would essentially mean there is never a problem.

But if someone put a single char before it, then dns_answer might be at an odd address e.g.:
char y;
char dns_answer[10];

Although even then it depends what came before y, etc.etc.

In case you may be thinking this could be a way of guaranteeing alignment, you can't make that assumption more generally - e.g. targets with small data/bss sections (.sdata/.sbss) may allocate variables of different sizes in different locations. Order of definition doesn't guarantee order in memory.

As for alignment, as it is independand of packing, as far as I know (not much in this case) arrays start aligned if that is the compiler setting.

They are only aligned according to their type. For char, that means 8-bit alignment[1].

Jifl
[1] I will conveniently ignore the bonkers systems out there with 16-bit chars (yes, they do exist and yes, this is legal in the C standard. No, lwIP would not work with them without quite a bit of work).
--
eCosCentric Limited      http://www.eCosCentric.com/     The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK.       Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["The best things in life aren't things."]------      Opinions==mine




reply via email to

[Prev in Thread] Current Thread [Next in Thread]