Sizeof problems(interview questions)

Discussion in 'C++' started by maxmus@gmail.com, Jul 8, 2005.

  1. Guest

    Class A
    {
    int a;
    };

    cout<<"the size of A is "<< sizeof(A)<<endl;

    Q: what is the result?

    Now Add in a function, int getVal();

    What's the result now?

    Explain why
    , Jul 8, 2005
    #1
    1. Advertising

  2. Artie Gold Guest

    wrote:
    > Class A
    > {
    > int a;
    > };


    Syntax error. What is `Class'? ('class', we know; C++ is case sensitive.)
    >
    > cout<<"the size of A is "<< sizeof(A)<<endl;


    Assuming that `A' has been declared correctly above, the answer is
    `whatever sizeof(A) *says* it is.
    >
    > Q: what is the result?
    >
    > Now Add in a function, int getVal();
    >
    > What's the result now?


    Similarly.

    >
    > Explain why
    >

    Because doing your own homework is something called, well, not cheating.

    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Jul 8, 2005
    #2
    1. Advertising

  3. Jack Klein Guest

    On 7 Jul 2005 18:25:02 -0700, wrote in comp.lang.c++:

    >
    > Class A
    > {
    > int a;
    > };
    >
    > cout<<"the size of A is "<< sizeof(A)<<endl;
    >
    > Q: what is the result?


    What did you answer? The only answer that the C++ language provides
    is that sizeof(A) is greater than or equal to sizeof(int). In
    numbers, 1 or greater.

    > Now Add in a function, int getVal();
    >
    > What's the result now?


    The answer to the first question is equally accurate for the second,
    still >= sizeof(int), still >= 1.

    > Explain why


    Because. Further explanation is pointless, as is trying to impress
    whoever it is came up with this as a useful question.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 8, 2005
    #3
  4. Guest

    Come on. I am providing a question for people to practise/discuss, not
    asking for the answer for myself.

    If you feel confident, just give your answer instead of laughing at
    people.
    , Jul 8, 2005
    #4
  5. Guest

    You haven't answered the question yet.
    Greater or equal to sizeof(int)?
    Does the later "adding " affect the result at all?
    , Jul 8, 2005
    #5
  6. Me Guest

    wrote:
    > class A
    > {
    > int a;
    > };
    >
    > cout<<"the size of A is "<< sizeof(A)<<endl;
    >
    > Q: what is the result?


    Prints out a number >= sizeof(int)

    > Now Add in a function, int getVal();
    >
    > What's the result now?


    Prints out the same thing.

    > Explain why


    A is still POD.
    Me, Jul 8, 2005
    #6
  7. Jack Klein Guest

    On 7 Jul 2005 19:33:38 -0700, wrote in comp.lang.c++:

    > You haven't answered the question yet.
    > Greater or equal to sizeof(int)?
    > Does the later "adding " affect the result at all?


    Usually I don't even answer people who can't even be bothered to quote
    correctly. If you make the choice to use the broken Google Groups
    interface instead of a proper newsreader, it is still up to you to
    quote properly.

    And yes, I answered both questions. Learn how to read. And how to
    quote.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jul 8, 2005
    #7
  8. Me wrote:
    > wrote:
    >> class A
    >> {
    >> int a;
    >> };
    >> [..]

    >
    > A is still POD.


    No, it ain't. 'a' is private, and that makes it non-POD.

    V
    Victor Bazarov, Jul 8, 2005
    #8
  9. David White Guest

    wrote:
    > You haven't answered the question yet.


    Well, answers were given. I assume you asked the questions because you don't
    know the answers, so how do you know that the answers you were given are
    inadequate? What sort of answers would satisfy you?

    DW
    David White, Jul 8, 2005
    #9
  10. Me Guest

    Victor Bazarov wrote:
    > Me wrote:
    > > wrote:
    > >> class A
    > >> {
    > >> int a;
    > >> };
    > >> [..]

    > >
    > > A is still POD.

    >
    > No, it ain't. 'a' is private, and that makes it non-POD.


    Bleh. Well, so anyway, the reason why adding a non-virtual function
    can't add space is 9.2/12 "so might requirements for space for managing
    virtual functions (10.3) and virtual base classes (10.1)." neither of
    which apply here.
    Me, Jul 8, 2005
    #10
  11. Guest

    wrote:
    > You haven't answered the question yet.
    > Greater or equal to sizeof(int)?
    > Does the later "adding " affect the result at all?


    He DID answer your question. Let me reformulate, since you don't seem
    to understand. The C++ language says this:

    - sizeof(int) >= 1
    - The size of any class is greater or equal to size of every member in
    the class.
    - If the class has any virutal methods then its size may be greater.

    That's it. There's no way of knowing more precisely what sizeof(A) is.
    If someone at an interview expected you to provide a more precise
    answer without specifying which platform they are talking about then
    they are clueless and you shouldn't bother trying to get the job they
    are offering.


    Bart.
    , Jul 8, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,307
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,391
    Lawrence Kirby
    May 12, 2005
  4. Vijay
    Replies:
    2
    Views:
    574
    Öö Tiib
    May 7, 2010
  5. reema
    Replies:
    0
    Views:
    244
    reema
    Aug 26, 2008
Loading...

Share This Page