unsigned char buf[100] . . . unsigned int val; . . . val = *((unsigned int *)(&buf[1])); . . .
comments?
Yes, it is non-portable code. I can cause an alignment fault on some platforms.
been there, done that, and, even worse
Some compilers for 'multibyte word' processors that require alignment will not declare a fault but access the previous byte and the pointed byte instead of the pointed byte and the next. (I.e. a 16 bit processor that ignore LSB of the address for word fetch). When I complained about it to one compiler maker, I was informed "there is nothing in ANSI C about this". I never checked, what good does it do to state "there is something in ANSI C about this", if your program does not work because of such.
Then, of course, there are the endianness which, if the array is accessed both as char and int, will make a real pow-wow.
Erik (sorry abuot "multibyte word" what else would you call it)
Yes, but life isn't expected to be simple. Anything non-trivial has to be based on a number of assumptions.
We can't avoid assumptions. Just try to make good ones, and to qualify them. We can made risk assesments for a project - what if our assumptions about used hardware, used tools, available time, stability of customer requirements etc are wrong. We can document our code, specifying what assumptions we have made (or rat least realizes that we have made). We can - if the hhardware permits - perform checked builds, that contains extra integrity-testing code. We can make use of the preprocessor. We can use regression testing...
While our job is to produce working - and economical - solutions, we can't ignore assumptions. If we think that there are no assumptions involved, then we have just made a very big, and very wrong assumption.
In short: it is almost impossible to write any non-trivial applications that are guaranteed to work on any existing platform that has an ANSI/ISO-conformant compiler.
That comes as no surprise.
It is nice that you see that I have a lot of experience.
If, on the other hand, you were trying to belittle me, you must have missed "BTW the 'error' was not mine, it occurred ... in my post.
Er5ik
Unfortunately, it may rely upon false assumptions - and it might only work by pure luck!
True - But someone who builds up experience of such things can learn to more reliably determine the risk.
Just to follow on from my previous - Through experience I have determined that I don't have to put on my wellington boots before getting out of bed.
I would prefer to consider it a calculated risk.
I would not consider it wrong - I might, possibly, change my mind if I were to get my feet wet one morning ;)
True - But someone who builds up experience of such things can learn to more reliably determine the risk. there is nothing wrong with experience, if therte was, I would be up the creek re the '51 :)
Of course, were I to 'verify' my assumption that a char is 8 bits every time I type char, I would never get anywhere.
I can state my point in another way, which may be better: "when you see a bug, before anything else, verify the correctness of your assumptions"
Erik
the references to ANSI C had absolutely nothing to do with the point
The references to ANSI C had absolutely everything to do with my point, however.
Let me try to translate my statement to something a smoked sardine can understand: "it makes no sense to discontinue development because of a bug (perceived or real makes no difference) in the tools if there is a workaround"
No, that isn't a translation of what you said. It may be what you wish you had said, but it certainly doesn't reflect what you did say.
Word alignment is NOT a C issue
So, why did you complain to the compiler vendor about your problem?
Sadly you do seem to have a lot of experience of bugs which you should never have introduced into the code in the first place. Your assumption when something doesn't work as you expect is that the problem lies with the tools - this is quite typical of those who (as you admit openly, in fact you seem proud of it) haven't read the appropriate documentation.
Note that in this case the 'tool' is the 'C' language, and the 'appropriate documentation' is the definition of the language.
you must have missed "BTW the 'error' was not mine, it occurred ... in my post.
Yes, I noticed you try to salvage some credibility in a followup post.
In reality, you have to. But you don't have to do it exactly when you write the 'char' keyword. But you - at least - have to verify it when you select a new processor or a new compiler. I don't expect one of my existing compilers to change that behaviour - at least not without adding a very visible note in the release notes :)
Luckily, no chip manufacturer dares to step away från n*8-sized integer data types, because of the very bad feedback they would get from all the people who made the incorrect assumption that their programs will not fail if run on a machine with a 7-bit or 9-bit char data type. If they do add unusual data types, they add them as complements to more standard data types.
One of the first things you should do when looking for a new compiler, is to get your hands on the documentation about their take on the implementation-specific parts of the standard. You want to know if there are significant limits in # of nested if statements, number of case statements in a switch, if you will be able to nest multiple include files, if they have the usual include files (complete with expected contents) etc.
In the same way, you have to make sure if a selected processor has a general stack implementation, or if it may have a hard-coded return stack with a limited # of return etnries. You want to know if it can index data - and to what degree. You want to know if it will glow like Chernobyl or if it has reasonable power-save modes. Some details you will look up. Some details you will forget to look up. Some details you will assume to be ok, and will ignore checking up on. Checking up on everything requires perfect documentation, perfect memory and almost infinite amounts of time.
Why do you not read it all When I complained about it to one compiler maker, I was informed "there is nothing in ANSI C about this". I never checked, what good does it do to state "there is something in ANSI C about this", if your program does not work because of such.
I would claim that ANY multiword byte compiler should, at least, warn when a memory location is "typecasted up" I really do not give ahoot if it is ANSI C or not, it is dead easy for a compiler to 'see' that "a memory location is "typecasted up" and an (optional) warning would make the tool much more useful.
I care about good tools, if the toolmalker decides to hide behind ANSI C that is just too bad.
Sadly you do seem to have a lot of experience of bugs which you should never have introduced into the code in the first place. When being a member of groups, I have most often been "the debugger" thus your uttely stupid assumption that bugs I have seen were those I "have introduced into the code in the first place" is completely false.
Your assumption when something doesn't work as you expect is that the problem lies with the tools Where on earth did you get that impression? Of course in a toolmakers forum tools are the main subject of discussion, it evidently takes a smoked sardine to ASS U ME that limiting a discussion to the subject means that there are no other subjects.
But you - at least - have to verify it when you select a new processor or a new compiler. Of, course; however, I wrote "every time"
Luckily, no chip manufacturer dares to step away från n*8-sized integer data types, because of the very bad feedback they would get from all the people who made the incorrect assumption that their programs will not fail if run on a machine with a 7-bit or 9-bit char data type. If they do add unusual data types, they add them as complements to more standard data types. I would not know about chips, but Univac and (some) DEC machines use 6 bit bytes.
PS "från" slipped into your post, do you write in Swedish first or did a word just slip in ?
No two-step translation involved. However, I sometimes gets parity bit errors - especially when someone comes in and wants to talk to me in the middle of writing :)
Sw: från = Eng: from. They start the same, the same number of letters and similar pronounciation.
IBM had 36-bit big iron, so they impplemented 4x 9-bit characters or 5x 7-bit characters.
My view is that it is obvious non-portable code; but since the coder wrote it explicitly, it is intentional and is not wrong.
Intention doesn't imply correctness. You're missing the possibility that the coder may just as easily not have the slightest idea what he was doing.
Code like the OP, particularly without a comment clearly stating the assumptions it relies on and why those assumptions should hold in the case at hand, is wrong.
No, a program using implementation-specific behaviours in C is not considered invalid.
That's quite beside the point --- the code we're talking about is considerably worse than that. It doesn't just rely on implementation-defined behaviour, but rather causes undefined behaviour.
To give an explicit example: 6.1.3.4 in the language standard notes that it is undefined behaviour to make a conversion from an integer data typ to a different data type that can't handle the full numeric range. Assigning an int to a char for example.
That example is seriously flawed --- conversion from int to char is not covered by 6.1.3.4. Nor does it cause undefined behaviour. It's covered by 6.1.3.3, and the behaviour is at worst implementation-defined.
I really do not give ahoot if it is ANSI C or not
if the toolmalker decides to hide behind ANSI C that is just too bad.
ANSI 'C', now ISO 'C', is the 'C' language. In a recent thread you demonstrated that you didn't even know how a 'for' statement worked, so I guess it should come as no surprise that you can't grasp that simple fact.
whatever does you snide reply have to do with the fact that I posted "I care about good tools" which you left out in your quote.
What I said, and you decided to overlook, was that if it is possible to put out a warning for risky code (in this case "risky on the particular platdoem") the tool should do so whether it has anything to do with the C standard or not
Your other snide remark re a for. For your information I actually posted that I knew how a for loop worked, just wanted to make sure of one particular fact where the text in the standard was slightly unclear.
Why do you not just go bask to the sardine can someboduy let you out of
Nothing at all. That's why I left it out of the quote. Why do you ask?
You should only rely on a compiler to behave as specified by the standard. It is unreasonable for you to expect a compiler writer to try and anticipate every mistake someone who refuses to read the standard might make and issue a warning. Think about it: if you rely on a non-standard warning issued by a compiler you will have to read the documentation of every compiler you use to determine whether your incorrect code will work, generate a warning or simply fail. Why not just read the standard to determine whether your code is guaranteed to work and be done with it?
For your information I actually posted that I knew how a for loop worked, just wanted to make sure of one particular fact
Either you know something or you don't know something. Why would you need to 'make sure' of something you know?
where the text in the standard was slightly unclear.
Please quote this 'unclear text' from the standard. By the way, I fully expect you to be unable to do so.
I'm sure you're ego would prefer that, but sorry, you're out of luck.
How very amazing. You babble about "the standard" and what we are discussing is "a warning if some architecthure created easy to detect mistake". I have never had alignment problems, be that from typecasting or something else, but have in, at least 5 cases solved somebody elses problem by fixing alignment issues. There are legions of people that believe that a REAL C person should NEVER have even an inkling about what the hardware does, and for such (I have met many) a warning when an easily detectable architecturally dependent mistake is made, what is your opposition to asking for a warning there.
Please quote this 'unclear text' from the standard. By the way, I fully expect you to be unable to do so. sorry not to meet your expectations. the 'description' of a for states "3 is done as long as 2 is valid".
There is nothing said about "updated 3 is written back whatever 2 is"
in addition re an excerpt from you babble above whether your incorrect code will work who on earth cares if incorect code will work, incorect code IS incorrect code.
Think about it: if you rely on a non-standard warning issued by a compiler you will have to read the documentation of every compiler you use to determine whether your incorrect code will work, generate a warning or simply fail.
The question isn't if people rely on warnings. Some people do. But rely or not: We do know that they catch a number of problems. Is a compiler that helps catching problems good or bad?
People don't buy lint programs just because they are stupid or lazy. They know that they make mistakes. They know that projects can have multiple developers, and one developer may make assumptions that are not consistent with the views of the rest of the team. Not too many developers likes to scan every line checked in by all other developers, just in case they have done something stupid.
I try not to rely on my car warning if a door isn't fully closed. However, I still like that extra information. If I chose to distrust everyone, then I would always have to take a full walk around the car whenever someone have touched a door or have thrown in some extra luggage.
How very amazing. You babble about "the standard" and what we are discussing is "a warning if some architecthure created easy to detect mistake".
You misunderstand. If you write code that is guaranteed by the standard to work then you don't have to worry about or rely on the compiler to issue a warning. Simple.
I have never had alignment problems,
Ok, ok. I'll pretend to believe that all these mistakes you seem to have encountered have been made by other people. I'm sure everyone else will as well. Let's face it, you demonstrate your exemplary knowledge from the very basics of 'C' all the way up to complicated concepts like a 'for' statement with refreshing regularity.
There are legions of people that believe that a REAL C person should NEVER have even an inkling about what the hardware does,
A real 'C' person writes code that is guaranteed to work by the standard ie *code that will work on all architectures* wherever possible. Unnecessary use of knowledge of the architecture makes code non-portable, and depending on precisely what non-standard behaviour you are relying on potentially unreliable.
a warning when an easily detectable architecturally dependent mistake is made, what is your opposition to asking for a warning there.
I have no objection to the warning, and the compiler is entitled to issue one if it wishes. What I object to is your reliance on such warnings, or complaints about lack of them, rather than just doing the sensible thing and actually reading the language documentation to find out whether the code is guaranteed to work.
sorry not to meet your expectations. the 'description' of a for states "3 is done as long as 2 is valid".
Please quote the paragraph number, I'm having trouble finding that particular bit of text.
who on earth cares if incorect code will work, incorect code IS incorrect code.
That is precisely my point. You need to write correct code rather than relying on your 'suck it and see' approach.
Ok, ok. I'll pretend to believe that all these mistakes you seem to have encountered have been made by other people. Of course not. This is in response to your snide remark about "all the mistakes I have made"
A real 'C' person writes code that is guaranteed to work by the standard ie *code that will work on all architectures* wherever possible. Unnecessary use of knowledge of the architecture makes code non-portable, and depending on precisely what non-standard behaviour you are relying on potentially unreliable. The point you are totally missing is that using 'nonstandard features' e.g. DATA, XDATA is a requirement for writing efficient code, which YOU quite clearly is incapable of since to adhere to the standard and the satndard only you are forced into the LARGE memory model. I am quite certain that Keil added these things just to annoy you.
I have no objection to the warning, Then why all the words about it?
What I object to is your reliance on such warnings, or complaints about lack of them You state that I, personally rely on such warnings after I have posted that I have never had alignment problems (I know what alignment is) but have found them in other code.
I'm having trouble finding that particular bit of text. look for 'for' in the index, if you can not u8nderstand what I wrote, you do not know about it.
That is precisely my point. You need to write correct code rather than relying on your 'suck it and see' approach Mr preacher, you are preaching to the choir, nothing is farther from my coding than "it and see".
Ok, I'm going to try a different method of getting sense out of you. We'll deal with your points one at a time in separate posts:
I'm having trouble finding that particular bit of text.
look for 'for' in the index, if you can not u8nderstand what I wrote, you do not know about it.
You originally stated that there was some text in the standard that was unclear. I asked you to quote the text in question, your response was some text that does not appear in the standard. You now suggest I look up 'for' in the index - well, that takes me to the section describing the for statement, but not to any unclear text. Now please, quote the actual text that you felt was unclear and provide the paragraph number that will allow me to find it.
I may have abbreviated a bit too much, but thought that you with your self proclaimed brilliance coud figure out the abbreviateion of expression_2 to '2' etc.
One place for reference would be section 8.5 in ANSI C language summary in Kochan: "programming in ANSI C", the reference to expression_3 speak of 'evaluation'. Thus reading this with a very suspicious eye you could get that it would be within the standard to 'evaluate' expression_3 vs expression_2 without updating the content of expression_3.
Now, of course, you are going to refer to K&R, but every comparison I have made between the "ANSI C LAnguage Summary" in Kochans book has shown them the same and thus I refeer to one book instead of running sround in the library to find more sources.
Most of us refer directly to the standard, instead of any books, since this world is full of authors that only "thinks" that they know what they are writing about, and that want to rewrite the information in a more clear way - while at the same time missing a number of important details of the original, unabreviated, language.
Section from 6.8.5.3, #1: "The expression expression-3 is evaluated as a void expression after each execution of the loop body."
Since void, it never returns a value to be used. Hence, I must expect that the sole goal of evaluating expression-3 is to produce some form of side effect, sinch as updating zero or more loop variables.
6.8.6.2 shows what happens when you have a (non-nested) continue within the loop body - i.e. the loop body will be considered processed, so evaluation of expression-3 will follow.
6.8.6.3 shows what happens when you have a (non-nested) break within the loop body, i.e. the loop body will not be finished and expression-3 will not be evaluated.
does 'evaluated' mean that the result is 'permanent' even if the 'evaluation' result in that the 'evaluated' entity is not to be used any more.
I am not a compiler writer, but as I read the text the resulting code of "for (x=0; x !=8 ; x++)" could 'legally' be e.g. either of these enter ..... loop: ....... inc x cjne x,#8,loop sjmp out
or
enter: mov r4,x ........ loop: mov x,r4 ..... inc r4 cjne r4,#8,loop sjmp out
In the first case x would be 8 when the for exits, in the second case x would be 7 when the for exits.
This, Mr Smoked Sardine, is not about understanding C but about understanding English in the K&R usage.
With Keil C51 x is 8 when the for exits and I am relying on Keil not to change that, but since this is about portability, the discussion should be "could this be different in another compiler"
The result of the evaluation is always thrown avay, since it is evaluated as a void expression.
However, whenever evaluated, any side effects will always be permanent.
Speculative evaluation are outside the scope of any language specification. It is part of compiler optimizations or (more commonly) internal processor optimizations. Speculative evaluation may then be done to process several branches at the same time - but with the requirement that only the side effects of the actually taken branch should be visible to an end user.
In short: Unless you add a break or return statement in the body of the loop, your expr3 side effects will always - for any C/C++ compiler - result in expr3 side effects (the loop variable being updated) until expr2 doesn't allow any furter iterations.
Too thick fingers, or to dim mind...
"your expr3 side effects will always - for any C/C++ compiler - result in expr3 side effects (the loop variable being updated) until expr2 doesn't allow any furter iterations."
Should say "your expr3 evaluations will always - for any C/C++ compiler - result in expr3 side effects (the loop variable being updated) until expr2 doesn't allow any furter iterations."
I am not a compiler writer, but as I read the text the resulting code of "for (x=0; x !=8 ; x++)" could 'legally' be e.g. either of these
No, it couldn't.
No, it isn't.
I'm glad to see that you've finally come clean about the supposed 'unclear text' in the standard - you didn't actually read the standard at all.
When will you realise that what you happen to believe based on your 'experience' is, with monotonous regularity, wrong?
Yes, it is, mr smoked sardine. It really is a question about understanding the english.
He specifically wrote "but as I read the text the resulting code of "for (x=0; x !=8 ; x++)" could 'legally' be e.g. either of these..."
It really is a question of language since 100% of the world population are not native english speakers. The people who are not, may have a lot harder to figure out the exact meaning of a sentence with the razor-sharp precision needed to get all the intricacies of a programming language. That is something you really have to accept - if not, it is time for us to start ridiculing you for being more than a little stupid.
If you want to continue discussing for loops, long/small/high/low/... whatever, you will get more respect (whatever alias you choose to use) if you concentrate on the "why" instead of always optimizing your answers for maximum ridicule.
Please return with a full linquistic analysis of the sentences of the for-loop description of the IEC/ANSI stanard. That would be a way better method of getting someone to see the point than slamming down a "No, it couldn't" - especially since you didn't manage to pick up the full meaning of Erik's sentence. You specifically failed to detect the "as I read it", which completely changes the meaning of that sentence.
And yes, I know I'm now open for ridicule too, since I'm not a native english speaker and you will be able to find a number of errors in this message. Fine - shoot. I'm sure everyone will be impressed, and we all love a long bloody battle on this forum.
I disagree. If he had familiarised himself with the concepts of variable scope and postfix operator side-effects there would have been no room for this uncertainty in the first place. If he really, truly doesn't know what 'evaluate' means after 20 years of 'C' programming then I despair.
I assume that the 'text' referred to is your quotations from the standard? Please bear in mind the fact that he hadn't read the standard when his question first arose, so the exact terminology used in those quotes is unlikely to be the problem.
It really is a question of language since 100% of the world population are not native english speakers. The people who are not, may have a lot harder to figure out the exact meaning of a sentence with the razor-sharp precision needed to get all the intricacies of a programming language.
I find that non-technical native english speakers have great difficulty reading technical documents. I find that I cannot make head nor tail of a legal document. Marketing speak flies over my head. As an engineer, though, I can read and understand technical documents. I would expect that any engineer whose english is as good as Erik's and with such lengthy experience to be able to read and understand english technical documents. As I understand it, english is the 'default' language used for international technical documents.
And yes, Erik's english is good, when he makes an effort. His posts are often difficult to understand however for the following reasons: a) He doesn't take enough time to read and understand what he is replying to. b) He doesn't take the time to type properly at the keyboard c) He doesn't take the time to think properly about what he's going to write.
I don't concentrate on 'why' because he should be capable of reading the documentation to find out for himself. In particular, he often criticises newbies for not reading documentation, so I really don't see why he cannot follow his own advice. If I ridicule him it is a response to his arrogance.
Please return with a full linquistic analysis of the sentences of the for-loop description of the IEC/ANSI stanard.
No, I won't attempt that, as linguistics is not my field. In any case, the for statement description is not sufficient on its own to answer his dilemma, as I have already pointed out.
That would be a way better method of getting someone to see the point than slamming down a "No, it couldn't" - especially since you didn't manage to pick up the full meaning of Erik's sentence. You specifically failed to detect the "as I read it", which completely changes the meaning of that sentence.
I think I've already answered these points.
And yes, I know I'm now open for ridicule too, since I'm not a native english speaker and you will be able to find a number of errors in this message.
Not at all. In general I find your posts to be well thought out and informative. You make fewer errors in your english than many (most?) native speakers.
I'm sure everyone will be impressed, and we all love a long bloody battle on this forum.
I have no interest in any sort of battle with you. However, I take exception to Erik's hypocritical and unfounded arrogance, so I will point out his errors. If he chooses not to accept that he is wrong I will keep going in the hope that his massive ego will deflate enough to actually accept that he is. Usually this moment comes after endless fabrication and mangling of quotations, desparate attempts to change the subject being debated to something else, childish name calling etc but culminates in the repeated posting of something describing me as a 'blabbering idiot'. That's when I realise that even he has given up on the idea that he can continue trying to brazen his way out of an untenable position.
Please, will everyone here accept my profound appologies for (re-)starting the Erik versus Jack discussion.
View all questions in Keil forum