Thread: Problem with pointers

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    18

    Problem with pointers

    Hello,
    I was doing some experiments with pointer and wrote the following code :-
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int *p;
    	int *q;
    
    	p = (int*) malloc (sizeof(int) * 3);
    
    	printf ("Address: &p = %u, p = %u\n", &p, p);
    
    	p[0] = 10;
    	p[1] = 20;
    	p[2] = 30;
    
    	q = p;
    	
    	printf ("&q = %u, q = %u\n", &q, q);
    
    	free(p);
    	
    	printf ("After Freeing p:\n&q = %u, q = %u\n", &q, q);
    
    	printf ("%d %d %d\n", q[0], q[1], q[2]);
    
    	return 0;
    }
    I have allocated the memory to pointer p for 3 integers and then i assigned them values 10, 20, 30. Now for pointer q, I set it to point to the address pointed by p. Now I freed pointer p's allocated memory. After freeing, q will still point to the same address. Now when I print the values again through q, the first value q[0] comes out to be 0 and remaining the same. Why does it happen? Any Idea.

    Thanks and Best Regards,
    Aakash Johari

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > free(p);
    As soon as you do this, the q pointer becomes invalid as well.

    There is nothing special about your observation (except that the code is broken). It could equally well have produced the same results, or resulted in a segfault.
    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 2011
    Posts
    18
    In which way, it becomes invalid. Its still pointing to the same address. And after running the whole code, I found the output:

    Code:
    0 20 30
    Means, it just cleared out the first integer space and did not do anything with the other allocated memory part.

    And please explain the term "Code Broken".

    Thanks and Best Regards,
    Aakash Johari

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's invalid because the memory you had allocated is no longer reserved for your program after you free it. It doesn't matter if you haven't modified the memory you're pointing at anymore, anything could be changing memory at that location. Referencing the memory after you've freed it is never, ever something you want to do. It's described as "undefined behavior" by the C standard and trying to determine what it's doing is just a waste of time. You can't define something that's designed to be undefined.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    q points to the same memory address, yes. But the memory at that memory address is no longer yours, since you gave it back; it now belongs to some other process on the machine.

    And any code that attempts to access memory which it is not entitled to access is broken (it may compile, but that doesn't mean it's right).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by aakashjohari
    In which way, it becomes invalid. Its still pointing to the same address.
    Yeah, but that address no longer corresponds to an object (or array thereof) that exists.

    Quote Originally Posted by aakashjohari
    Means, it just cleared out the first integer space and did not do anything with the other allocated memory part.
    From the point of view of your program, the memory is no longer allocated, so talking about "the other allocated memory part" is nonsense.

    Quote Originally Posted by aakashjohari
    And please explain the term "Code Broken".
    Your code is incorrect. Accessing q[0], q[1] and/or q[2] after free(p) results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    This could be a quirk of the os or compiler because of the way they manage dynamic memory.
    After free(p) there's no guarantee if the memory is in the process' address space or has been released.
    So in this aspect what you are doing is undefined and at best it is implementation-dependent.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    18
    Thank you very much.

    I got it.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aakashjohari View Post
    Thank you very much.

    I got it.
    Think of it this way... You burned down the house with both cars still in the driveway. The cars may or may not be damaged by the fire.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    18
    Quote Originally Posted by CommonTater View Post
    Think of it this way... You burned down the house with both cars still in the driveway. The cars may or may not be damaged by the fire.
    Sorry, I couldnt get you.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The house is analogous to the memory location.... the cars are the pointers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointers and objects.
    By ihorse in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2010, 04:40 PM
  2. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  3. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  4. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  5. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM