Thread: using new

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    using new

    How can a C++ developer use the placement new syntax to make new allocate an object of class SomeClass at a particular memory address stored in a pointer type variable named pmem?

    A. new SomeClass(pmem);
    B. new(pmem) SomeClass;
    C. new SomeClass pmem;
    D. new pmem SomeClass;
    E. new (pmem, SomeClass);

    Hmm. This confuses me a bit as I'm only use to seeing:
    Code:
    someClass *pmem = new someClass;
    But I'm rather sure A is correct as its a copy constructor. And the rest are incorrect.

    Can anyone confirm. Thx.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    B is correct and the others are not. You are apparently unfamiliar with placement new.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where did you get this question from? The word "allocate" is rather dangerous in this context since placement new is not used to allocate memory. Does your book mention anything on placement new? If not, you might want to search the web on "placement new".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    Quote Originally Posted by CornedBee
    You are apparently unfamiliar with placement new.
    I'm use to using malloc instead.

    Quote Originally Posted by laserlight
    Where did you get this question from?
    http://ikmnet.com/

    Quote Originally Posted by laserlight
    Does your book mention anything on placement new?
    Nope - was not aware of "placement new" until now.
    Last edited by coletek; 01-11-2009 at 12:29 PM.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    malloc and placement new have absolutely nothing to do with each other. In fact, they're pretty much orthogonal: malloc allocates memory, placement new constructs an object inside.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37
    Quote Originally Posted by CornedBee
    malloc and placement new have absolutely nothing to do with each other. In fact, they're pretty much orthogonal: malloc allocates memory, placement new constructs an object inside.
    Just found that out now. Thx peoplez.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by coletek View Post
    http://ikmnet.com/

    Nope - was not aware of "placement new" until now.
    So does that mean we're answering questions to help you get hired for a job you're not qualified for?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Written tests are given to determine job eligibility? How offensive. Aren't there degrees for that?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CodeMonkey View Post
    Written tests are given to determine job eligibility? How offensive. Aren't there degrees for that?
    Degrees don't tell you much other than you spent a lot of money for a fancy piece of paper. The real question is, did you learn anything when you were taking all those courses, or did you just cheat your way through...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed