Thread: free

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    free

    how do you tell if free works, I was just curious becuase I have no way of telling if free(first) is just wasting a line of code or actually freeing it. I know free returns no value so that doesn't help.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    nevermind.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    What are you going to say for this code

    Please reply me friend for this code.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
    	char *message;
    	message=(char*)malloc(20);//allocate some memory 20 is just random..........
    	gets(message);//see to that it should not > 20
    	printf("\nBefore freeing memory first character is %c\n",*message);	
    	free(message);
    	printf("\nAfter freeing the memory first character is %c\n",*message);
    	printf("\n%c\n",*message);	
    }
    Saravanan.T.S.
    Beginner.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >message=(char*)malloc(20);
    The cast isn't needed. You also need to check to see that malloc succeeded.

    >gets(message);
    gets is *never* needed, use fgets instead.

    >printf("\nAfter freeing the memory first character is %c\n",*message);
    >printf("\n%c\n",*message);
    Bang! You're dead. Accessing memory that you don't own is undefined behavior. When you called free on message, you signed a contract saying that you'll never dereference that pointer again until you point it to memory you own.

    >main()
    int main ( void ), and remember to return a value at the end.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Hi

    What prelude told is correct but it is not understood by chrismiceli.
    To make it sure what prelude told it did that.
    Saravanan.
    Saravanan.T.S.
    Beginner.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Accessing memory that you don't own is undefined behavior.
    Just clarifying on this point...

    Undefined behavior means it's not guaranteed to work at all. Chances are, that memory you just free()'d won't be messed with if you access it immediately afterwards. But that memory is no longer yours. It could change at any time, without your knowledge. Accessing it could cause a segmentation fault, which crashes your program. Your program could allocate that space back again for some different purpose. That means that two sets of data share memory. This is never a good thing. (Ignore intentional sharing through unions.)

    In short, there's no advantage and plenty of disadvantages to doing anything labeled 'undefined behavior'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM