some questions . please give feedback

Discussion in 'C++' started by free2cric, Oct 17, 2005.

  1. free2cric

    free2cric Guest

    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.

    free2cric, Oct 17, 2005
    1. Advertisements

  2. free2cric

    Greg Guest

    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.
    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.
    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.
    Its size is greater than zero bytes.
    Declare its constructor private and provide a factory method to
    instantiate instances of the class.
    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, Oct 17, 2005
    1. Advertisements

  3. free2cric

    Zara Guest

    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
    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
    Make all constructors private
    head node: pointer to previous is 0 (or some other value meaning
    tail node: pointer to nect is 0 (or some other value meaning end)
    Zara, Oct 17, 2005
  4. free2cric

    persenaama Guest

    4. what is the size of empty class and its object?
    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;
    persenaama, Oct 17, 2005
  5. free2cric

    Klein Guest

    No, my result is foo size is 1
    My environment: Visual Studio .Net 2003
    Klein, Oct 17, 2005
  6. free2cric

    Greg Guest

    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

    Greg, Oct 17, 2005
  7. * 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.
    Alf P. Steinbach, Oct 17, 2005
  8. free2cric

    Greg Guest

    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, Oct 17, 2005
  9. * Greg:
    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
    Alf P. Steinbach, Oct 17, 2005
  10. free2cric

    Greg Guest

    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

    Greg, Oct 17, 2005
  11. * Greg:
    All this evasive action and lying even more about Persenaama won't save you,

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

    * Greg -> :
    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.
    Alf P. Steinbach, Oct 17, 2005
  12. free2cric

    Greg Guest

    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, Oct 17, 2005
  13. * Greg:
    See Persenaama's correction to your earlier message.

    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.

    Admit your error, Greg.

    And do stop lying about Persenaama.
    Alf P. Steinbach, Oct 17, 2005
  14. free2cric

    persenaama Guest

    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
    persenaama, Oct 17, 2005
  15. free2cric

    persenaama Guest

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

    I did not make such claim! That's bullshit!

    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.

    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).
    persenaama, Oct 17, 2005
  16. free2cric

    persenaama Guest

    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. :)
    persenaama, Oct 17, 2005
  17. free2cric

    Kai-Uwe Bux Guest

    Greg wrote:

    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:


    An object is a region of storage. ...


    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


    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 17, 2005
  18. free2cric

    Greg Comeau Guest

    I agree it's often important to cut through the chase, but
    I wouldn't be convinced the question asked for an absolute answer.
    That's what he said.
    If it matters, most modern compilers do support it.
    Maybe. What if a question is bogus? What if the question is
    intended to trick? A person can respond w/o lectures.
    Which is probably often the case. So maybe dazzling is worth it
    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).
    Greg Comeau, Oct 18, 2005
  19. free2cric

    Greg Comeau Guest

    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?
    Greg Comeau, Oct 18, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.