Weird error when using array

This is a discussion on Weird error when using array within the C++ Programming forums, part of the General Programming Boards category; Hi, I have this piece of code: Code: #define NUMPACKETS 100 double arrivalTimeArray[NUMPACKETS]; for(int i=0; i<=NUMPACKETS; i++) { next = ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    Weird error when using array

    Hi,
    I have this piece of code:
    Code:
    #define NUMPACKETS 100
    double arrivalTimeArray[NUMPACKETS];
    for(int i=0; i<=NUMPACKETS; i++) {
    			next = expDistArrival();
    			arrivalTimeArray[i] = next + current;
    			current = arrivalTimeArray[i];
    	}
    	double test =  arrivalTimeArray[100];
    expDistArrival() just gets a random number. This works. I double checked.

    When I try to run the last line, I get the following error:
    Run-Time Check Failure #3 - The variable 'arrivalTimeArray' is being used without being defined.

    I am using MSVC++ .NET 2003. Any help would be highly appreciated. Thanks
    Amish

  2. #2
    *this
    Join Date
    Mar 2005
    Posts
    498
    well remember when you have an array of 100 items, the last item is in the 99th spot so on that subject:

    Code:
    #define NUMPACKETS 100
    double arrivalTimeArray[NUMPACKETS];
    for(int i=0; i<NUMPACKETS; i++) { // its not <= because it doesnt go up to 100
    			next = expDistArrival();
    			arrivalTimeArray[i] = next + current;
    			current = arrivalTimeArray[i];
    	}
    	// double test =  arrivalTimeArray[100]; there is no 100th spot

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Arrays starts counting at zero, not one.

    So index 1 is actually at index array[0], 2 is at array[1], ect.

    You also have the problem in your for loop of it being less than or equal to 100. So it will try and write data into the 100 index of the array, which will not work.

    If you make the for loop just less than array size, and call the last proper array index (99 in this case) it should probably might work. But I make no guarntees.

    *edit*
    Bah, as I was typing JoshR posted before me.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Damn that was one stupid mistake. Thanks a lot for the answer,
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21