How to use new operator

1
2
3
4
5
6
7
struct student
{
   char name[128];
   int age;
   int gender;
   int courseCode;
};


Sorry for my bad skills , for this code , how I use new operator to create a dynamic array of n student instances? N is an integer that I defined already .
student* arrayOfStudents = new student[N];
thanks , easy to get what you mean for this code .

by the way , how I want to return the value of the new operator ? in the event the computer cannot allocate the requested memory . Erm , for some example , how should I write the code to check if the new actually succeeded?

sorry .. still in progress of structure since I still having diploma..
If new fails it throws an exception.
1
2
3
4
5
6
7
8
try
{
	student* arrayOfStudents = new student[N];
}
catch (std::bad_alloc& e)
{
	std::cout << "The allocation failed!" << std::endl;
}
http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/

The first version allocates size bytes of storage space, aligned to represent an array object of that size (or less, if the implementation uses array overhead), and returns a non-null pointer to the first byte of this block. On failure, it throws a bad_alloc exception.
Last edited on
@peter , thanks again . this is the way to check it right?

@moschops , i read the things already , so if I wan to delete the dynamic allocated array that i create in
 
student* arrayOfStudents = new student[n]

how i delete is
 
delete[] arrayOfStudents

right?
indeed.
Topic archived. No new replies allowed.