some questions . please give feedback

F

free2cric

Hi,
I attanded an interview on C++
Question asked were and my answers to them..

1. In a CPP program what does memory leak occure?
-- i said.. In a constructor , the pointer variables were assigned
a memory block but the programmer forgets to free them in the
destructor by usng delete operator.

2. ok, the delete operator is used .. variables were freed. but still
memory leak occurs .. tell me why
-- no answer

3. tell me at the end of the program , i want to prevent any kind of
memory leak still there.. how to do that?
-- no answer

4. what is the size of empty class and its object?
-- one byte

5. I want to define a class in such a way so that no further class can
be derived from it?
-- no answer

6. In a doubly link list ,, how can u decide a head and a tail node?

Please review my answer and try to give me feedback.

thanks,
Cric
 
G

Greg

Hi,
I attanded an interview on C++
Question asked were and my answers to them..

1. In a CPP program what does memory leak occure?
-- i said.. In a constructor , the pointer variables were assigned
a memory block but the programmer forgets to free them in the
destructor by usng delete operator.

More generally, a memory leak occurs when a pointer to allocated memory
is "lost" in any number of ways - by overwriting it, by a failure to
store it, by a failure to free it when the structure which stored it
was itself deleted, and so forth.
2. ok, the delete operator is used .. variables were freed. but still
memory leak occurs .. tell me why
-- no answer

This type of leak can happen when an object of a derived class is
deleted as a pointer to a base class, and the base class has no virtual
destructor. In this case the member variables declared in the derived
class are not destroyed.
3. tell me at the end of the program , i want to prevent any kind of
memory leak still there.. how to do that?

Use the debugging facilities offered by the programming environment
such as MallocDebug on some UNIX based systems, to report any memory
blocks still allocated at program termination.
4. what is the size of empty class and its object?
-- one byte

Its size is greater than zero bytes.
5. I want to define a class in such a way so that no further class can
be derived from it?
-- no answer

Declare its constructor private and provide a factory method to
instantiate instances of the class.
6. In a doubly link list ,, how can u decide a head and a tail node?

Flip a coin. Actually, I believe the "next" link would be null in the
tail node, and the "previous" link would be null in the head node.

Greg
 
Z

Zara

Hi,
I attanded an interview on C++
Question asked were and my answers to them..

1. In a CPP program what does memory leak occure?
-- i said.. In a constructor , the pointer variables were assigned
a memory block but the programmer forgets to free them in the
destructor by usng delete operator.

2. ok, the delete operator is used .. variables were freed. but still
memory leak occurs .. tell me why
-- no answer
Some of the data freed may contain other dynamic pointers that are not
destroyed. Some memory might be allocated by using C operator alloc or
another dofferent means, and never freed or whichever methos is used
3. tell me at the end of the program , i want to prevent any kind of
memory leak still there.. how to do that?
-- no answer
You may overload global operator new and delete, such that they leave
somewhere a trace of data not freed, and use this traces to do a
final freeing (but with no object deleting)
For the other ways of allocating memory, each should be treated its
own way
4. what is the size of empty class and its object?
-- one byte

5. I want to define a class in such a way so that no further class can
be derived from it?
-- no answer

Make all constructors private
6. In a doubly link list ,, how can u decide a head and a tail node?
head node: pointer to previous is 0 (or some other value meaning
beginning)
tail node: pointer to nect is 0 (or some other value meaning end)
 
P

persenaama

4. what is the size of empty class and its object?
Its size is greater than zero bytes.

The size can be zero bytes when doing inheritance.

class foo
{
};

class bar : public foo // foo can be 0 bytes in this case
{
int x;
};
 
G

Greg

persenaama said:
The size can be zero bytes when doing inheritance.

class foo
{
};

class bar : public foo // foo can be 0 bytes in this case
{
int x;
};

The incremental increase in the size of class by adding a member can be
zero (known as "the empty base class optimization") but the size of a
class itself is always non-zero.

Besides, it's easy to tell the motivation behind this question. "Zero"
would be the obvious, naive answer which is exactly why any answer
mentioning a zero-sized object would lose points. And the applicant
will not gain points by complicating the answer with extraneous
information, which even if correct, makes the answer harder to
understand and for the interviewer to decide whether it is completely
correct.

Greg
 
A

Alf P. Steinbach

* Greg:
The incremental increase in the size of class by adding a member can be
zero (known as "the empty base class optimization") but the size of a
class itself is always non-zero.

Besides, it's easy to tell the motivation behind this question. "Zero"
would be the obvious, naive answer which is exactly why any answer
mentioning a zero-sized object would lose points. And the applicant
will not gain points by complicating the answer with extraneous
information, which even if correct, makes the answer harder to
understand and for the interviewer to decide whether it is completely
correct.

Well, with reasonable parsing of the text persenaama is correct, and you're
not. ;-)

The newser will not gain points by complicating the answer with extranous
information and managerial gobbledygook which, even if incorrect, gives the
posting an appearance of competence.

Nor will the newser gain points by attempts at weaseling, but only by
admitting his error.

persenaama referred as I read it to the size of an object, not of a class, and
the Holy Standard mentions specifically in note 70 that the actual size of a
base class subobject can be less than the sizeof of the subobject's class.

Mentioning a zero-sized base class sub-object, as persenaama did, should
therefore of course gain points, not lose them as you write.
 
G

Greg

Alf said:
* Greg:

Well, with reasonable parsing of the text persenaama is correct, and you're
not. ;-)

The newser will not gain points by complicating the answer with extranous
information and managerial gobbledygook which, even if incorrect, gives the
posting an appearance of competence.

Nor will the newser gain points by attempts at weaseling, but only by
admitting his error.

persenaama referred as I read it to the size of an object, not of a class, and
the Holy Standard mentions specifically in note 70 that the actual size of a
base class subobject can be less than the sizeof of the subobject's class.

Mentioning a zero-sized base class sub-object, as persenaama did, should
therefore of course gain points, not lose them as you write.

The original question, albeit somewhat garbled, asked for an absolute
answer. A subobject base may be smaller than the derived class, but it
doesn't have to be. And in real life even that much is uncertain since
it requires a compiler that supports this optimization in the first
place. Therefore an answer of "zero" comes with a lot qualifications,
and still doesn't provide the clearcut answer the question sought.
Wouldn't a more nuanced answer better educate the questioner? No doubt,
but the candidate has to first answer the question asked. The candidate
is not entitled to answer a different question that she would have
preferred or one that would better shown off her knowledge. And since
the question didn't mention empty subobjects or derived classes - it
mentioned only an empty class - the best thing to do - if the job is of
any interest at all - is to give the absolute answer the questioner
wants and save the lecture for another day.

Sure, veering off in one's own direction spouting reams of C++ minutiae
may dazzle the interviewer, or it may irritate the interviewer to no
end if every answer to a question asked goes on for five minutes longer
than it needs to. Imagine how well such a habit would wear on potential
coworkers. Especially, if when the answer finally winds down, the
questioner is no more sure of the answer having asked the question.

Certainly breadth and depth of knowledge have value. But unless that
knowlege can prove useful, the likelihood of being paid just because
one has the knowledge is rather slim.

Greg
 
A

Alf P. Steinbach

* Greg:
[snipped rambling]

And since the question didn't mention empty subobjects or derived classes - it
mentioned only an empty class - the best thing to do - if the job is of
any interest at all - is to give the absolute answer the questioner
wants and save the lecture for another day.

I think you should have saved those lectures you've given here... Why not
just admit you were wrong instead of huffing & puffing? Anybody can make a
mistake.
 
G

Greg

Alf said:
* Greg:
[snipped rambling]

And since the question didn't mention empty subobjects or derived classes - it
mentioned only an empty class - the best thing to do - if the job is of
any interest at all - is to give the absolute answer the questioner
wants and save the lecture for another day.

I think you should have saved those lectures you've given here... Why not
just admit you were wrong instead of huffing & puffing? Anybody can make a
mistake.

I certainly make my share of mistakes. You need only look in my source
code to find some. But no one has made a mistake here. Persenaama's
answer does not lose points because it is incorrect. It loses points
because it is the wrong answer to give in response to that interview
question. It doesn't address the question directly, it muddies the
waters and the interviewer may not even know as much. Granted, it may
be unjust to lose points for an otherwise factually correct answer, but
this thread after all is supposed to be helping the original poster
learn how to answer C++ interview questions effectively. And we are
doing a disservice to the original poster if we are claiming that his
answer of "one" was incorrect and that it really should have been
"zero". That claim is simply misleading.

In a C++ interview, the candidate has to do more than simply be
correct. The candidate has to answer correctly the questions asked. On
USENET we can pick and choose which C++ questions to answer, a
candidate being interviewed does not have that luxury - and for some
obviously making such an adjustment is more difficult than it is for
others.

Greg
 
A

Alf P. Steinbach

* Greg:
Persenaama's answer does not lose points because it is incorrect. It loses
points because it is the wrong answer to give in response to that interview
question.

All this evasive action and lying even more about Persenaama won't save you,
Greg.

Persenaama _corrected_ your incorrect, wrong, misleading answer, to wit

* Greg -> (e-mail address removed):
Its size is greater than zero bytes.

to which Persenaama gave a purely technical correction, with code example, and
what was your response? To try to put that correction in a context where it
would appear to be something else and would reflect badly on Persenaama
instead of on yourself. You should have admitted your error, or let it be.
 
G

Greg

Alf said:
* Greg:

All this evasive action and lying even more about Persenaama won't save you,
Greg.

Persenaama _corrected_ your incorrect, wrong, misleading answer, to wit

* Greg -> (e-mail address removed):

to which Persenaama gave a purely technical correction, with code example, and
what was your response? To try to put that correction in a context where it
would appear to be something else and would reflect badly on Persenaama
instead of on yourself. You should have admitted your error, or let it be.

What error? An empty class does have a size greater than zero bytes.
See §9.0/2 if you are skeptical. It is an empty _base_ class (of an
empty derived class) that may be zero bytes in size. The same holds
true for an object. An object of an empty class will always be larger
than zero bytes. A base class _subobject_ on the other hand can be zero
bytes in size. Since the question specifically used the terms "class"
and "object" and not "base class" and "subobject", the empty base class
optimization is ruled out. That leaves only one correct answer to this
question. And that answer is not "zero".

Greg
 
A

Alf P. Steinbach

* Greg:
What error?

See Persenaama's correction to your earlier message.

Since the question specifically used the terms "class"
and "object" and not "base class" and "subobject", the empty base class
optimization is ruled out.

Do you really believe that anybody will think that you believe that?

"What is the size of an object of an empty class?"

Oh, let's say it's always greater than zero, because if they'd meant to
include the zero case, which is the interesting thing about empty classes and
sizes of their objects, they'd certainly hint a bit stronger, yes sir.

That leaves only one correct answer to this question.

Admit your error, Greg.

And that answer is not "zero".

And do stop lying about Persenaama.
 
P

persenaama

If someone asks me, I'll say that if I were giving the full answer, not
just adding a sidenote, I would have first mentioned that each object
must have unique address according to the specification, therefore
something must be done. I bet it's not black magic so I suppose the
objects are given some size other than zero, atleast that sounds the
most pragmatic thing to do to me. I wouldn't bet my testicles on that
though!
 
P

persenaama

answer of "one" was incorrect and that it really should have been "zero".

I did not make such claim! That's bullshit!
and for some obviously making such an adjustment is more difficult than it is for
others.

That's more than bullshit, that's straight-out slander! OMG! ROTFL!
Just kidding, but because you seem to take interest in me here's what
I'd answer to these questions..

1. In a CPP program what does memory leak occure?

I'm assuming that's not the original question, I think the "what"
should read "where", or the question in general tried to query HOW
memory leaks occur, in general. Apologies but I am unable to answer
nonsense question with facts so I had to do some reconstruction here.
Apologies if I am wrong. Okay, to the answer.

Usually memory leak occurs when memory is allocated with
new/new[]/malloc and matching delete/delete[]/free is missing. That's
the most typical way to conjure this beast anyway.

2. ok, the delete operator is used .. variables were freed. but still
memory leak occurs .. tell me why

Apperently not all delete operators were called where needed. It is a
common pattern to allocate memory in one place, and free it on other
place without one-to-one correspondence and clear ownership model.. in
models like this memory leaks are easy to conjure if the policy is not
followed throughout. Or maybe the destructor of object doesn't delete
memory that should be it's responsibiliy to delete.

Those are just the reasons that come into mind first, ther are more...
maybe a follow-up question if the interviewer is so inclined. Maybe a
discussion about losing ability to free up memory (such as pointer
going out of the scope, being in stack or part of object).

3. tell me at the end of the program , i want to prevent any kind of
memory leak still there.. how to do that?

At end of the program, it doesn't matter as all allocated memory is
freed automatically when the process terminates anyway.

Okay, so I stumbled on that one. Big Deal. Maybe this is a hook for
memory leak detection? I wouldn't guess the intention of the question
just say what I think about the issue. That's what I think. :)

4. what is the size of empty class and its object?

Enough to guarantee unique address for instances of the class. <-
Really, that is what I would answer. The expected follow up is: "And
how would you do that", I would say that's up to the compiler vendor to
decide because I wouldn't want the job anyway and would be a smartass
and just enjoy the situation a while before I take my shit and leave.
Really!

5. I want to define a class in such a way so that no further class can
be derived from it?

I haven't ever thought about that, but I guess making the default
constructor private, and then not giving the class any other
constructors? When I don't know for sure, I don't try to hide it. I
even ask my colleagues for help when I need it. You see here in Finland
we have this open thing about working places where you can go and ask
w/o shame, and no need to promose yourself all the time. A strange
cultural thing, or maybe it is just where I work.

So in short: wow, I don't know! Hope my theory turns out to be okay,
notice that I would immediately test it given the chance. Notice also
that I didn't rush out to the compiler and c++ specs to check answer
first and test it in practise.

6. In a doubly link list ,, how can u decide a head and a tail node?

The simple solution is to have "NULL" pointer at prev in the head node
and "NULL" pointer at next in the tail node.

Inserting nodes to front or back of the sequence is simpler, however,
if there is a sentinel node at each end. This can give a slight speed
bump in iteration as the value isn't constant like "NULL" pointer would
be (consumes more registers).

The mileage varies depending on what tradeoffs the implementation is
willing to do.

.... end

That's about it. Please rate my answers as possible employer, please be
fair and give me how you would rate me. I'm interested as I never been
to a job interview (the employers always seek me rather than the other
way around).
 
P

persenaama

I am using the same compiler and I get the following results:

sizeof(bar) == sizeof(int) == 4

Where did the foo go?

sizeof(foo) == 1, ofcourse.. but that's wasn't my point. :)
 
K

Kai-Uwe Bux

Greg wrote:

[snip]
[snip]
Since the question specifically used the terms "class"
and "object" and not "base class" and "subobject", the empty base class
optimization is ruled out.

Huh? That interpretation of the words "class" and "object" has no
foundation on the wording of the standard whatsoever. The standard
is pretty clear about the meaning of the term object:

1.8/1

An object is a region of storage. ...

1.8/2

Objects can contain other objects, called sub-objects.
A sub-object can be a member sub-object (9.2), a base
class sub-object (clause 10), or an array element. An
object that is not a sub-object of any other object is
called a complete object.

So the term "object" includes sub-objects. Unless someone is
explicitly saying "complete object", there is no reason to
assume that sub-objects are not considered. Also, the term
object cannot be construed to apply to most derived objects
only.


Best

Kai-Uwe Bux
 
G

Greg Comeau

The original question, albeit somewhat garbled, asked for an absolute
answer.

I agree it's often important to cut through the chase, but
I wouldn't be convinced the question asked for an absolute answer.
A subobject base may be smaller than the derived class, but it
doesn't have to be.

That's what he said.
And in real life even that much is uncertain since
it requires a compiler that supports this optimization in the first
place.

If it matters, most modern compilers do support it.
Therefore an answer of "zero" comes with a lot qualifications,
and still doesn't provide the clearcut answer the question sought.
Wouldn't a more nuanced answer better educate the questioner? No doubt,
but the candidate has to first answer the question asked. The candidate
is not entitled to answer a different question that she would have
preferred or one that would better shown off her knowledge. And since
the question didn't mention empty subobjects or derived classes - it
mentioned only an empty class - the best thing to do - if the job is of
any interest at all - is to give the absolute answer the questioner
wants and save the lecture for another day.

Maybe. What if a question is bogus? What if the question is
intended to trick? A person can respond w/o lectures.
Sure, veering off in one's own direction spouting reams of C++ minutiae
may dazzle the interviewer, or it may irritate the interviewer to no
end if every answer to a question asked goes on for five minutes longer
than it needs to. Imagine how well such a habit would wear on potential
coworkers. Especially, if when the answer finally winds down, the
questioner is no more sure of the answer having asked the question.

Which is probably often the case. So maybe dazzling is worth it
afterall.
Certainly breadth and depth of knowledge have value. But unless that
knowlege can prove useful, the likelihood of being paid just because
one has the knowledge is rather slim.

Perhaps, but in the meantime the interview was asking about minutiae,
which, while it shouldn't be swept under the carpet, is far from any
interview I've ever conducted and hence IMO missed much of the point
if it's the sole factor (of course, nobody said it was this time, but
I have heard of it being so).
 
G

Greg Comeau

Alf said:
* Greg:
[snipped rambling]

And since the question didn't mention empty subobjects or derived classes - it
mentioned only an empty class - the best thing to do - if the job is of
any interest at all - is to give the absolute answer the questioner
wants and save the lecture for another day.

I think you should have saved those lectures you've given here... Why not
just admit you were wrong instead of huffing & puffing? Anybody can make a
mistake.

I certainly make my share of mistakes. You need only look in my source
code to find some. But no one has made a mistake here. Persenaama's
answer does not lose points because it is incorrect. It loses points
because it is the wrong answer to give in response to that interview
question. It doesn't address the question directly, it muddies the
waters and the interviewer may not even know as much. Granted, it may
be unjust to lose points for an otherwise factually correct answer, but
this thread after all is supposed to be helping the original poster
learn how to answer C++ interview questions effectively. And we are
doing a disservice to the original poster if we are claiming that his
answer of "one" was incorrect and that it really should have been
"zero". That claim is simply misleading.

In a C++ interview, the candidate has to do more than simply be
correct. The candidate has to answer correctly the questions asked. On
USENET we can pick and choose which C++ questions to answer, a
candidate being interviewed does not have that luxury - and for some
obviously making such an adjustment is more difficult than it is for
others.

Let's say you are right. Ignoring that for the moment, I'm
curious what is the shortest possible correct answer you could
provide to the question?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top