Defects in C++ code

Hello!

My C++ teacher asked me today, what problems and defects can I see in this code:

void Foo(int n)
{
int *x = new int[n];
int *y = new int[n];
Bar(x, y);
delete [] x;
delete [] y;
}

I need an advice. It's interesting for me, did I find all defects:)
Is 'n' guaranteed to be a positive non-zero number?
Last edited on
there is a problem concerning destructors I believe.

[edit] Ah just realized Bar(x, y); is not an object instantiation. Ignore this post.
Last edited on
Also, what does Bar() do?
Hint:
x is the address of the pointed-to value. (As is y.)
*x is the actual value. (As is *y.)
x in this case is an array, so maybe they want to pass the two arrays to Bar.
I know what they are doing, I was wondering if they did something dumb like have Bar() delete the pointers.
It will be silly if the "correct" answer from your teacher is that Bar() somehow deletes the pointers =D, but there is no clue here. Afterall it is only a "detect", not something we should never do (in fact people have to pass pointers frequently).

x is the address of the pointed-to value. (As is y.)

It's perfectly ok to pass a pointer as a parameter of Bar().

[EDITED]
Why pushing all the posts up with unnessary replies
Last edited on
firedraco.append("malfoy");

Sorry, I had to get that off my chest...
Topic archived. No new replies allowed.