Thread: Memory quiz, question 2

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Memory quiz, question 2

    Hi,

    http://www.cprogramming.com/ans/ansmem.html

    Question 2 is:

    #


    Question #2
    # Assuming that myObj is less than 1000 bytes, is there anything wrong with this code?

    char x[1000];
    myObj *obj = reinterpret_cast(x);

    new (obj) myObj;]

    a) Nope, it works fine
    b) Yes, there could be byte alignment issues
    c) Yes, the syntax for calling new is incorrect
    The correct (but incorrect) answer is b).

    1. I think the reinterpret_cast need the template type argument (at least msvc complains if it's not given), true?

    2. ']' is a typo in line 4, true? If so, could someone correct it?

    3. What header file needs to be included to call the placement new operator?

    4. Why exactly could there be byte alignement issues? If "myObj is less than 1000 bytes", doesn't that mean "it is less than 1000 bytes, including any possible padding bytes (what's what the c++ sizeof operator understands by size)"?

    Thank you!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pheres View Post
    The correct (but incorrect) answer is b).
    I would like to say it's b).
    Remember, this is essentially what all C programmers do with malloc.

    1. I think the reinterpret_cast need the template type argument (at least msvc complains if it's not given), true?
    Yes, C++ is a strongly typed language, so char* != MyObj*, hence a cast is necessary.

    2. ']' is a typo in line 4, true? If so, could someone correct it?
    Yes, it is. Just remove the "]".

    3. What header file needs to be included to call the placement new operator?
    Just <new>.

    4. Why exactly could there be byte alignement issues? If "myObj is less than 1000 bytes", doesn't that mean "it is less than 1000 bytes, including any possible padding bytes (what's what the c++ sizeof operator understands by size)"?
    There will be no alignment issues if the entire sizeof(MyObj) [this also assumes that it includes all possible padding] fits inside x, because the compiler can (and will) lay out the object as if allocated with new (not placement).
    However, there may be alignment issues if x isn't aligned on a proper address. If you allocate a block of char using new, then use placement new, there would be no alignment issues.
    Last edited by Elysia; 06-08-2008 at 05:23 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could also write the same thing without a cast:
    Code:
    char x[1000];
    MyObj* obj = new (x) MyObj;
    Couldn't the potential alignment issue be that x itself doesn't begin at a word border?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When you mention it, yes, I suppose that's right.
    You'd need an aligned address, preferably from new and then construct something in place.
    I'll update the post to avoid confusion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    So the only possible problem is the buffer address does not start at a multiple of the size of a machine word? What kind of behavior could be caused by that? A little example would be nice.

    Quote Originally Posted by Elysia View Post
    Yes, it is. Just remove the "]"
    I can't, some site administarator has to do it. That's what I meant by "could someone correct it?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  2. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  3. Another Question...memory allocation
    By quickclick330 in forum C Programming
    Replies: 4
    Last Post: 12-12-2007, 04:25 PM
  4. Design question
    By MacGyver in forum C Programming
    Replies: 9
    Last Post: 05-17-2007, 02:36 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM