8 Replies - 194 Views - Last Post: 03 August 2012 - 08:03 PM Rate Topic: -----

#1 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Error With a Program

Posted 03 August 2012 - 06:13 PM

The Program isn't complete yet, however when I ran it to test it, I received an error on the char filename[80] located within the Diatom() Function.
the error read EXC_BAD_ACCESS (code = 1, address...), can anyone help me figure out this error.




#include <iostream>
#include <fstream>
using namespace std;
int menu();
int Diatom();
int Phytoplankon();

int main()
{
   
    
    
    bool Exit = false;
    for (;;)/>
    {
        int choice = menu();
        switch(choice)
        {
            case (1):
                Diatom();
                break;
            case (2):
                Phytoplankon();
                break;
            case (3):
                Exit = true;
                break;
            case (4):
                menu();
                break;
                
            default:
                cout << "Please Selete again!" << endl;
                
        } // end switch
        if (Exit == true)
            break;
    } // end
    return 0;
} // end main

    
int menu()
{
    
    int choice;
    
    cout << "Welcome The Algae Information Recoder Program " << endl;
    cout << "(1) Enter Information for a Diatom " << endl;
    cout << "(2) Enter Information for a Phytoplankton " << endl;
    cout << "(3) Quit Program " << endl;
    cout << "(4) Redisplay Menu " << endl;
    cin >> choice;
    return choice;
    
}

int Diatom()
{
    
    char filename[80];
    cout << "Enter File Name: ";
    cin.get(filename, 79);
    ofstream fout(filename);
    
    unsigned long int const MaxLength = 4294967295;
    char Species[MaxLength];
    char Locationfound[MaxLength];
    char Desciption[MaxLength];
     //open for writing
    cout << "Enter The Taxanomic Name For The Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Species, MaxLength);
    fout << Species << "\n";
    cout << "Enter The Location That The Species Was Found: ";
    cin.ignore(1,'\n');
    cin.getline(Locationfound, MaxLength);
    fout << Locationfound;
    cout << "Add a Description Regarding the Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Desciption, MaxLength);
    fout << Desciption;
    fout.close();
    
    return menu();
    
}

int Phytoplankon()
{
    unsigned long int MaxLength = 4294967295;
    char Species[MaxLength];
    char Locationfound[MaxLength];
    char Desciption[MaxLength];
    char filename[80];
    
    
    cout << "Enter File Name: ";
    cin.get(filename, 79);
    
    ofstream fout(filename); //open for writing
    cout << "Enter The Taxanomic Name For The Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Species, MaxLength);
    fout << Species << "\n";
    cout << "Enter The Location That The Species Was Found: ";
    cin.ignore(1,'\n');
    cin.getline(Locationfound, MaxLength);
    fout << Locationfound;
    cout << "Add a Description Regarding the Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Desciption, MaxLength);
    fout << Desciption;
    fout.close();
    
    return menu();
}



Correction the error is pointing to line 62
cout << "Enter File Name: ";

Is This A Good Question/Topic? 0
  • +

Replies To: Error With a Program

#2 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Re: Error With a Program

Posted 03 August 2012 - 06:48 PM

I've been doing some research and it seems to me that the error is according because i'm over loading the stack, how would i put this on the heap?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1900
  • View blog
  • Posts: 5,697
  • Joined: 05-May 12

Re: Error With a Program

Posted 03 August 2012 - 07:02 PM

Yes you are overloading the stack, see lines 66-69, 91-94.

Quickest solution is simply to use std::string:
std::string Species;



The string class will take care of allocating memory on the heap as needed.

Note: An array of 4294967295 chars is 4294967295 bytes -> which is about 4 GB. And you are allocating 3 of these on the stack to try to get 12 GB. If I remember correctly Windows is the more generous one for giving you 1MB of stack space, and Linux is pretty stingy at 8K.

But if you really want to allocate space from the heap yourself, you can use the new [] operator allocate space. Remember to call the delete [] operator to free up the space.
Was This Post Helpful? 1
  • +
  • -

#4 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Re: Error With a Program

Posted 03 August 2012 - 07:27 PM

View PostSkydiver, on 03 August 2012 - 07:02 PM, said:

Yes you are overloading the stack, see lines 66-69, 91-94.

Quickest solution is simply to use std::string:
std::string Species;



The string class will take care of allocating memory on the heap as needed.

Note: An array of 4294967295 chars is 4294967295 bytes -> which is about 4 GB. And you are allocating 3 of these on the stack to try to get 12 GB. If I remember correctly Windows is the more generous one for giving you 1MB of stack space, and Linux is pretty stingy at 8K.

But if you really want to allocate space from the heap yourself, you can use the new [] operator allocate space. Remember to call the delete [] operator to free up the space.


okay, I've made some changes and the program builds fine, by the way i'm using Xcode as my framework. When I run the code the menu pops up as expected and i'm prompted to enter a number. When entering 1 or 2 it should jump to the function, however it loops the menu. I don't know why, perhaps you can see a mistake I made.


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int menu();
int Diatom();
int Phytoplankon();

int main()
{
   
    
    
    bool Exit = false;
    for (;;)/>
    {
        int choice = menu();
        switch(choice)
        {
            case (1):
                Diatom();
                break;
            case (2):
                Phytoplankon();
                break;
            case (3):
                Exit = true;
                break;
            case (4):
                menu();
                break;
                
            default:
                cout << "Please Selete again!" << endl;
                
        } // end of switch
        if (Exit == true)
            break;
    } // end of loop
    return 0;
} // end of main

//****************************************************************//
//****************************************************************//
int menu()
{
    
    int choice;
    
    cout << "Welcome The Algae Information Recoder Program " << endl;
    cout << "(1) Enter Information for a Diatom " << endl;
    cout << "(2) Enter Information for a Phytoplankton " << endl;
    cout << "(3) Quit Program " << endl;
    cout << "(4) Redisplay Menu " << endl;
    cin >> choice;
    return choice;
    
}
//********************************************************************//
//********************************************************************//
// Diatom Defination
int Diatom()
{
 
    
    int *pmaxfile = new int [80];
    
    char filename[*pmaxfile];
    cout << "Enter File Name: ";
    cin.get(filename, *pmaxfile);
    ofstream fout(filename);    //open for writing
    
    
    unsigned long int *pMaxLength = new unsigned long int [3000]; //Placed on the heap
                                                                  //to avoid stack overload
    char Species[*pMaxLength];
    char Locationfound[*pMaxLength];
    char Desciption[*pMaxLength];
       
    
    cout << "Enter The Taxanomic Name For The Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Species, *pMaxLength);
    fout << Species << "\n";
    cout << "Enter The Location That The Species Was Found: ";
    cin.ignore(1,'\n');
    cin.getline(Locationfound, *pMaxLength);
    fout << Locationfound;
    cout << "Add a Description Regarding the Diatom: ";
    cin.ignore(1, '\n');
    cin.getline(Desciption, *pMaxLength);
    fout << Desciption;
    fout.close();
    delete [] pMaxLength; // delete pointer
    pMaxLength = 0; // set to null to avoid stray
    delete [] pmaxfile;
    pmaxfile = 0;
    
    
    
    return menu();
    
}
//***********************************************************//
//***********************************************************//
// Phytoplankton Defination

int Phytoplankon()
{
    int *pmaxfile = new int [80];
    
    char filename[*pmaxfile];
    cout << "Enter File Name: ";
    cin.get(filename, *pmaxfile);
    ofstream fout(filename);    //open for writing
    
    
    unsigned long int *pMaxLength = new unsigned long int [3000]; //Placed on the heap
                                                                //to avoid stack overload
    char Species[*pMaxLength];
    char Locationfound[*pMaxLength];
    char Desciption[*pMaxLength];
    
    
    cout << "Enter The Taxanomic Name For The Phytoplankton: ";
    cin.ignore(1, '\n');
    cin.getline(Species, *pMaxLength);
    fout << Species << "\n";
    cout << "Enter The Location That The Species Was Found: ";
    cin.ignore(1,'\n');
    cin.getline(Locationfound, *pMaxLength);
    fout << Locationfound;
    cout << "Add a Description Regarding the Phytoplankton: ";
    cin.ignore(1, '\n');
    cin.getline(Desciption, *pMaxLength);
    fout << Desciption;
    fout.close();
    
    delete [] pMaxLength; // delete pointer
    pMaxLength = 0; // set to null to avoid stray
    delete [] pmaxfile;
    pmaxfile = 0;
    
    
    return menu();
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1900
  • View blog
  • Posts: 5,697
  • Joined: 05-May 12

Re: Error With a Program

Posted 03 August 2012 - 07:40 PM

This code:
    int *pmaxfile = new int [80];
    
    char filename[*pmaxfile];



is not doing what you think it is doing.

Line 1 allocates space for an array of 80 integers. Note that array of 80 integers will have random data in it since C++ does not guarantee zero filling an array on allocation.

Line 3 allocates space on the stack as big as whatever random number happened to be the first integer in the array of integers.

Out of curiosity, why aren't you using the string class? You're including the string header on line 3 of your source file anyway. Or is this some kind of Howard Jones song?

This post has been edited by Skydiver: 03 August 2012 - 07:41 PM

Was This Post Helpful? 1
  • +
  • -

#6 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Re: Error With a Program

Posted 03 August 2012 - 07:44 PM

View PostSkydiver, on 03 August 2012 - 07:40 PM, said:

This code:
    int *pmaxfile = new int [80];
    
    char filename[*pmaxfile];



is not doing what you think it is doing.

Line 1 allocates space for an array of 80 integers. Note that array of 80 integers will have random data in it since C++ does not guarantee zero filling an array on allocation.

Line 2 allocates space on the stack as big as whatever random number happened to be the first integer in the array of integers.

Out of curiosity, why aren't you using the string class? You're including the string header on line 3 anyway. Or is this some kind of Howard Jones song?


I added the string class thinking i was going to use it, but i'm not sure i want to yet. Eventually I want to write my own classes and create my own objects, but for now this will do. I just need something for the professor to run off of. secondly, I don't think i know what i'm doing wrong i changed the code again and its still looping.


int *pmaxfile = new int;
    *pmaxfile = 80;
    
    char filename[*pmaxfile];
    cout << "Enter File Name: ";
    cin.get(filename, *pmaxfile);
    ofstream fout(filename);    //open for writing
    
    
    unsigned long int *pMaxLength = new unsigned long int;
    *pMaxLength = 3000;                                           //Placed on the heap
    //to avoid stack overload
    char Species[*pMaxLength];
    char Locationfound[*pMaxLength];
    char Desciption[*pMaxLength]


Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2485
  • View blog
  • Posts: 8,521
  • Joined: 08-August 08

Re: Error With a Program

Posted 03 August 2012 - 07:51 PM

I see no cin.ignore() in the menu function.
Was This Post Helpful? 1
  • +
  • -

#8 Hrand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-June 12

Re: Error With a Program

Posted 03 August 2012 - 07:58 PM

View PostCTphpnwb, on 03 August 2012 - 07:51 PM, said:

I see no cin.ignore() in the menu function.


good call, thanks. Now it seems to be working. A few more test and i should have a demo ready for monday. Now I know why the Prof recommended this site.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1900
  • View blog
  • Posts: 5,697
  • Joined: 05-May 12

Re: Error With a Program

Posted 03 August 2012 - 08:03 PM

The only things you are allocating in the heap is one int (pmaxfile) and one unsigned long int (pMaxLength). filename, Species, Location, and Description are still getting allocated on the heap. I think that things are not crashing because of stackoverflow because not you are only using about 9K of stack space, and possibly the Mac gives more than the default 8K.

The reason for the looping is because after you read in the menu choice, there is a CR left in the input buffer. When you call cin.get() to get the filename, it runs across that and returns immediately. (See documentation here: http://www.cplusplus...m/istream/get/) And then things just cascade downhill from there.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1