Re: place stl container object on shared memory
 
dbtouch wrote:
I am trying to place an STL container object on shared memory
  String* pStr=new(ptr)String(); // segment fault!!!
No, that's not where the segv is occurring.
  if (pStr) {
    delete pStr;
This is.  Delete doesn't just call the destructor, it also tries to free 
the memory.  That's not what you want here.  You just want to call the 
destructor.
   pStr->~string();
    printf("test is good\n");
  }
You seem to be new to C++, and have carried a lot of C with you.  I'm 
posting a modern C++ version of your code here, in the hope that it will 
help you find "the C++ way."  Good luck.
/* POSIX */
extern "C" {
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
}
/* C++ Standard */
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>
#include <string>
/* Dbtouch library code. */
namespace dbtouch {
     typedef void* void_pointer;
     template<std::size_t Size>
     void check_posix(long i, char const (&command_name)[Size]) {
         if (i == -1L) {
             throw std::runtime_error(
                     std::string( command_name ) +
                     std::strerror(errno) );
         }
     }
     template<int Mode, std::size_t Size>
     class shared_memory
     {
         typedef void const* void_const_pointer;
         typedef shmid_ds* shmid_ds_pointer;
         static int create() {
             int const shmid =
                 shmget(IPC_PRIVATE , Size , Mode | IPC_CREAT);
             check_posix(shmid, "shmget");
             return shmid;
         }
         static void_pointer attach(int shmid) {
             void_pointer const memory =
                     shmat(shmid, void_const_pointer( ), Mode);
             check_posix(reinterpret_cast<long>(memory), "shmat");
             return memory;
         }
         static void detach(void_pointer memory) {
             check_posix(shmdt(memory), "shmdt");
         }
         static void destroy(int shmid) {
             check_posix(
                     shmctl(shmid, IPC_RMID, shmid_ds_pointer( ))
                     , "shmctl");
         }
         int m_shmid;
         void_pointer m_memory;
       public:
         shared_memory( )
           : m_shmid( create() )
           , m_memory( attach(m_shmid) ) { }
         ~shared_memory() {
             detach(m_memory);
             destroy(m_shmid);
         }
         void_pointer get() {
             return m_memory;
         }
     };
}
/* Test of dbtouch library. */
/* Configuration. */
namespace {
     typedef std::string object;
     int const mode = 0644;
     std::size_t const size = 1024 * 1024;
}
void unchecked_main() {
     dbtouch::shared_memory<mode, size> buffer;
     if (object* const p = new(buffer.get()) object) {
         p->~object();
         std::cout << "test is good\n";
     }
}
int main() try {
     unchecked_main();
     return EXIT_SUCCESS;
} catch (std::exception const& x) {
     std::cerr << "std::exception: " << x.what() << '\n';
     return EXIT_FAILURE;
}