Bug 31287 - Infinite for loop while initializing char array
Summary: Infinite for loop while initializing char array
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 31288 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-03-20 20:58 UTC by Bryan Sauser
Modified: 2007-03-20 21:26 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Bryan Sauser 2007-03-20 20:58:33 UTC
I have a simple for for loop that initializes a char array and never stops. Here is a small sample program that simulated the problem.

#include <stdio.h>

int main(int argc, char *argv[]){

   int i=0;
   char     name_array[7][100];

   for (i=0; i <= 7; i++)
   {
      printf("Loop i: %d\n", i);
      name_array[i][0] = '\0';
   }

   return 1;
}


Here are my compile options

g++ -o testloop testloop.cpp

Example of the output

Loop i: 1
Loop i: 2
Loop i: 3
Loop i: 4
Loop i: 5
Loop i: 6
Loop i: 7
Loop i: 1
Loop i: 2
Loop i: 3
Loop i: 4
Loop i: 5
Loop i: 6
Loop i: 7
Loop i: 1
Loop i: 2
Loop i: 3
Loop i: 4
Loop i: 5
Loop i: 6
Comment 1 Andrew Pinski 2007-03-20 21:01:05 UTC
Note you are going over name_array's bounds so you are invoking undefined behavior.  7 <= 7. arrays go from 0 to last-1 in C or (0, last-1] in math notation for C arrays.
Comment 2 Bryan Sauser 2007-03-20 21:03:25 UTC
Was code given to me by Contract programmer. Released person simple mistake after reviewing.
Comment 3 Andrew Pinski 2007-03-20 21:26:51 UTC
*** Bug 31288 has been marked as a duplicate of this bug. ***