Atmel studio warning that makes no sense - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: July 03, 2014, 10:03:58 am
content: I am getting:Warning 4 comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Wparentheses]my code that produce this is:Code:if (714 < V < 723) //21.50-21.75pwm_duty(1550);

author: daveshah
author_rank: Unknown
timestamp: July 03, 2014, 10:11:36 am
content: What your code is doing isCode:if ((714 < V) < 723)What you want isCode:if ((V > 714) && (V < 723))

author: Rerouter
author_rank: Super Contributor
timestamp: July 03, 2014, 10:13:34 am
content: I agree, I've fallen into the same trap many times while learning my way around C,

author: Simon
author_rank: Unknown
timestamp: July 03, 2014, 10:19:40 am
content: Oh shit, I now have 40 statements to correct

author: gmb42
author_rank: Frequent Contributor
timestamp: July 03, 2014, 12:58:15 pm
content: Just for ease of visual review I usually write such things asCode:((LowVal < x) && (x < HiVal))

author: andyturk
author_rank: Frequent Contributor
timestamp: July 03, 2014, 02:45:57 pm
content: Quote from: Simon on July 03, 2014, 10:19:40 amYou have 40 'if' statements (like that) in your code? Ouch. Aside from the syntax issue, is there a better way to accomplish what you're trying to do? E.g., a table of hi/lo values?

author: Simon
author_rank: Unknown
timestamp: July 03, 2014, 02:47:55 pm
content: I know it's probably not elegant or fast but I need to change a PWM based on supply voltage as well as an input selection so the code first checks which input is selected and then starts looking at the supply voltage in order to set the correct pwm

author: zapta
author_rank: Super Contributor
timestamp: July 03, 2014, 02:56:54 pm
content: Quote from: gmb42 on July 03, 2014, 12:58:15 pm+1. I avoid relaying on operator precedence.Another option, since the OP has 40 similar comparisons:inline boolean isInRange(int v, int min, int max) {return (v > min) && v (v > max);}and thenif (isInRange(v, 714, 723)) {}This also semantically cleaner because v will be evaluated exactly once (e.g. in case it's a function call).

author: linux-works
author_rank: Super Contributor
timestamp: July 03, 2014, 02:59:55 pm
content: Quote from: gmb42 on July 03, 2014, 12:58:15 pmyes!pet peeve of mine: people do not use enough parens in if-statements. I love to see explicit grouping. rely on precedence? no way, I don't always remember what comes before what, but with parens, its clear as day.I encourage c programmers to use more parens like this. its always better design to be very explicit, even if just to help readers understand what you wrote and meant.

author: nctnico
author_rank: Super Contributor
timestamp: July 03, 2014, 06:58:21 pm
content: @linux-works: +1 and a hug

author: rob77
author_rank: Super Contributor
timestamp: July 03, 2014, 08:43:34 pm
content: Quote from: Simon on July 03, 2014, 10:19:40 amvim and regexps are your best friends to do that

author: Lukas
author_rank: Frequent Contributor
timestamp: July 03, 2014, 10:13:25 pm
content: Luckily, there are languages where a<b<c means ((a<b) and (b<c)). Python is one of them. I'd have speculated that MATLAB would be equally smart (considering that MATLAB is for non-programmers) , but unfortunately, they're as dumb as C. MATLAB continues to disappoint me all the the time. Don't even begin with scoping...

author: rob77
author_rank: Super Contributor
timestamp: July 03, 2014, 10:20:59 pm
content: Quote from: Lukas on July 03, 2014, 10:13:25 pmC is a low-level language... it's mean't to be a language for writing operating systems and not math formulashave you ever noticed that more complex expressions are usually available only in HIGH-level languages ? it's that way for a reason (a very good reason)while using a low-level language the programmer is supposed to decompose the problem to a level needed by the language. actually the very same apply for the high-level languages but you don't have to "dig" so deep with high-level languages

author: Bored@Work
author_rank: Super Contributor
timestamp: July 04, 2014, 05:02:47 am
content: Quote from: rob77 on July 03, 2014, 10:20:59 pmC is a high-level language. As good as any other. What you mean is a domain-specific language.About that particular expression not understood by C. Every language has "formalities" you need to know. You can now argue that one formality is essential, or you can just learn what the language you use has, move on and spend your time solving the problem you really want to solve.Since Python was mentioned. Many people argue Python's "formalities", like the indentation style, are superior to other languages. While one formality (e.g. brackets) was just replaced by another (indentation). That is breaking continuity just for being different, not for adding anything of value. Same for interpreting (a < b < c) as ((a < b) and (b < c)). As a programmer you now have to learn that particular exception, and how the expression is evaluated, e.g. the precedence when it is used in combinations with others. That is not a "higher level", that is just annoying.

author: zapta
author_rank: Super Contributor
timestamp: July 04, 2014, 05:17:06 am
content: Quote from: Bored@Work on July 04, 2014, 05:02:47 amThe two expressions have different semantic. In (a < b < c) range checking each of a, b, c, is evaluated once while in ((a < b) and (b < c)) b is evaluated twice. Examples:(a < ++b < c) vs ((a < ++b) and (++b < c))(a < readVoltage() < b) vs (a < readVoltage()) and (readVoltage() < c)

author: vvanders
author_rank: Regular Contributor
timestamp: July 04, 2014, 05:27:54 am
content: Quote from: Bored@Work on July 04, 2014, 05:02:47 amI wouldn't say that python's whitespace restriction isn't adding anything. It forces a consistent coding style which is much appreciated when you have a lot of developersa in one codebase. I can't count the number of c/c++ styles I've seen over the years.

author: rob77
author_rank: Super Contributor
timestamp: July 04, 2014, 07:07:06 am
content: Quote from: Bored@Work on July 04, 2014, 05:02:47 amwith C you can directly access memory - it's not high level for me.link to wikipedia:http://en.wikipedia.org/wiki/High-level_programming_language#Relative_meaning

author: bwat
author_rank: Frequent Contributor
timestamp: July 04, 2014, 07:33:21 am
content: Quote from: rob77 on July 04, 2014, 07:07:06 amBut you cannot directly query the status flags generated by the ALU or manipulate I/O ports so it is high-level for me (I say this as an implementor of Prolog compilers and runtimes so I know what high-level is, and I'm currently debugging microcode for a CISC CPU so I know what low-level is). It's all relative and it's all meaningless. The real question is if it meets the spec. or not. Language wars are for the small minded.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 04, 2014, 07:34:06 am
content: Quote from: rob77 on July 04, 2014, 07:07:06 amno wonder for modern C# or NET hash hash programmers to say such thing. low level language is machine code, and then there is assembly code which is one to one translation of the machine code, hence still a low level language. and then there are more human readable languages, it starts from language such as C/C++. if its a low level language, you wont have the luxury of doing nonsense things like "if (a < b < c) bracket" since its a nonsense in machine code domain or semantically ambiguous there is. and then there are more modern languages type to cope with OOP (creation) hassles?, they are managed languages, perharps thats what you are using right now. and no, you cant access memory directly in C (or any other type of languages, even assembly) in protected mode OS.

author: rob77
author_rank: Super Contributor
timestamp: July 04, 2014, 09:04:06 am
content: Quote from: Mechatrommer on July 04, 2014, 07:34:06 amand what is a pointer then ? if not a pointer to a location in your address space ? no one says you can access the physical memory (that's hidden from you by the MM) - but you can freely poke around in the address space belonging to the process (virtual address space) with pointers in C.with high level languages (python, perl, ruby... whatever) you simply can't do that.

author: gmb42
author_rank: Frequent Contributor
timestamp: July 04, 2014, 11:39:17 am
content: Quote from: rob77 on July 04, 2014, 09:04:06 amctypes?? Not entirely certain what you can get up to with ctypes but it does give some low-level (for Python) access to memory.

author: rob77
author_rank: Super Contributor
timestamp: July 04, 2014, 01:38:04 pm
content: Quote from: gmb42 on July 04, 2014, 11:39:17 am-- quote from ctypes webctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.-- end quotedoes it sound to you like python itself can access the memory ? not to me

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 04, 2014, 02:20:09 pm
content: Quote from: rob77 on July 04, 2014, 09:04:06 ampointer is a "feature" provided by the language where it has no merit to decide if a laguange is low or high imho. from one perspective, "low or high" level was meant to how you interact programmatically with the native machine code. and from one perpective, pointers or arrays or even variables are all the same, they are addressing memory location. when you are assigning a = 2 you are actually manipulating memory spacegranted with variables or arrays you cant control of where in the memory location you want to manipulate, do you think you can do that with pointers in protected mode? such as *ptr = whereeverilike ? you may try, there is reason once upon a time the blue screen was called "famous". exception to some hacks maybe, thats i call the deviation from an "ideal" or "theoritical" or "intended" implementation of the system. being able to hack from C doesnt necessarily means its low enough level. (2cnts from a person who lived the life)and with pointers in C, you dont take it for granted, you need to call/pass system/OS's memory management or API to get the address. remember "free", "malloc", "new"? what do you think are they?

author: gmb42
author_rank: Frequent Contributor
timestamp: July 04, 2014, 02:46:13 pm
content: Quote from: rob77 on July 04, 2014, 01:38:04 pmAs it can apparently pass memory into C functions that take a pointer and access same on the way out there is something going on there, not as simple as p = 0x1234; *p = 0x5678 I admit.

author: rob77
author_rank: Super Contributor
timestamp: July 04, 2014, 04:08:59 pm
content: Quote from: Mechatrommer on July 04, 2014, 02:20:09 pmblue screens, segfaults and kernel panics are exactly the consequence of possibility poking around in the address space belonging to the process... i'm not saying you can do whatever you want with pointers (actually you can , but with appropriate consequences) - i'm saying it's possible with pointers in C and therefore C is low-level.i used to write linux kernel modules (device drivers - for non linux guys) for custom hardware.... you don't have to explain me anythingfrom my point of view (and it's not only me, there is lot of us over the world;) ) C is considered low level because you can directly manipulate the memory within the address space belonging to the process.back in times when C was born it was considered high-level , but not now.... if C is high level, then what would you call the real high level languages ? super-high-level ?

author: zapta
author_rank: Super Contributor
timestamp: July 04, 2014, 04:55:33 pm
content: Quote from: rob77 on July 04, 2014, 04:08:59 pmThat's a very narrow litmus test. By this logic every language peek/poke methods is low level regardless of all other aspects of the language.Quote from: rob77 on July 04, 2014, 04:08:59 pmThat's a good point, high is a relative term, but you can take it to the other end, if C is low level then what is Assembly, a super low level?Personally I don't consider C to be 'low level' but 'system level', that is a high level language that is low enough for system level programming.

author: miguelvp
author_rank: Super Contributor
timestamp: July 04, 2014, 06:07:26 pm
content: I know people frown on self modifying code, but I once wrote a runtime assembler that will take a user's math formula and compiled it in memory and returned a pointer to the function so it could run faster than emulating the formula.And yes, it was done in C using pointers.

author: rob77
author_rank: Super Contributor
timestamp: July 04, 2014, 06:29:03 pm
content: Quote from: zapta on July 04, 2014, 04:55:33 pmactually the "gap" between assembly and C is mush narrower than the "gap" between C and today's high level languages.Quote from: zapta on July 04, 2014, 04:55:33 pmOK. I buy this one

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 05, 2014, 01:58:36 am
content: Quote from: rob77 on July 04, 2014, 04:08:59 pmno. i told you... Ms termed it as "managed" language+1 to zapta. no one has redefined assembly or machine as "super low level" or anything thereof. but i agree, we are playing with relative term here... the lowest is machine, and next higher is assembly, the next higher is C, and then your super-high level language, or i prefer to call it "managed language" from where i stand.and from my eye, anything, C, basic, delphi, or probably phyton (i'm not phytoner i dont know) are on the same level since they compile directly into machine native code. with only different intention and syntax or coding style. C is for performance and close relation to assembly or machine logic. whereas basic, delphi or phyton are only try to become human readable languages, but still compile directly to machine code. one step higher is the managed language, things that cannot run without external engine, virtual machine or whatnot, and/or heavily embedded with OOP classes or libraries in the programming environment.but your judgement is based on memory manipulation capability, oh well, maybe you have more qualification in software engineering or computer science than i am hence i cant argue 100%http://hackaday.com/2011/03/17/writing-python-drivers-for-input-devices/http://stackoverflow.com/questions/981200/can-windows-drivers-be-written-in-python

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 05, 2014, 02:56:56 am
content: by your definition of pointer manipulation, the Visual Basic i'm using is not just partly managed language, but also system level language since i can do...Code:dim memPtr as longdim myHackDataPtr as longdim num as long'setup myHackDataPtr and nummemPtr = whereeverilikeCopyMemory(memPtr, myHackDataPtr, num)?

author: rob77
author_rank: Super Contributor
timestamp: July 05, 2014, 08:34:09 pm
content: Quote from: Mechatrommer on July 05, 2014, 01:58:36 amactually you can write a "device driver" in any language - but only user space drivers. for such a driver you still need the low-level layer (written in the "native" language of the OS) which is acting as a bridge between kernel space and your user space driver. it has nothing to do with the capabilities or suitability of the particular language (you could write such a user space driver even in shell script)

author: mikerj
author_rank: Super Contributor
timestamp: July 06, 2014, 09:51:13 am
content: Quote from: Mechatrommer on July 05, 2014, 02:56:56 amCopyMemory has nothing to do with Visual Basic, it's a Win32 API call (which is written in C).

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 06, 2014, 11:02:12 am
content: Quote from: mikerj on July 06, 2014, 09:51:13 amyou bet any languages will need to bind to those APIs when allocating, modifying and freeing memory.

author: Rigby
author_rank: Super Contributor
timestamp: July 06, 2014, 01:19:53 pm
content: For me, the border between a high level language and low level language is pretty clear: high level languages don't require the developer to manage resources directly, and low level languages do.Another way I've described it is that low level languages just do something, and high level languages either make you request permission to do something or make the request for you and hide it.My definition is probably naïve.

author: rob77
author_rank: Super Contributor
timestamp: July 06, 2014, 01:35:20 pm
content: Quote from: Rigby on July 06, 2014, 01:19:53 pmagreeShinySuperClass.new() and then don't care... garbage collector will do whatever it needs to do once it's not referenced... versus. malloc(sizeof struct) following by populating the pointers to functions...etc... and calling free() once your "object like feature" is not needed anymore

author: andyturk
author_rank: Frequent Contributor
timestamp: July 06, 2014, 04:13:59 pm
content: Quote from: Rigby on July 06, 2014, 01:19:53 pmYeah, probably.If the environment in which your C or C++ program runs usesgarbage collection, then you don't have to explicitly free memory. The garbage collector will figure it out. You still have to allocate storage/objects yourself though.

author: vvanders
author_rank: Regular Contributor
timestamp: July 06, 2014, 04:36:04 pm
content: That's just an allocation library, its still not a native feature of the language. There's also things like c++/CLI that can have GC semantics which are built into the language.

author: zapta
author_rank: Super Contributor
timestamp: July 06, 2014, 04:58:55 pm
content: Wikipedia has an entry on the topichttp://en.wikipedia.org/wiki/High-level_programming_language. My reading is that high 'levelness' is a relative term and one language can be higher or lower than other.

author: rob77
author_rank: Super Contributor
timestamp: July 06, 2014, 06:44:17 pm
content: Quote from: zapta on July 06, 2014, 04:58:55 pm== quote from that wikipedia articleToday, many programmers might refer to C as low-level, as it lacks a large runtime-system (no garbage collection, etc.), basically supports only scalar operations, and provides direct memory addressing. It, therefore, readily blends with assembly language and the machine level of CPUs and microcontrollers.== end quote"provides direct memory addressing" , "many programmers might refer to C as low-level" .... weren't you Guys fighting me because of such a statements ?(no offense) we already agreed on a term "system level" language , so no further fight

author: zapta
author_rank: Super Contributor
timestamp: July 06, 2014, 06:54:57 pm
content: Quote from: rob77 on July 06, 2014, 06:44:17 pmIt also goes in the other direction ;-)"Assembly language may itself be regarded as a higher level (but often still one-to-one if used without macros) representation of machine code, as it supports concepts such as constants and (limited) expressions, sometimes even variables, procedures, and data structures."BTW, for the people that consider C to be low level, do you consider C++ to be low level as well?

author: rob77
author_rank: Super Contributor
timestamp: July 06, 2014, 07:08:34 pm
content: Quote from: zapta on July 06, 2014, 06:54:57 pmnot at all - i would consider C++ as blend of low and high-level (still got the "low" level features of C , but provides "high" level features at the same time). so if we use the term "system level language", then C++ would be there along with C but on a "higher" level than C