Results 1 to 4 of 4

Thread: placement new question

  1. #1
    Join Date
    Apr 2007
    Posts
    44
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default placement new question

    Hello all.

    I need to allocate memory at address 0x1051C5180 for about 4096 byte.
    Please advice me.

    i try this code but failed to compile

    Qt Code:
    1. char *address =(char*)0x1051C5180;
    2. unsigned char *ptr = new (address) unsigned char[4096];
    To copy to clipboard, switch view to plain text mode 

    Thank.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: placement new question

    Quote Originally Posted by Teerayoot View Post
    I need to allocate memory at address 0x1051C5180 for about 4096 byte.
    I'm afraid there is just NO way to allocate memory at a pre-determined location unless your playing with very low-level and platform dependent stuff (or maybe developping a kernel yourself, whatever...). Placement new is nothing but a convinient way to call a constructor on a pre-allocated memory area... It's not because you want to use an address that it will be free for use (it might even be out of range and cause a segmentation fault...). The best you can do is allocate memory through malloc(), calloc(), mmap()... and feed it to placement new...
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    Teerayoot (23rd May 2007)

  4. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: placement new question

    Are you sure you need to allocate memory at that location?
    If you need to use a fixed address it is probable that the memory is already allocated/usable. If so, just assign it to a pointer and access it.
    If you should need to construct an object without allocating any memory, then placement new is what you want.

    (I've never seen memory to be allocated at a fixed address. Not sure if that is possible at all.)

    So, assuming you have the address in location:
    void *location = ...;
    MyObject * pObj = new (location) MyObject(...);

    HTH
    Christoph

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: placement new question

    Quote Originally Posted by caduel View Post
    (I've never seen memory to be allocated at a fixed address. Not sure if that is possible at all.)
    You can allocate a block of memory and ask the system to assign it to a desired virtual address in your process memory space. So if that is what is required, it is possible, but hardly usable. If the thread starter means the physical address of the memory then only the os kernel can do that. Userspace processes don't have access to physical addresses.

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  2. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34
  3. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.