The following code builds perfectly well and runs on Turbo C++. I build the same on MSVC2010, it builds without error, but when I run it (run without debug), It shows
An unhandled win32 exception occurred in gentic.exe
Also during debugging it shows :
Unhandled exception at 0x00411672 in genetic.exe: 0xC0000005: Access violation writing location 0xcccccccc.
This occurs after I input row and columns... at *dat2=(double *)malloc(r*sizeof(double*));
(yellow arrow is now pointing these lines)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
class genetic
{
public:
int i,j,m,n;
double **dat2,**dat;
double** createarray(int r,int c)
{ int i;
*dat2=(double *)malloc(r*sizeof(double*));
for(i=0;i<r;i++)
{
dat2[i]=(double*)malloc(c*sizeof(double));
}
return dat2;
}
void input()
{
printf("enter rows \n");
scanf("%d",&m);
printf("enter cols \n");
scanf("%d",&n);
dat=createarray(m,n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
double val;
scanf("%lf",&val);
dat[i][j]=val;
}
}
}
void output()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3lf ",dat[i][j]);
}
printf("\n");
}
}
};
void main()
{
genetic g1;
g1.input();
g1.output();
getch();
}
Any idea why this diffrent behavior in MSVC and how do we solve this issue?
Update:
As suggested I changed to :
double** createarray(int r,int c)
{ int i;
double **dat2;
dat2=(double *)malloc(r*sizeof(double*));
for(i=0;i<r;i++)
{
dat2[i]=(double*)malloc(c*sizeof(double));
}
return dat2;
}
But still I am facing problem:
Error 1 error C2440: '=' : cannot convert from 'double ' to 'double *'