Segmentation Error Help!

This is a discussion on Segmentation Error Help! within the C++ Programming forums, part of the General Programming Boards category; So I have an array, that works up to about int array_size = 410000; and then anything higher than that ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    13

    Segmentation Error Help!

    So I have an array, that works up to about int array_size = 410000; and then anything higher than that it says segmentation error. I want to be able to use array_size = 1470000; Help!!! (YES, it compiles fine.)

    Here is where I use it:

    Code:
    int main ( int argc, char *argv[] )
    {
    
    
      char *filename_hdr, *filename_ascii_dat,*filename_binary_dat, *filename_ascii_write, * pEnd;
      string line, line1, dum1[6], string_value;
      ifstream is; //this will use the lvis binary data 
      Header laurentides;
      int array_size = 410000;
      int lonpix [array_size], latpix[array_size], i = 0, length, end_data; 
      float  value[array_size];   
      long offset;
      double datastore[(array_size+1)][1]; 
      
       
     filename_hdr = argv[1];
     filename_ascii_dat = argv[2];
     filename_binary_dat = argv[3];
     filename_ascii_write = argv[4];
    
    laurentides.getHdrFile(filename_hdr);
    
     end_data = array_size+ 1;
    /*
    .
    .
    .
    .
    .
    .other lines of code not important...
    .
    .
    .
    .
    .
    */
    
      ofstream asciiwrite(filename_ascii_write, ios::out);
      
    
     //while loop to extract data from .txt file and calculate pixel degrees
      while(i < end_data  && getline(datain, line1))
        {
    	  
          istringstream datstream(line1, istringstream::in);
         
          datstream >> dum1[0] >> dum1[1] >> dum1[2] >> dum1[3] >> dum1[4] >> dum1[5] ;
          datastore[i][0] = strtod(dum1[0].c_str(), &pEnd); //lat 
          datastore[i][1] = strtod(dum1[1].c_str(), &pEnd); //long
    	 
    
    	 
          latpix[i] = (laurentides.lat-datastore[i][0])/(laurentides.x_pixsize);
          lonpix[i] = (datastore[i][1]- laurentides.lon)/(laurentides.y_pixsize );
    	
    	  
    
          //offset here calculates the  number of pixels that the data occupies in the actual binary file and points to it 
          offset = latpix[i]*laurentides.numsamples+lonpix[i];
          
          //here were getting the size of the data in memory
          offset = offset*sizeof(long); //float
          
          // cout<<"reading file at "<< offset <<endl;
    
          is.seekg (offset, ios::beg);   
          is.read ((char *) &value[i], sizeof(float)); //double
         
          //cout <<"THIS IS FSEEK VALUE: " <<  value[i] << endl;
            
          asciiwrite << dum1[0] << "     "
                     << dum1[1] << "     " 
    		 << dum1[2] << "     " 
    		 << dum1[3] << "     "  
    		 << dum1[4] << "     " 
    		 << dum1[5] << "     " 
    		 << value[i] << endl;
        
           i++;
    	
        }
    
    
    
      is.close();
      asciiwrite.close();
      return 0;
    
     
    
    }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    How about a long instead of an int ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    Code:
    int array_size = 410000;

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    You should use a vector instead. Then you don't even have to specify the limit, you can just push_back more and more values as your program read in more and more data. A vector doesn't store the data on the stack which means it wont run into the stack size limit that you are running into.
    If you cant use that, then you will need to use dynamic memory allocation through new and delete.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    K, I tried turning it in to a long,using a vector, and allocating memory and nothing worked

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    What I dont understnd is why it works up until about 410,000 and then won't work after that and says "segmentation fault". I just don't get it.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    It appears that you're defining your array size to be 410,000 - so when you go past this value, you're writing into memory that you don't own. Hence, a segmentation fault.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Yes, but I only posted that there as reference lol. I do actually change it to array_size= 1466861 in my code and that's when I get segmentation fault and I don't know whyyyyyyy.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,244
    Sorry, I misunderstood.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Code:
    is.read ((char *) &value[i], sizeof(float)); //double
    Can someone explain how the above is safe to an C programmer?
    Using a float to store chars just seems wrong.

    And, I missed the line of code opening "is"; does it default to something?

    Tim S.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    13
    Hello Tim,

    Yeah "is" came from a call to ifstream as "ifstream is;" and I used that to open a file of mine. As for the is.read, no idea. I jsut looked up the function to do what I needed it to do and it did it! So, at this point it's prob not the safest but i'm a novice so hopefully time will be a better teacher. For now, have to get this work done! ahhh.

    Also, for my problem and anyone who looks at this, I was told that the limit to my array size could actually be the computer itself and something called its "stack memory."

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Yes, I also suspect you're hitting the stack limit.
    Using your example:

    Code:
     int array_size = 410000;
      int lonpix [array_size], latpix[array_size], i = 0, length, end_data; 
      float  value[array_size];   
      long offset;
      double datastore[(array_size+1)][1];
    With your example of 410000, you're trying to allocate 2870000 bytes, that's around 2.5MB, on the stack.

    You said you'd tried allocating memory, how?
    "new" in C++ and "malloc" in C are used to allocate memory on the heap. You should be able to use that:

    Code:
      char *filename_hdr, *filename_ascii_dat,*filename_binary_dat, *filename_ascii_write, * pEnd;
      string line, line1, dum1[6], string_value;
      ifstream is; //this will use the lvis binary data 
      Header laurentides;
      int* lonpix = new int[array_size];
      int* latpix = new int[array_size];
      int i = 0, length, end_data; 
      float*  value = new float[array_size];    
      long offset;
      double (*datastore)[1] = new double[(array_size+1)][1];
    However segmentation faults can be caused by any kind of access violation, so it might not be this. You can confirm by using a debugger or putting a "cout << "reached here" statement at the beginning of the function. If it's never reached (the segfault happens on function entry) then it's very likely the stack. If it's happening somewhere else, probably not the stack and maybe overrunning array bounds as suggested.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,456
    If you do use new, you have to accompany that with delete. Hence, it's better to use std::vector if you can.
    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.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by Blah937 View Post
    I was told that the limit to my array size could actually be the computer itself and something called its "stack memory."
    It's not a limit of the computer itself, it's a limit that your project settings is imposing. You can change this limit but that is generally the wrong solution, which is why you were given other solutions.

    K, I tried turning it in to a long,using a vector, and allocating memory and nothing worked
    That just means you don't know what you're doing. We cant see how you're messing up without seeing some code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Error
    By david.jones in forum C Programming
    Replies: 16
    Last Post: 04-26-2011, 03:07 PM
  2. Segmentation Error
    By Native in forum C Programming
    Replies: 2
    Last Post: 10-26-2010, 08:41 AM
  3. I get segmentation error
    By iLike in forum C Programming
    Replies: 11
    Last Post: 11-01-2009, 12:38 PM
  4. segmentation error
    By akono in forum C Programming
    Replies: 3
    Last Post: 04-26-2009, 07:29 AM
  5. Segmentation Error / Bus Error in ANSI
    By drag0n69 in forum C Programming
    Replies: 10
    Last Post: 02-05-2008, 08:45 AM

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