| www.delorie.com/djgpp/bugs/show.cgi | search |
I had created a c++ program with dynamic 2d array creation it compiles successfully, but when running it shows general proctection fault.the code is given below please tel me where i went wrong? I tried to sent this to djgpp@deloire.com but it rejects.
#include<iostream.h>
class dynamic
{
int **ptr;
int n;
public :
void allocate()
{
cout<<"Enter the number of students";
cin>>n;
ptr= (int **)new int [n][3];
}
void accept()
{
cout<<"enter the marks of "<<n<<"students";
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
cin>>ptr[i][j];
}
}
void display()
{
cout<<"the marks of students are"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
cout<<ptr[i][j];
}
}
};/* end of class dynamic*/
void main()
{
dynamic d;
d.allocate();
d.accept();
d.display();
}
the program executes upto accept function statement 1 when it do the cin statement in the loop it fails.This bug tracker is not the right place to learn C++, it's for bugs in the DJGPP library. Your code is wrong; take the cast off the "new" and heed the warnings thereof, as you're not allocating the memory correctly.
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2010 by DJ Delorie | Updated Jul 2010 |