-4
//C - code 
int num
int *pi = # //1 
printf(" *pi=%d  and  pi=%d \n ", *pi, pi);  // p1
pi = #      //2
printf(" *pi=%d  and  pi=%d \n ", *pi, pi);  // p2

As for comment 2, I understand that we are assigning a valid address to the pointer pi but I don't understand what is happening in the line of comment 1 because because both the printf() statements p1 and p2 are doing the same job.

And in case of dynamic memory management with malloc(sizeof(datatype)) function, why the following is an invalid approach?

int *pi;
*pi = (int*)malloc(sizeof(int)); // terminates the program 

while the following:

int *ri = (int*)malloc(sizeof(int)); 

and

int *qi;
qi = (int*)malloc(sizeof(int));

works fine.

Please explain.

EDIT : The question marked as duplicate to this question doesn't talk about pointers and problems that we face during dynamic memory management.

log0
  • 2,206
  • 2
  • 14
  • 24

4 Answers4

2

You are getting confused between the use of * in the declaration of a pointer and the use of * as the dereferencing operator.

To understand better, while declaring, you can write int *p as (int*) p. This means, that p is of type 'pointer to an int'.

However, * is also the dereferencing operator, or the 'value at' operator. So, *pi = value at pi, i.e., the value stored at the location of num, i.e., the value of num.

int *pi;
*pi = (int*)malloc(sizeof(int));

crashes becuse you try to assign the result of (int*)malloc(sizeof(int)) to the location where pi is pointing. but as pi is not pointing anywhere (it is uninitialized), hence, it doesn't work.

On the other hand,

int *ri = (int*)malloc(sizeof(int)); 

works because you are making ri point to the location where the result of (int*)malloc(sizeof(int)); is stored.

int *qi;
qi = (int*)malloc(sizeof(int));

also works because you are making qi point to the location where (int*)malloc(sizeof(int)); is stored.

Amita
  • 944
  • 1
  • 8
  • 20
  • what is happening in the background in cases of int *pi = # And pi = # ? – log0 Nov 15 '16 at 09:36
  • When `pi = &num` , it means, pi is pointing to the location of num. When `*pi = &num` , it means the value at pi is the address of the location of num. or in other words, the location at which pi is pointing will contain the address of num. – Amita Nov 15 '16 at 09:41
1

You should understand what is a pointer.

A pointer should hold a valid memory address.

int *p;

Here p is a pointer which holds the memory address and *p is called dereferencing a pointer. In other words *p gives the value stored in the location p.

So during dynamic memory allocation

int *p = malloc(sizeof(int) );

Here pointer p is assigned the address returned by malloc().

int *p;

p = malloc(sizeof(int));

Again pointer is being assinged the value returned by malloc(). Note that I am not using *p because still memory is not allocated for my pointer p

int num;
int *p = #

Assignment is proper but before intializing the variable if you deference then you get erroneous results.

int num = 10;
int *p = #

Now *p gives 10.

Gopi
  • 19,784
  • 4
  • 24
  • 36
1
int *pi;
*pi = (int*)malloc(sizeof(int)); // terminates the program 

So you define a pointer variable without initializing it. It now contains an unspecified address. You then try and write to that unspecified address. That's undefined behavior according to the standard, and your run-time is kind enough to crash, so you'd know there's a problem.

int *qi;
qi = (int*)malloc(sizeof(int));

You again define a pointer variable, which again holds an unspecified address. But in the very next line, you assign to the variable some (probably valid) address. So all is well (so far).

So you you see, the two cases are different. In the first, you de-reference the pointer and assign to that (unspecified address), and in the second you assign to the pointer.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
-1

The last time I programmed in C was more than 10 years ago, but AFIK, your first example crashes, because you assign the result of the malloc() to *pi (you try to assign the result of malloc to the address pi currently points to. Since *pi was not initialized, this crashes your program).

Dieter Rehbein
  • 941
  • 8
  • 16
  • " you try to assign the result of malloc to the address pi currently points to"- Would you please elaborate it more. I had read this earlier too but could not understand. I think this is what I am looking for. – log0 Nov 15 '16 at 09:21