# How am I wrong?

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

## 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 on other sites
[] dereferences a pointer. pcClass[0] is a CClass &, so you would use . to get at the members.

##### Share on other sites
new throws std::bad_alloc if it can't allocate the memory.

##### Share on other sites
Quote:
 Original post by RhaalWhen 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 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 on other sites
Quote:
 Original post by RhaalCan 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 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 on other sites
Quote:
 Original post by RhaalWow 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.

1. 1
2. 2
JoeJ
20
3. 3
frob
19
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632206
• Total Posts
3004772

×