Author Topic: while loop in software  (Read 64850 times)

0 Members and 1 Guest are viewing this topic.

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: while loop in software
« Reply #75 on: December 18, 2014, 09:41:23 am »
You just need to call:

 memset(myArr, 0, 2000000000*sizeof(int));

And you are done.

If the micro has a set repeat and loop instruction set it will use that, using the for loop won't give enough hints to the compiler to choose a single assembly instruction.

Look up Single Instruction Multiple Data (SIMD)

that is in its simplistic form and the dumbest example ever... we know zeroing memory using memset or other built in function... please redo again with....
Code: [Select]
int *ptr = &myArr[0], count = 2000000000;
for (int i = 0; i++; i < count) *ptr++ = (some inline very complex or cryptic mathematical formula)(i,x,y,etc);

Oh since I'm simple and dumb, you should revisit your understanding on how a for loop is constructed, since your loop wont even do one iteration :P

I guess your code is faster since it doesn't change anything in memory  :-DD
« Last Edit: December 18, 2014, 09:44:40 am by miguelvp »
 

Offline f1rmb

  • Regular Contributor
  • *
  • Posts: 180
  • Country: fr
Re: while loop in software
« Reply #76 on: December 18, 2014, 10:03:32 am »
Also, I would use size_t for "i"  ;-)
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: while loop in software
« Reply #77 on: December 18, 2014, 10:23:53 am »
Also, I would use size_t for "i"  ;-)

Someone will just come along and point out that signed-vs-unsigned is just too confusing for their terrible developers...
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline AndreasF

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: gb
    • mind-dump.net
Re: while loop in software
« Reply #78 on: December 18, 2014, 10:42:33 am »
I also think while loops are perfectly acceptable and useful, and in some languages may even be required to solve a particular problem. I got bitten by this in Matlab a while back when I foolishly assumed that a Matlab "for x=1:1:100" is equivalent to "for(int x=1; x<101, x++)". Try this to see why:

Code: [Select]
for x=1:10,
if (x>5), x=x+1; end;
disp(x);
end

my random ramblings mind-dump.net
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3231
  • Country: gb
Re: while loop in software
« Reply #79 on: December 18, 2014, 10:49:52 am »
Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.
I still don't see the need for the effing things.

If you were designing a language, and someone wants to pass a huge structure or array between functions, how would you achieve this?  Would you copy this enormous data structure onto the stack, massively slowing program execution and greatly increasing memory requirements, or would you pass a reference?  Undoubtedly you would choose the latter in nearly all circumstances.  Passing by reference is using a pointer, even though it may be done "under the hood".

A major part of the power of C is the ability to explicitly use and manipulate pointers, and they are absolutely nothing to be afraid of once you learn to use them correctly.  Suggesting C should be avoided because it uses pointers is like saying cars should be avoided because they use wheels.
« Last Edit: December 19, 2014, 09:53:36 pm by mikerj »
 

Offline f1rmb

  • Regular Contributor
  • *
  • Posts: 180
  • Country: fr
Re: while loop in software
« Reply #80 on: December 18, 2014, 11:18:34 am »
Also, I would use size_t for "i"  ;-)

Someone will just come along and point out that signed-vs-unsigned is just too confusing for their terrible developers...

I'm afraid count assignment will make the compiler unhappy ;-)
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: while loop in software
« Reply #81 on: December 18, 2014, 11:45:02 am »
Hard to say these days what the result of any C statement will be, even a simple for loop may well be optimised.

For instance, the latest Visual Studio compilers (VS2012 on) when compiling for PC will vectorise a for loop, if the count is upwards and the step size is 1.  This means I have to get out of the habit of 30 years of making for loops count down to zero to save a comparison with the terminal value.

 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4206
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: while loop in software
« Reply #82 on: December 18, 2014, 12:01:34 pm »
Would someone mind please explaining what they think should be used instead of a while{} loop, and why the alternative is better?

Surely a perfectly good, generic solution to the "problem" of the loop never terminating, is to write code of the form:

while ((condition is true) && (there is still likelihood that condition will become false at some point in the future))
{
  do stuff that affects condition;
  check that it's still reasonable to be looping;
}

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3231
  • Country: gb
Re: while loop in software
« Reply #83 on: December 18, 2014, 12:28:13 pm »
Would someone mind please explaining what they think should be used instead of a while{} loop, and why the alternative is better?

Quite.  It's as daft as suggesting people should avoid using pointers at all costs.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7673
  • Country: de
  • A qualified hobbyist ;)
Re: while loop in software
« Reply #84 on: December 18, 2014, 12:48:45 pm »
while() / do while()

There's nothing wrong with while() and some programmers consider it more elegant. Use what you think fits best. I use while() more often than for(), because the variable/counter handling is more visible to me, expecially in complex loops.

code conventions

We all should have some, not just for teams but also for one-man projects. It makes maintenance much more easy. If you work on code from someone else or some of your own old code you'll know what I mean.

pointers

I love pointers! They make a lot of things more simple, faster and efficient. A classic example is sorting data. Would you sort an array and copy the data each time or would you sort a linked list and just swap pointers? Ever used callback functions? Just another point(er) ;)
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4206
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: while loop in software
« Reply #85 on: December 18, 2014, 12:50:36 pm »
Quite.  It's as daft as suggesting people should avoid using pointers at all costs.
Please don't misunderstand; I'm not trying to make a point here, I genuinely want to learn. An ever-increasing part of my job is embedded firmware development, and I want to get it right - or at least, learn the "rules" so I can break them properly.

I like while{} loops, my code is full of them. What should I be doing instead, and why?

Offline madires

  • Super Contributor
  • ***
  • Posts: 7673
  • Country: de
  • A qualified hobbyist ;)
Re: while loop in software
« Reply #86 on: December 18, 2014, 01:07:27 pm »
Surely a perfectly good, generic solution to the "problem" of the loop never terminating, is to write code of the form:

Code: [Select]
while ((condition is true) && (there is still likelihood that condition will become false at some point in the future))
{
  do stuff that affects condition;
  check that it's still reasonable to be looping;
}

Actually, it's not harder to make a for() loop endless by mistake than a while() loop. That seems to be a prejudice. The additional condition would be something for an error tolerant high reliability code, and for() would need that too.
« Last Edit: December 18, 2014, 01:11:44 pm by madires »
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #87 on: December 18, 2014, 01:20:50 pm »
Well, as someone who wasn't force-fed pointers early in his programming career, I don't see the need for the damn things.  I understand their history and how they were used to create and access arrays before arrays became a formal structure, and so on.  I know what they do, what they don't do, and why people use them.
I still don't see the need for the effing things.

If you were designing a language, and someone wants to pass a huge structure or array between functions, how would you acheive this?  Would you copy this enormous data structure onto the heap, massively slowing program execution and greatly increasing memory requirements, or would you pass a reference?  Undoubtedly you would choose the latter in nearly all circumstances.  Passing by reference is using a pointer, even though it may be done "under the hood".

A major part of the power of C is the ability to explicitly use and manipulate pointers, and they are absolutely nothing to be afraid of once you learn to use them correctly.  Suggesting C should be avoided because it uses pointers is like saying cars should be avoided because they use wheels.
Well, hey, there's my point, except glossed over and ignored.

If passing by reference is the same as using a pointer, why use pointers?  Syntactically it is stupid and introduces yet another very common source of bugs and exploits in C programs.

Pointer syntax feels as archaic as using the horse and carriage as a primary means of transport, to me. 

Inherent bounds checking doesn't exist in C.  Is that also a good thing?  Hell no it isn't.  Heartbleed and all the hell it caused is the result of one smart programmer making a mistake that the language should not have allowed him to make.  Once he saw the error, it was fixed in literally a few seconds.

How can ANYONE argue for a language that allows such stupidity?
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6458
  • Country: nl
Re: while loop in software
« Reply #88 on: December 18, 2014, 01:23:07 pm »
Actually, it's not harder to make a for() loop endless by mistake than a while() loop. That seems to be a prejudice. 
I agree in theory you're right IMO but in practice I guess it might be easier to detect an infinite loop when the escape condition and the manner at which this escape condition will be reached is in the same statement as the for( ; ; ) does.

For instance Ex1 vs Ex2 almost no programmer I know will use Ex2 for this example.

Ex1:
for(x=0;x<5;x++)
{
  DoSomething();
}

Ex2:
x=0;
while(x<5)
{
  DoSomething();
  x++;
}

Then there is this ever ongoing discussion if one for an infinite loop should use A) or B), if I am working with a new compiler I always check the assembly if it makes a difference and if so what changes.

A) while(true){}  or while(1) {}
B) for( ; ; )
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7673
  • Country: de
  • A qualified hobbyist ;)
Re: while loop in software
« Reply #89 on: December 18, 2014, 01:52:13 pm »
Inherent bounds checking doesn't exist in C.  Is that also a good thing?  Hell no it isn't.  Heartbleed and all the hell it caused is the result of one smart programmer making a mistake that the language should not have allowed him to make.  Once he saw the error, it was fixed in literally a few seconds.

How can ANYONE argue for a language that allows such stupidity?

Following your argumentation we should drop assembler too  >:D Either you take care about those issues yourself or you choose some language doing that for you. But the compiler of a programming language doing all the sanity checking can fail too. And all programs compiled with that compiler will have the same security issue. We've seen too much examples of that so far. My question would be, why are so many programmers doing the same stupid mistakes all over again? Why don't colleges/universities teach students how to write secure code? Why do many coders think that the right language would fix all problems?
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #90 on: December 18, 2014, 02:35:21 pm »
Following your argumentation we should drop assembler too  >:D Either you take care about those issues yourself or you choose some language doing that for you. But the compiler of a programming language doing all the sanity checking can fail too. And all programs compiled with that compiler will have the same security issue. We've seen too much examples of that so far. My question would be, why are so many programmers doing the same stupid mistakes all over again? Why don't colleges/universities teach students how to write secure code? Why do many coders think that the right language would fix all problems?

If assembly was the only way to write software for the majority of microcontrollers, I would be making the exact same argument.

Assembly isn't the only way to write for micros, so I'm not saying it should be avoided.

I'm saying that being happy with C as the only non-assembly language one can use in most cases is not defensible.  I see the surge of interest in microcontroller programming lately, thanks to the Arduino and this "Internet of things" and I am happy.  I think of all those new C devs making all those same, stupid mistakes in all those devices and I am not happy.

We should be further along than this.  We have languages that don't require the use of pointers, that compile to fast machine code, we just choose not to use them on micros.  Why?

If there is one thing I have noticed about C developers, it is that they truly believe that C is the alpha and the omega in terms of true development.  There is no room for anything else, and other languages are all toys.

I'm saying that other languages have real benefits and that C is a pile of shit and should he avoided.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5313
  • Country: gb
Re: while loop in software
« Reply #91 on: December 18, 2014, 02:42:32 pm »
Would someone mind please explaining what they think should be used instead of a while{} loop, and why the alternative is better?

Quite.  It's as daft as suggesting people should avoid using pointers at all costs.

Sounds like Dartmouth BASIC, and indeed most BASICs until about 1980.
 

Offline Kjelt

  • Super Contributor
  • ***
  • Posts: 6458
  • Country: nl
Re: while loop in software
« Reply #92 on: December 18, 2014, 02:43:09 pm »
We should be further along than this.  We have languages that don't require the use of pointers, that compile to fast machine code, we just choose not to use them on micros.  Why?
What we need is a compiler that does not throw 100 irrelevant errors for forgetting a single ; in a header file.
We need a compiler that will have some extra intelligence telling the programmer he missed a ; in that header file.
We need a compiler that handles (misuse) of  pointers, and many compilers already warn about writing outside arrays if the size is known at compile time. But with dynamic sizes the compiler is not able to see what is going on.
So if we don't get smarter compilers , we sure don't need another higher level programming language slurping up an extra few k's of precious memory:
we need (more) intelligent software engineers ;)
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: while loop in software
« Reply #93 on: December 18, 2014, 02:48:38 pm »
If passing by reference is the same as using a pointer, why use pointers?  Syntactically it is stupid and introduces yet another very common source of bugs and exploits in C programs.

The style around here says to use c++ references for const pass by reference and pointers for mutables. Not sure why but I guess there is some thinking behind it.

Once you get used to a certain style it becomes a second nature and it eliminates stupid dilemmas.
 

Offline AG6QR

  • Frequent Contributor
  • **
  • Posts: 857
  • Country: us
    • AG6QR Blog
Re: while loop in software
« Reply #94 on: December 18, 2014, 05:05:27 pm »
If passing by reference is the same as using a pointer, why use pointers?  Syntactically it is stupid and introduces yet another very common source of bugs and exploits in C programs.

Only one of the uses of pointers is to pass parameters by reference.  A much more rich and essential use of pointers is to build and traverse complex data structures, like linked lists, binary trees, directed graphs, etc.

I'll grant that those types of data structures don't necessarily see a lot of use in typical embedded microcontroller software.  But for software implementing complex algorithms where performance matters, it is essential to be able to build and traverse data structures that use some notion of pointers.

Yes, it is possible to take the FORTRAN approach, where statically declared arrays are the only aggregating data type, and an array index can be used as a pointer, of sorts.  But that is at least as prone to semantic bugs as direct manipulation of pointers is, and it doesn't allow dynamic sizing of the data structures.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3231
  • Country: gb
Re: while loop in software
« Reply #95 on: December 18, 2014, 05:57:29 pm »
Quite.  It's as daft as suggesting people should avoid using pointers at all costs.
Please don't misunderstand; I'm not trying to make a point here, I genuinely want to learn. An ever-increasing part of my job is embedded firmware development, and I want to get it right - or at least, learn the "rules" so I can break them properly.

I like while{} loops, my code is full of them. What should I be doing instead, and why?

I think you misunderstood my position on this.  I'm saying that trying to avoid while() loops is as daft as trying to avoid pointers.  They are both useful tools when used correctly and there's no reason to avoid either of them.  As with any tools, improper use will inevitably lead to pain...
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11518
  • Country: my
  • reassessing directives...
Re: while loop in software
« Reply #96 on: December 18, 2014, 06:23:17 pm »
Inherent bounds checking doesn't exist in C.  Is that also a good thing?  Hell no it isn't.
why dont you tell the ieee or computing comitte or the people in power that?

Following your argumentation we should drop assembler too
and any OS built from that or from other lower level language, or the silicons and molecules to store the memory at, because they are not safe, they dont have inherent boundary check. :D

why did we even code at all in the first place? we created a solution for the problem, and then the solution created another problem for us, well that is certainly an infinite loop. how do we expect a perfect thing from an imperfect beings? GIGO. Garbage In is the "human"... ;)
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #97 on: December 18, 2014, 09:28:26 pm »
You seem to be telling me that I don't believe that coding is useful.  That isn't my stance.

My stance is that things COULD be better, but we ignore that shit and dont implement better language features because that shit isn't C and if it isn't vanilla decrepit archaic C, then it's a toy language for little babies.

I don't believe that ignoring advances in automation and language syntax clarity is a good stance to take.  We have tools that can find bugs like heartbleed in less time than it takes a human brain to fire a single neuron, yet we don't use those tools because too many C developers have so much hubris that they can't see things as they really are.
 

Offline Rigby

  • Super Contributor
  • ***
  • Posts: 1476
  • Country: us
  • Learning, very new at this. Righteous Asshole, too
Re: while loop in software
« Reply #98 on: December 18, 2014, 09:45:30 pm »
Also, I understand that the errors are all input by humans and I'm not saying that we get rid of humans.

I'm saying that things could be better, I'm not advocating for perfection.  I'm not saying "perfection or nothing," I'm saying "if someone is dead set on using C, let's assist the developer through automation, and use the computer for things that computers are very good at, like analyzing thousands of lines of code at a time looking for common mistakes, like the mistake that created heartbleed."

I'm saying let the human do things that humans are good at, and let the computer do things that computers are good at.

I want to knock the hubris out of every C developer that thinks that C is all that is needed, or is all that is useful.

 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: while loop in software
« Reply #99 on: December 18, 2014, 11:21:09 pm »
I want to knock the hubris out of every C developer that thinks that C is all that is needed, or is all that is useful.

Programming languages is after all a popularity contest. If 60% of people use C (source http://langpop.com/).

If you are a lone wolf howling at the horrors of C, then you have to ask yourself....

* Do some pretty smart people use C?
* Am I smarter than most of them?
* What do I see that others don't?
* What do others see that I don't?
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf