Jump to content
  • Advertisement
Sign in to follow this  
Rhaal

How am I wrong?

This topic is 3407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone explain to me how I got this answer wrong? This was on a test for a position I was trying for, but I really don't think it's wrong. "On some embedded systems that may not have full exception handling capabilities, what is the potential problem with the following code snippet?"
void	GetValue(void)
{
CClass	*pcClass;

pcClass = new CClass[3];

pcClass[0].m_nMember = 1;
pcClass[1].m_nMember = 3;
pcClass[2].m_nMember = 5;

delete [] pcClass;
}
My answer: When accessing member variables of pcClass, you should use the “->” operator, not the “.” operator. Now I understand there may be a problem with dereferencing an invalid pointer, but 1 - How is that even possible with this exact code? They're all alocated so they are not invalid - and 2 - How is my answer "wrong"? Thanks for any insight. I must be crazy.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Rhaal
When accessing member variables of pcClass, you should use the “->” operator, not the “.” operator.
Incorrect. The fact that pcClass is itself a pointer does not make pcClass[0] a pointer.

Quote:
Now I understand there may be a problem with dereferencing an invalid pointer, but 1 - How is that even possible with this exact code? They're all alocated so they are not invalid
What if the allocation fails?

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
"On some embedded systems that may not have full exception handling capabilities..."


By default, if new fails it will throw an exception. On a system with no exception mechanism you would probably be using no-throw new, and a failed new will return a null pointer. Therefor, pcClass may be null in this example.

Also, pcClass[0] gets you a CClass&, not a CClass*, so the use of operator. is correct.

Edit: Beaten, x3.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
Can someone explain to me how I got this answer wrong? This was on a test for a position I was trying for, but I really don't think it's wrong.

"On some embedded systems that may not have full exception handling capabilities, what is the potential problem with the following code snippet?"


You failed to understand the question. The question was not "will this blend(compile)?" It was a question about error handling in an environment that doesn't support the standards defined way. Staying on topic would have helped immensely.

Share this post


Link to post
Share on other sites
Wow you guys jumped all over it :) Thanks. I guess I'll plug this into some code and step through it. I never really messed with an array of classes - haven't had the need to. Guess I have the wrong understanding of what an exception is, so I'll study up there too.

Edit: Stonemetal, while I appreciate the insight there's no need to be a dick.

Share this post


Link to post
Share on other sites
Quote:
Original post by Rhaal
Wow you guys jumped all over it :) Thanks. I guess I'll plug this into some code and step through it.


Look up documentation on operator new, as well as overloading it. For extra fun, compile without C and C++ run-time libraries and implement those functions yourself (when you have no malloc/new available).

Of course, even when you do have exceptions, the nothrow is a big can of worms.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!