CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jan 2008
    Posts
    5

    Unhappy malloc/free question

    My question is regarding C, specifically the gcc compiler.
    Essentially, I did the following:

    Code:
    void *ptr = malloc( (sizeof(int)*2) + (sizeof(char)*STRLEN) );
    void *p = ptr;
    *(int*)p = 1;
    p += sizeof(int);
    *(int*)p = 2;
    p += sizeof(int);
    strcpy((char*)p, "hello");
    
    // did some stuff
    
    free( ptr );
    Did I free the memory properly?
    I know the first integer is freed, but I accidentily printed the values of all these locations and the second integer's and string's values were still intact.
    Last edited by c.reilly; February 18th, 2008 at 06:28 AM. Reason: Added frowny face
    Visual C++ 2008 Express Edition
    Windows Vista

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured