Thread: malloc address

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    malloc address

    I am trying to print the memmory address that malloc allocates
    AND print the memory address that has been freed.

    how do I do that?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	char *pt;
    
    	pt=(char*)malloc(40);
    	if (pt==NULL)
    		{
    		perror("dynline1");
    		exit(1);
    		}
    
    	strcpy(pt, "abcdefghijklmnopqrstuvxyz");
    	puts(pt);
    
    	free(pt);
    
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Its been awhile, but maybe:
    Code:
    printf("Address is %p.\n",pt);
    This will let you print out the address stored in the pt variable.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	char *pt;
    
    	pt=(char*)malloc(40);
    	if (pt==NULL)
    		{
    		perror("dynline1");
    		exit(1);
    		}
    
    	strcpy(pt, "abcdefghijklmnopqrstuvxyz");
    	printf("%p", pt);
    
    	free(pt);
    
    	return 0;
    }
    In C you don't have to cast malloc().

  4. #4
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Thanks......and how can I print the address freed after free(pt)

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    The pointer that you allocated memory to is also the one that you free'd.
    Code:
    char *pt;
    pt = malloc(40);
    printf("%p",pt);
    free(pt);         //You've already printed it's address

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Some additional information...

    http://www.eskimo.com/~scs/C-faq/q7.21.html
    When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller remains unchanged, because C's pass-by-value semantics mean that called functions never permanently change the values of their arguments.

    A pointer value which has been freed is, strictly speaking, invalid, and any use of it, even if is not dereferenced can theoretically lead to trouble, though as a quality of implementation issue, most implementations will probably not go out of their way to generate exceptions for innocuous uses of invalid pointers.

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by Dave_Sinkula
    Some additional information...

    A pointer value which has been freed is, strictly speaking, invalid, and any use of it, even if is not dereferenced can theoretically lead to trouble, though as a quality of implementation issue, most implementations will probably not go out of their way to generate exceptions for innocuous uses of invalid pointers.
    Which is why it's so very good to NULL your pointers after you free them.
    Code:
    char* ptr = malloc(50);
    free(ptr);
    ptr = NULL;

  8. #8
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    In C you don't have to cast malloc().
    Actually this is not correct. Using type coercion on malloc() has not so much to do with C as it has to do with the compiler you are using.

    The older the compiler, the more likely it flag a 'pointer mismatch' unless you've coerced.

    ---

    It does ABSOLUTELY NO HARM to use type coercion. If anything, it allows for a wider portability to older platforms or compilers because nothing is left to chance.
    It is not the spoon that bends, it is you who bends around the spoon.

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Sayeh
    Actually this is not correct. Using type coercion on malloc() has not so much to do with C as it has to do with the compiler you are using.

    The older the compiler, the more likely it flag a 'pointer mismatch' unless you've coerced.

    ---

    It does ABSOLUTELY NO HARM to use type coercion. If anything, it allows for a wider portability to older platforms or compilers because nothing is left to chance.
    dont waste your time decapitating your programs for pre-ansi-c89 compilers.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. address out of bounds in sockets program
    By newbie_socketsp in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-05-2008, 06:41 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM