Wim
Wim

Reputation: 181

Free pointer after affectation to another one

I have the following source code example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
     int *ptr1,*ptr2;       // two pointers of type int

  ptr2=ptr1;
  ptr1=NULL;

  if(ptr2==NULL)
     printf("OK Clean\n");
   else
     printf("KO not clean\n");

}

I would like to know why the pointer ptr2 is not NULL, I get always KO not clean? If I affect ptr1 to ptr2 and make ptr1 NULL , ptr2 in this case must be NULL? How can do to get a NULL in ptr2?

Upvotes: 0

Views: 147

Answers (6)

user694733
user694733

Reputation: 16043

There are 2 problems in here.

  • Using ptr2 = ptr1 is invalid.
  • Why does assignment to ptr1 not change ptr2?

Invalid assignment

ptr1 uninitialized and value is garbage. Assigment to ptr2 leads to undefined behaviour. The program is broken.

To fix this, initialize ptr1 with valid address:

int a = 2;
int * ptr1 = &a; // Initialize ptr1
int * ptr2;
ptr2 = ptr1; // Assigment OK, ptr1 has valid address

Assigment to ptr1 not changing ptr2

ptr1 and ptr2 hold same value, but have no connection to each other. It's same as

int a = 1, b = 2;
a = b; // a is now 2
b = 0; // a is still 2

If changing value of ptr1 should reflect on ptr2, it should be pointer to pointer:

int * ptr1 = &a;  
int ** ptr2;
ptr2 = &ptr1;
ptr1 = NULL;
if(*ptr2 == NULL) // Test value of ptr1 through ptr2

Upvotes: 1

Philipp Murry
Philipp Murry

Reputation: 1682

I think you are confusing the value of a pointer (the address it stores) with the value it points to. When the two pointers ptr1 and ptr2 point to the same address, then changing the value that address points to will affect both pointers. But the addresses both pointers store store (the addresses they point to) can still be different! So be careful to distinguish the value of a pointer (it's address) and the value it points to.

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148880

The assignement ptr2 = ptr1 only says that at that time ptr2 and ptr1 point to same memory. But as you later modify the pointer ptr1 they are different again.

It would be different in you were changing the pointed value :

int i = 5;
int * ptr1, *ptr2;
ptr1 = ptr2 = &i;
*ptr2 = 3;

Then *ptr1 also has value 3.

Upvotes: 0

scorpiozj
scorpiozj

Reputation: 2687

Like @Bathsheba refers, this has undefined behaviour problem. But I think @Issam want to know why the ptr2 is not changed.
For the code belowing:


    int *ptr1,*ptr2;       // two pointers of type int
    int num = 2;
    ptr2=ptr1 = #
    ptr1 = NULL;
    if(ptr2==NULL)
        printf("OK Clean\n");
    else
        printf("KO not clean\n");

The result is still KO.
The point equal means the 2 points has the same memory address, and when ptr1 points to NULL, it will not have effect on ptr2.

Upvotes: 1

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

you are assigning the address of ptr1 to pointer ptr2. SO this is the reason that is not null.

ptr2=ptr1;

You want to make the pointer as null. You can assign like this.

ptr2=ptr1=NULL; or
ptr2=NULL;

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234665

This is undefined behaviour: you're assigning ptr2 to an uninitialised value ptr1.

Writing ptr1 = ptr2 = NULL; would be safe. NULL is the only literal value that you can assign to a pointer, without using the address-of operator &. (As a further remark, you shouldn't assume that NULL is related to memory address 0: the standard doesn't).

Upvotes: 1

Related Questions