Allocating memory of a structure

Hello i am trying to allocate memory for a structure is this correct
Structure
1
2
3
4
5
6
struct Student 
{
	char * name; // pointer to dynamically array for the student’s name
	unsigned int score;

}

Allocating memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Student ** allocateArrayOfStudentPointers (unsigned int arraySize)
{
	Student ** ptrToPtr = NULL;
	 
	try
	 {
		ptrToPtr = new Student * [arraySize];
	}
	 catch(bad_alloc & param)
	 {
		return NULL;
	 }

	 return ptrToPtr;
	
	 
}

Yes, although it could be made significantly shorter.
1
2
3
4
//size_t is preferable for buffer sizes
inline Student **allocateArrayOfStudentPointers(size_t n){
	return new (std::nothrow) Student *[n];
}
¿Why do you want an array of pointers?
Oh ok @helios thank you for the tip. Im not that far into those type of functions yet though.
@ helios: I'm curious, why would you declare that inline? It doesn't appear to be a member function.

OP wrote:
Im not that far into those type of functions yet though.

That's fine, we can explain this code if that's what you are looking for. Because you are not familiar with it though we should mention that because helios designated "std::nothrow" then in order to test for a bad allocation, like you are doing on with your try catch block, you would test the returned value for NULL.
I'm curious, why would you declare that inline? It doesn't appear to be a member function.
I don't see what one thing has to do with the other.
The only time I've known the inline designation to be beneficial is when I'm writing a struct\class and for one reason or another (in my case thread injection) I don't want the compiler to move it relative to the object.

EDIT: I can except if I've been completley misusing inline this entire time. I'm still trying to break some of my bad habits.
Last edited on
inline is a suggestion for the compiler to copy the body of the function onto the point of call to avoid making an actual subroutine call. It can be applied to any function. In the particular case of member functions, they can be asked to be inlined without using the inline keyword.
So, please let me know if I understand you correctly.

- Due to the actual amount of work being performed by the function, you decided to make sure this function was compiled inline with it's call because pushing and popping the registers in this case would just be needless over head?

- Or was it to make sure that the memory you allocated doesn't get overwritten\deallocated because the subroutine doesn't exist so there is no call to RET? (This one was a last minute theory right before I hit submit).
Last edited on
The former is closer to the truth. The latter doesn't make sense.

inline is a suggestion. There's no guarantee that the code will actually get copied (some compilers have an alternative keyword that does make this guarantee). However, inline works great for short, macro-like functions like the one above.
Yeah, the second thought was a little half-baked but I was thinking that because in a C++ __stdcall() it's up to the callee to preserve and restore the memory state (ESP and EBP), not creating a subroutine would mean that you are remaining in the same Stack Frame so the memory would remain acessable. But since you're returning that address anyway it doesn't matter in the slightest.
Last edited on
I was thinking that because in a C++ __stdcall() it's up to the callee to preserve and restore the memory state (ESP and EBP), not creating a subroutine would mean that you are remaining in the same Stack Frame so the memory would remain acessable.
When thinking about semantics, don't think about any platform-specific details like calling conventions or registers. It will only trip you up.
inline doesn't change the semantics of a function, only the way code is generated.
I'll remember that. Thanks for the explaination.
Topic archived. No new replies allowed.