New Posts  All Forums:Forum Nav:

Some easy C++

post #1 of 6
Thread Starter 
Hey guys. Having a big of an odd error which I can't get my head around.

After altering my dynamic array in the function fDetails, I try and display each line of its contents, strings, but a get a EXC_BAD_ACCESS error.

The for loop in main is where I am having the difficulty, the rest seems to work.

Any idea why it does this, as I am just using the functionality of C++.
Code:
//
//  main.cpp
//  3. CPPTutorialA Slides 8-11
//
//  Created by Adam Thoseby on 09/09/2012.
//  Copyright (c) 2012 Adam Thoseby. All rights reserved.
//

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

int fDetails(string arrayTokens[]);

int main(int argc, const char * argv[])
{
    string arrayTokens[2];
    int nTokens;
    string * pStart, * pFinish;

    nTokens = fDetails(arrayTokens);
    
    for (int i=0; i<nTokens; i++) {
        cout<<arrayTokens[i]<<endl;
    }
    
    pStart = &arrayTokens[0];
    pFinish = &arrayTokens[nTokens];
    
    cout<<pStart<<endl<<pFinish;
    return 0;
}


int fDetails (string arrayTokens[]){
    int nTokens = 0, i = 0;
    string fname, number;
    
    
    //cout<<"Please enter in the address, file name and the extension so we can open the file: "<<endl;
    //cin>>fname;
    fname="/Users/ANCT/Desktop/num.txt";
    
    ifstream fin;
    
    fin.open(fname.c_str());
    if (!fin) {
        cout<<"\nPlease ensure that the file is located in the default directory.\nAlso please ensure that the file is spelt correctly, including the extension.\n"<<endl;
        return 0;
    }
    
    while (!fin.eof()) {
        i++;
        fin>>number;
    }
    
    fin.close();
    fin.open(fname.c_str());
    
    arrayTokens = new string[i];
    
    for (int n=0; n<i; n++) {
        getline(fin,arrayTokens[n]);
    }
    
    nTokens = i;
    
    return nTokens;
}
post #2 of 6
What platform are you running this on?
Also
What line exactly causes the error?
BlueStorm
(13 items)
 
  
CPUGraphicsRAMHard Drive
Xeon W3520 GTX 470 SLI 3x2GB OCZ Gold 1600Mhz 2xSamsung Spinpoint F3 RAID0 
OSPowerCase
Windows 7 x64 750W Corsair TX NZXT Tempest 
  hide details  
Reply
BlueStorm
(13 items)
 
  
CPUGraphicsRAMHard Drive
Xeon W3520 GTX 470 SLI 3x2GB OCZ Gold 1600Mhz 2xSamsung Spinpoint F3 RAID0 
OSPowerCase
Windows 7 x64 750W Corsair TX NZXT Tempest 
  hide details  
Reply
post #3 of 6
Thread Starter 
haha no, it is this that causes the problem
Code:
cout<<arrayTokens[i]<<endl;

I can put an & there to pull out the address, which is fine, but when I remove that, it gives the error.

Are you able to run it?

I am just running it on xCode, console application.
post #4 of 6
The better way to do this is to have the fDetails function RETURN a pointer to an array of strings, and provide the length via an output parameter. This is better because the caller doesn't need to allocate the array.
Code:
//
//  main.cpp
//  3. CPPTutorialA Slides 8-11
//
//  Created by Adam Thoseby on 09/09/2012.
//  Copyright (c) 2012 Adam Thoseby. All rights reserved.
//

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

string* fDetails( int *nTokens );

int _tmain( int argc, const char * argv[] )
{
        int nTokens = 0;
        string * pStart, * pFinish;
        string* arrayTokens = fDetails( &nTokens );

        for ( int i = 0; i < nTokens; i++ ) 
        {
                cout << arrayTokens[i] << endl;
        }

        pStart = &arrayTokens[0];
        pFinish = &arrayTokens[nTokens];

        cout << pStart << endl << pFinish;

        /* Free the buffer! */
        delete[] arrayTokens;
        return 0;
}


string* fDetails( int *nTokens )
{
        int i = 0;
        string* arrayTokens = 0;
        string fname, number;

        *nTokens = 0;

        fname = "/Users/ANCT/Desktop/num.txt";

        ifstream fin;
        fin.open( fname.c_str() );

        if (!fin) 
        {
                cout << "\nPlease ensure that the file is located in the default directory.\nAlso please ensure that the file is spelt correctly, including the extension.\n" << endl;
                return 0;
        }

        while ( !fin.eof() ) 
        {
                i++;
                fin >> number;
        }

        fin.close();
        fin.open( fname.c_str() );

        arrayTokens = new string[i];

        for ( int n = 0; n < i; n++ ) 
        {
                getline( fin, arrayTokens[n] );
        }

        *nTokens = i;
        return arrayTokens;
}

The code also fixes your problem smile.gif
Anyway, what is happening here is that the array isn't actually getting passed reference. When you pass an "array" to a function in the way you were, only the pointer to the first element of the array is actually copied.
Edited by tompsonn - 9/9/12 at 5:55pm
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by tompsonn View Post

using namespace std;


i think its because your computer has an std
post #6 of 6
Quote:
Originally Posted by meckert15834 View Post

i think its because your computer has an std

Lol...
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
My System
(30 items)
 
"Zeus"
(13 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i5 2500K (4.5ghz @ 1.320v) Gigabyte Z68X-UD3R-B3 MSI R7970 Lightning Corsair 16GB (4x4GB) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Crucial M4 128GB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 7 Ultimate 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2212HM Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Microsoft IntelliMouse Optical  XTRAC Ripper XXL 
AudioAudioAudioAudio
Westone W3 IEMs RE-272 IEMs Shure SE-215 IEMs Schiit Bifrost DAC 
AudioAudio
Schiit Asgard 2 amp HiVi Swan M50W 2.1 
CPUMotherboardGraphicsRAM
Intel Core i7 950 GA-X58-UD3R Radeon HD 5450  24GB Corsair @ 1333mhz 
Hard DriveOSPowerCase
4x WD Cavair Red 1TB in RAID 0 Windows Server 2008 R2 x64 Corsair HX-520 LianLi LanCool 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming