Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include "StdAfx.h"
#include<iostream>
using namespace std;

class stream
{
	char *name_of_stream;
	int year_of_starting;
	public:
	void set_stream(char *n,int y)
	{
		name_of_stream=n;
		year_of_starting=y;     //error comes at this point
		
	}
	void get_stream()
	{
		cout<<name_of_stream;
		cout<<year_of_starting;
		
	} 
};
class students:public stream
{
	char *stud_name;
	int stud_rollno;
	public:
	void set_data(char *n,int r)
	{
		stud_name=n;
		stud_rollno=r;
	}
	void get_data()
	{
		cout<<stud_name;
		cout<<endl<<stud_rollno;
	}			
};
int main()
{
	students *s;
	int num=0;
	s=new students[num];
	char c;
	char stream[10],name[50];
	int year;
	double rollno;
	do
	{
		cout<<"enter the stream";
		cin>>stream;
		cout<<"enter the year of joining the college"<<endl;
		cin>>year;
		cout<<"enter your name";
		cin>>name;
		cout<<"enter your rollno";
		cin>>rollno;
		(s+num)->set_data(name,rollno);
		(s+num)->set_stream(stream,year);
		num++;
		cout<<"do you want to continue  (y/n)";
		cin>>c;
	}while(c!='n');
	cout<<num;
	for(int i=0;i<num;i++)
	{
		(s+i)->get_data();
		(s+i)->get_stream();
	}
	delete s;
	return 0;
}

the program doesn't run after executing the do-while loop in main()

error is:-
Windows has triggered a breakpoint in "projectname.exe".

This may be due to a corruption of the heap, which indicates a bug in projectname.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while database_of_student.exe has focus.

The output window may have more diagnostic information.



please help me
thnx in advance
Posted
Comments
JackDingler 2-Jul-13 15:18pm    
Have you tried stepping through the code with the debugger?
bhawin parkeria 2-Jul-13 15:36pm    
yup
JackDingler 2-Jul-13 15:48pm    
What did you learn?

Where does the exception occur?
Have you turned on all run-time checks?

1 solution

Part of your problem is here:

C++
    int num=0;
s=new students[num];


you are creating a new array of students that's zero elements long. So every time you advance the pointer you are corrupting memory. You need to allocate as many elements as you are going to use, or use some sort of linked list or vector if you want dynamic allocation.

Also you are adding to num by using num++, which only advances the pointer sizeof(int), which is going to overwrite data in your students array. You need to advance the pointer by sizeof(students), not just num++. You are trying to use num as both an iterator and a counter, which isn't going to work. You need to separate the two, have a count, and a pointer to your current element.
 
Share this answer
 
v2
Comments
H.Brydon 2-Jul-13 15:35pm    
Agreed (+5). Two more points:
(1) The code "(s+num)->set_data(name,rollno);" may be technically correct but the more conventional representation is "s[num]->set_data(name,rollno);"
(2) This looks like homework. Those type of questions shouldn't be asked here, and shouldn't be answered in full either. (Ron did it right IMHO)
(3) You would think that you are allocating an array of zero elements, but in fact a zero allocation gives you one (1) element. Scott Meyers wrote an interesting analysis of this in one of his books but I can't find a similar reference on the net.
bhawin parkeria 2-Jul-13 15:36pm    
thnx for the answer
can you tell me the code of incrementing with sizeof(students)
i am confused at that
Ron Beyer 2-Jul-13 15:53pm    
H.Brydon is right, you should use the s[num] instead of directly accessing the s+... to get to the right element. Using the array accessors automatically calculates the correct position of the pointer, rather than trying to do it yourself. Otherwise you need to do something like (s+(num * sizeof(students))->MethodName() to get to the right element.
H.Brydon 2-Jul-13 16:23pm    
Your formula is not correct. It would be (s+num)->MethodName() ... which adds to my point because people incorrectly believe that s+num points to 'num' bytes past s, when it actually points to 'num'*sizeof(blah) past s. Use of s[num] as the pointer somewhat disambiguates the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900