Thread: Strage memory behavior

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

    Strage memory behavior

    Any one has any idea why this code if causing glibc errors?
    It seems that for some reason casting the buffer to something and then using it,
    would mess up with the delete comming up later. If I change that from dynamic
    allocation to simple static allocation (just char a[sizeof(s)]) and remove the delete
    at the end, everything is fine.

    Code:
    class A {
       public:
          A(){}
          ~A() { std::cout << "I'm dying" << std::endl; }
    };
    
    struct s {
       A ty;
       int a;
       float b;
       bool c;
    };
                                                                  
    int main() {
    
       char *a = new char(sizeof(s));
       //char a[sizeof(s)];
       s *obj = (s*)a;
    
       obj->ty = A();
       obj->a = 5;
       obj->b = 6;
       obj->c = true;
    
       for (int i=0;i<sizeof(s);i++) {
          std::cout << "a[" << i << "]=" << *a++ << std::endl;
       }
    
       delete a;
       return 0;
     }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >   for (int i=0;i<sizeof(s);i++) {
    >      std::cout << "a[" << i << "]=" << *a++ << std::endl;
    You increment a several times, so a no longer points the the beginning of its allocated memory.
    Code:
    >   delete a;
    Then you try to delete a, but it's been incremented above. By the way, this should be:
    Code:
    delete []a;

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    > char *a = new char(sizeof(s));

    This allocates a single character with an initial value of sizeof(s). What you want is
    char *a = new char[sizeof(s)];

    And that's still undefined behaviour, due to reinterpretation cast. There might be some platforms out there that happily give you an alignment error, causing your application to crash. Note: while the block of memory allocated by the underlying operator new[]() is guaranteed to be suitably aligned for any object type, there is no such guarantee on the pointer return by new[] itself.
    It's also undefined behaviour because you're dereferencing a pointer to an uninitialized non-POD type. (Class A has a destructor, thus it's non-POD. Struct s contains an A, thus it's non-POD.)

    If you want raw memory, call operator new() directly. Don't allocate char arrays with new[] unless you actually want char arrays.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM