Thread: Something to watch for!

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Something to watch for!

    Passing pointers to functions means that you can alter the data that it points to, fine!

    Just don't expect to re-assign this pointer in a function and
    expect it to be pointing there when you return from the function.

    That's because even though you can alter the data, you cannot
    alter where the pointer is pointing to.
    Reason being that a copy of the pointer is passed to the function.

    Eg.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void func(char* name) 
    {
    	name = 0;
    }
    
    int main()
    {
    	char *name = (char*)malloc(30);
    	strcpy(name,"TEST");
    	printf("Before func() : %s\n", name);
    	func(name);
    	printf("After func()  : %s\n", name);
    	free(name);
    	return 0;
    }
    If you want to alter the pointer then do it this way:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void func(char** name) 
    {
    	*name = 0;
    }
    
    int main()
    {
    	char *name = (char*)malloc(30);
    	strcpy(name,"TEST");
    	printf("Before func() : %s\n", name);
    	func(&name);
    	printf("After func()  : %s\n", name);
    	free(name);
    	return 0;
    }
    Just something good to know!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    This is probaly a bad example because 'name' is not pointing to the allocated memory anymore!

    I just wanted to bring the point across!

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Same example fixed:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void func(char** name) 
    {
    	char* temp;
                    temp = *name;
                    *name = 0;
                    free(temp);
    }
    
    int main()
    {
    	char *name = (char*)malloc(30);
    	strcpy(name,"TEST");
    	printf("Before func() : %s\n", name);
    	func(&name);
    	printf("After func()  : %s\n", name);
    	free(name);
    	return 0;
    }
    Now all the memory is being freed!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Same example fixed
    Are you sure.... I think not

    >Now all the memory is being freed!
    Yes, but you've created another bug.... can you see it?
    I'll let you ponder on it for a while!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Wink

    Well i don't think i'll find it now if ever!

    Please "point" it out.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Oops, i forgot to do this in the original post!

    Code:
    void func(char** name) 
    {
    	char* temp;
                    temp = *name;
                    *name = (char*)malloc(50);
                    strcpy(*name, "This is a new string");
                    free(temp);
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, let's see..

    In func(), you free()'d the memory pointed to by temp. In doing this, you free()'d the memory pointed to by name. This was because you assigned:
    >temp = *name;
    and therefore the two are now the same.

    We're OK so far. When you return from func(), the first thing you do is use name in printf(). Then you free() it. But, it's already been free()'d so how can you safely do either of these things?

    Now, thanks to you setting the value to name to 0:
    >*name = 0;
    the damage is limited. What you have done here, is NULL'd the pointer (although it should be done: *name = NULL). This means that the call to free() in main() is OK, because free()'ing a NULL pointer is acceptable. I am not sure if passing a NULL pointer to printf() is defined behaviour (my compiler doesn't complain though).

    After all that waffle on my part, there wasn't actually a bug as such, just some things which made the behaviour tricky to monitor. Once you have free()'d a pointer, try not to use it again without assigning it another address.

    It's obvious your code is written purely to highlight the pitfulls of pointers and dynamic memory, and I really shouldn't expect you to bullet proof it at this stage..... I guess I'm just being picky.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. watch variable??
    By haochao in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2008, 12:34 PM
  2. Watch for mouse events
    By belhifet in forum Linux Programming
    Replies: 2
    Last Post: 04-10-2007, 11:12 AM
  3. watch this
    By The Brain in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-13-2005, 11:23 PM
  4. Live 8! Watch Now
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 07-09-2005, 04:33 PM
  5. Some bottleneck in coding digital watch simulator
    By megablue in forum C Programming
    Replies: 6
    Last Post: 08-29-2004, 09:39 AM