Thread: Free a pointer

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    Free a pointer

    Hi all!
    I am an new bie!
    I have an error, could you should why it wrong and how to fix it!
    My sample code:
    Code:
    typedef struct _pointer_note{ 
    	char name[10];
    	char *loai;
    	int  data;
    	float *stack_next;
    }pointer;
    
    int main()
    {
     pointer p1; //declare a  pointer type variable
     p1.loai = (char*)malloc(sizeof(char)*256);//
     strcpy(p1.loai,"hello world");
     // Do some thing
    free(p1.loai);//free a pointer
    return 0;
    }
    When run program, an error "DAMAGE: after normal block(#42)"

    Help me!
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So how much code have you commented out with the "// do some thing" ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Quote Originally Posted by Salem View Post
    So how much code have you commented out with the "// do some thing" ?
    I do not add anything at this. My code really like this
    Code:
     pointer p1; //declare a  pointer type variable
     p1.loai = (char*)malloc(sizeof(char)*256);//
     strcpy(p1.loai,"hello world");
     free(p1.loai);//free a pointer
    Thanks!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I've no idea why that program would cause that to happen. It looks OK and works as expected here.

    The message you get suggests that you overwrote some memory, for example copying more than 10 characters to the name.

    Are you just compiling the program, or are you doing something like "rebuild all".
    Just occasionally, doing a "rebuild all" fixes things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Check if you included stdlib.h

    remove the (char *) cast from malloc and see if your compiler warns you that there is no prototype for malloc.

    On what platform do you work?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Thanks Salem!
    I think error in complie envoriment, because when I write a sample program and complie in Turbo C 3.0, it run well but when I complie in VC++ 6.0, it appear this error!

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    nemo3110, Seriously get with the times. Turbo C 3.0 was released in 1991, That's at least 17 years.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Works for me in VC6.

    Besides, if it really is bug-free then it should work with all compilers, not just a selection.

    Are you absolutely sure you're pasting the code you're running, and not just writing it out again from memory based on what you think you wrote (we get that a lot here).

    Because if you really have
    strcpy(p1.name,"hello world");

    and not as you claim
    strcpy(p1.loai,"hello world");
    then I can easily believe that you'd get the message you're seeing.

    More specifically, I also get "Damage after..." when I change the 256 to be something less than the length of the "hello world" string, say just 10 bytes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    did you try my suggestion? removing the (char *) cast from malloc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM