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

class stream
	char *name_of_stream;
	int year_of_starting;
	void set_stream(char *n,int y)
		year_of_starting=y;     //error comes at this point
	void get_stream()
class students:public stream
	char *stud_name;
	int stud_rollno;
	void set_data(char *n,int r)
	void get_data()
int main()
	students *s;
	int num=0;
	s=new students[num];
	char c;
	char stream[10],name[50];
	int year;
	double rollno;
		cout<<"enter the stream";
		cout<<"enter the year of joining the college"<<endl;
		cout<<"enter your name";
		cout<<"enter your rollno";
		cout<<"do you want to continue  (y/n)";
	for(int i=0;i<num;i++)
	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
JackDingler 2-Jul-13 15:18pm    
Have you tried stepping through the code with the debugger?
bhawin parkeria 2-Jul-13 15:36pm    
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:

    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
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