Archived

This topic is now archived and is closed to further replies.

Prod

loop values and compile time

Recommended Posts

I was wondering if the values for loops are calculated at compile time. In my database program, the first thing it does is load the number of records and then loop through that number to load the information.For the loop to work correctly i have to "hard code" in the value for the number of records. For example (of my problem) :
example code:

fin >> numOfRecords;
printf("%i", numOfRecords); /*prints the correct number of records*/

for (int i = 1; i < numOfRecords; i++)
      printf("%i", i); /*never stops incrimenting*/

  
[edited by - prod on April 29, 2003 11:16:44 PM]

Share this post


Link to post
Share on other sites
If something is ''hard coded'' the value can''t change. Your value can change.

I can''t see how your code is incorrect. It also works fine on my machine (if that''s anything to go by!). I just changed fin to cin.

Share this post


Link to post
Share on other sites
numOfRecords might be a bad value due to either the file being loaded incorrectly or corrupt. In that way it would appear to loop forever, when in reality it's going to a really high limit. Output is extremely slow, take away the printf and it should run to the limit rather quickly, assuming the loop isn't optimized away and my theory was correct

[edited by - Zipster on April 30, 2003 4:02:26 AM]

Share this post


Link to post
Share on other sites
I think the OP is confusing macro loop limits with variable loop limits.

#define MAX_STRING_LENGTH 100

for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char

is different from

unsigned int MAX_STRING_LENGTH = 100;

for(int i = 0; i < MAX_STRING_LENGTH; ++i);//do something for each char

The first one is compile time (before compile time, actually). The second is at runtime.

Share this post


Link to post
Share on other sites