question from job interview

Write your question here.

1
2
3
4
5
6
  void func()
{int i;
int *pint=new int();
Do things
delete pint
}


what is the danger here?
find 2 ways to solve the problem?
It might not be modern c++, but there is no danger there.

What is the code behind Do things?
Smart pointers?

Aceix.
Q: what is the danger here? A: Not exception safe

Q: find 2 ways to solve the problem? A: There is only one good way to solve the problem.
1
2
3
4
5
6
7
void func()
{
    int i = 0 ; // automatic storage duration

    // Do things 

}

Do things is a regular code that doesn't contain any problem
Do things is a regular code that doesn't contain any problem

if this is true then "Do things" also checks if pint is valid ?.
if so then there is no "danger" other than uninitialized variable i

presumably "Do things" part has a good reason why pint must be a pointer, so function spends time to dinamicaly allocate plain int.

also delete pint in your posted exaple is invalid since there is a missing semicolon.

futer more your function may be written to be more readable by moving int i; one line bellow, and also shifting the body one tab to the right :)
Last edited on
In addition to not being exception safe, it may leak memory if "do things" contains a return statement. Even if not, the code is fragile because someone might add a return statement in the figure, thus inadvertently causing a memory leak.

For a small object like an int, there's no good reason to allocate it on the heap and free it. It should be an automatic variable instead.
and if there is a retrun in th middle what can we do to sovle the problem
Last edited on
and if there is a retrun in th middle what can we do to sovle the problem

In that case the code won't compile because of your typo Wakka wakka!. Seriously though, if there already IS a return in the middle of the "Do Something" code body, then that is where you would use some form of smart pointer such as Aceix suggested. If you are asked "what if someone adds one in the future?" then this is functionally the same as you're employer asking you "what if the person we assign this project to later is incompetent?". More often then not this won't be a question in the interview; if it is then you need to reconsider the kind of place that you are applying to.
and if there is a retrun in th middle what can we do to sovle the problem
unique_ptr<int> pint;
at this point you can put your finger in your ear and start singing marching songs knowing you don't have to care about memory management :D
> and if there is a retrun in th middle what can we do to sovle the problem

Repeat: For an int that is needed only for the duration of the function,

use automatic storage duration.
use automatic storage duration.
use automatic storage duration.
use automatic storage duration.

1
2
3
4
5
6
7
void func()
{
    int i = 0 ; // automatic storage duration

    // Do things 

}


Using a smart pointer for this is dumb.
+100 @ JLBorges.
I've got a feeling that you should use automatic storage duration...not sure what gives me that feeling but it is the thing to do.
I want to start off by saying that I am not intentionally trying to be argumentative here, I guess this is more so for my own sake. But from the admittedly limited amount of code given, everything suggests that 'i' and 'pint' are two distinct and different variables and with out more information they should remain so. I would think that if 'pint' was intended to point at 'i', then it would be initialized to point at 'i' and it would not be allocated it's own space.

EDIT: Now if 'pint' where a reference in the original example, then I can see how you guys came to this conclusion.

Over all I think that this interview question is stupid and so open ended that any answer you could give would be wrong for lack of data. It doesn't filter out any ones ability to read what a section of code does as a whole; and in fact it's inclusion in the interview process seems designed to discourage candidates who would point this fact out. It looks like it wants to find people who can identify what individual lines of code do but that trait is useless without the former. It's like something a TA would slap together and hope you don't actually think about it. If this really is the kind of crap you guys were asked in your first interview then I don't know how you guys made it through it with out jumping across the table and punching someone in the brain.
Last edited on


1
2
what is the danger here?
find 2 ways to solve the problem?


I guess by danger you mean compiler warnings.

1
2
3
4
5
6
7
8
9
 void func()
{int i; //garbage value
int *pint=new int();
Do things
delete pint //missing ;
//*pint = NULL;
//incase if it is deleted again the program won't crash
}
Last edited on
Its true that automatic storage would be the best here, but in the case of a pointer, a smart pointer is the best option.

@Gkneeus
*pint!=NULL, but pint=nullptr; just to promote good conventions.

Aceix.
this interview question is stupid and so open ended that any answer you could give would be wrong for lack of data


We had a C++ test that contained several questions that were somewhat open-ended. The purpose was to see what sort of answers the candidate would give and do spark discussion during the interview itself. This question may have been intended similarly. However, I agree that it's poorly described.

An example from our test was "what is the difference between malloc() and new? There are many differences and we wanted to see what candidates came up with.

Also, the purpose of the test was not to see if you could "pass" the test. Rather, it was designed to see if you could demonstrate knowledge that more-or-less corresponded with the level of experience that you claimed on your resume. We told candidates this up front.
Topic archived. No new replies allowed.