www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000388

When Created: 07/04/2007 07:38:54
Against DJGPP version: 2.00
By whom: anilthirumala@gmail.com
Abstract: dynamic 2d array creation fails
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.

Note added: 07/05/2007 10:38:06
By whom: dj@delorie.com
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.

Closed on 07/05/2007 10:39:11: Not a bug in DJGPP
By whom: dj@delorie.com



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010