zO_ok
zO_ok

Reputation: 11

Trying C++ on Xcode on Mac- ERROR-exc_bad_access (code=1 address=0x100500000)

I have been trying to write a program where I have to read a binary file which has around 30 million long values and we have to find out the modes I have successfully done it using a smaller number of values but when I try with the file it always gives me the error- exc_bad_access (code=1 address=0x100500000), I am trying it in Xcode on my MAC and implementing in C++. The code is as below, can anyone help me out?

#include <iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

void mode(long data[], long size, long &num_modes, long modeNums[], long    &maxFrequency);

int main()
{




long *data,*modeNums, size;

ifstream binaryin("TestData.bin", ios::binary);
binaryin.read(reinterpret_cast<char *>(&size), 4);



data = new(nothrow)long[size];




if(!data)
{ cout<<"Memory allocation error for data array, program will terminate\n";
    system("pause");
    exit(0); }

binaryin.read(reinterpret_cast<char *>(data),size*sizeof(long));

//cout<<"SIZE "<<size<<endl;

long num_modes;long maxFrequency;
modeNums=new(nothrow)long[size];

size=3000000;

   mode(data,size,num_modes,modeNums,maxFrequency);

   /* for(int i=0;i<20;i++)
     cout<<"data[i]"<<data[i]<<endl;*/



   // cout<<"maxFrequency= "<<maxFrequency;
   // cout<<"\nnum_modes= "<<num_modes<<endl;

       //  for(int i=0;i<num_modes;i++)
      //    cout<<"modeNums[]= "<<modeNums[i]<<endl;

}


void mode(long data[], long size, long &num_modes, long modeNums[], long  &maxFrequency)
{
  // size=300000;

// cout<<"SIZE "<<size<<endl;

int cnt=0,val=0;// long *arr,*arr1;

vector<long> arr;
vector<long> arr1;

//arr=new(nothrow)long[size/2];
//arr1=new(nothrow)long[size/2];

// cout<<"INSIDE THE FUNCTION\n";
//cout<<"SIZE "<<size<<endl;
long k;
//cout<<"SIZE   "<<size<<endl;

cout<<"\nstarting for loop\n";

for(int i=0;i<size;i++)
{   cout<<"Inside for loop\n";
    cnt=0; k=(size-1);

    long num=data[i];

    int flag=0;

    int temp=val;

    while(temp!=0)
    {  cout<<"inside while loop\n";
        if(arr[temp]==num)
        { flag=1;}
        temp--;
    }

    if(flag==0){
        int count=0;
        for(int j=0;j<(size/2);j++)
        {// cout<<"Inside for which is inside while\n";
            cout<<"data[j] "<<data[j]<<" "<<"data[k] "<<data[k]<<endl;
             cout<<"COUNT "<<count++<<endl;
            if(num==data[j] && num==data[k])
            { cnt+=2; }

            else if(num==data[j]||num==data[k])
            {  cnt++;   }
            k--;
        }
        arr.push_back(num);arr1.push_back(cnt);
        val++;
    }
    else
        flag=2;
}

cout<<"\nend of for loop\n";
// for(int i=0;i<val;i++)
//   cout<<"NUM "<<arr[val]<<"Mode "<<arr1[val]<<endl;

maxFrequency=0;
for(int i=0;i<val;i++)
{
    if(arr1[i]>maxFrequency)
        maxFrequency=arr1[i];
}

num_modes=0; int value=0;
for(int i=0;i<val;i++)
{
    if(arr1[i]==maxFrequency)
    {   num_modes++;
    modeNums[value]=arr[i];
        value++; }
}

 /*   int value=0;
for(int i=0;i<val;i++)
{
    if(maxFrequency==arr1[i])
    { modeNums[value]=arr[i];
        value++;}
} */

 //  for(int i=0;i<val;i++)
//    cout<<"Value "<<arr[i]<<"                    Mode "<<arr1[i]<<endl;

//  cout<<"maxFrequency= "<<maxFrequency<<endl;
// cout<<"\nnum_modes= "<<num_modes<<endl;
cout<<"\nend of function\n";

}

enter image description here

Upvotes: 1

Views: 213

Answers (1)

The Dark
The Dark

Reputation: 8514

This line:

size=3000000;

means that you allocate your arrays of a certain size (whatever you read from the file), then tell your mode function that those arrays are actually 3000000 longs in size. This could lead to a buffer overrun.

I can't see any reason for this line to be in there.

Also if you use new(nothrow) you should check the return value.

Upvotes: 1

Related Questions