Allocate memory for an Array

This is a discussion on Allocate memory for an Array within the C++ Programming forums, part of the General Programming Boards category; I am trying to run this code below after I have compiled in C++. The code do succesful when compiling. ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Allocate memory for an Array

    I am trying to run this code below after I have compiled in C++.
    The code do succesful when compiling.

    When I doubleclick the program it starts but immediately the program encounters a problem and closes again.
    What the code does is to read in values from a .txt file in arrays. As seen the arrays I am using has values like: Timearr[100000] etc...
    If I change these to 10000 instead the program works and no problem occurs.

    Could there be any idéas why this is happening ?
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    int main () 
    { 
    	std::string Datee;
    	char Comma;
    	int Timee = 0;
    	
    	std::string Datearr[100000];
    	int Timearr[100000];
    	int CountArray = 100000;
    
    	ofstream MainFile;
    	MainFile.open ("MainFile.txt");
    	ifstream stockfile ("CCE.txt");
    			
    	while	(    getline(stockfile, Datee, ',')   )           		
    	{
    		
    		stockfile >> Timee;		       
    		stockfile >> Comma;                   
    		stockfile.get();				// read in trailing newline character
    	//////////////////////////////////////////////////////////////////////		
    	// Read in File into Arrays (The Code is running now)
    	
    
    		CountArray = (CountArray - 1);	// CountDown for CountArray while all values is inserted for the arrays
    		Datee = Datearr[CountArray];
    		Timee = Timearr[CountArray];
    		
       }							// End of While Statement
    	//////////////////////////////////////////////////////////////////////		
    	
    
    		return 0;
    }			// int main braces
    Last edited by Coding; 01-05-2008 at 05:45 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The stack size that is available for your program is rather limited: just 1-2 MB.

    For large arrays, you'll need to allocate memory dynamically. Which is tricky to manage, and that's why there's the std::vector class.

    std::vector is also useful if you don't know how many items there will be in the array, as it can grow if you have more items. So you won't need to allocate 100000 of everything just in case and still be in trouble if it turns out that the "large number" is not enough.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    You're putting too much data on the stack. Use std::vector so you can use dynamic arrays depending on how much data you need.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I think this solved the problem... I will experiment with the std::vectors... Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  3. Replies: 2
    Last Post: 05-10-2005, 08:15 AM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM

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