user1624400
user1624400

Reputation: 395

do I need to allocate memory to an initialized pointer?

int a = 10;    
int *p = &a; 

*p = 20;  /* Is this a valid statement? */

I understand that if I do int *p; and then if I do *p = 10 , it is invalid because I have not assigned any memory to p. However, I was wondering if initializing a pointer to some address allocates memory to that pointer or not?

Upvotes: 1

Views: 3184

Answers (7)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

I was wondering if initializing a pointer to some address allocates memory to it or not?

It does NOT allocate any memory. Because p a points to an already allocated memory

int a = 10;    // a is statically allocated and value 10 is assigned.
int *p = &a;  // p is pointed to address of a. 
*p = 20;  // at this point p points to that statically allocated memory

Upvotes: 6

ams
ams

Reputation: 25579

A pointer is just a number. It has a special type to make sure it can represent the right range of numbers, and to aid program understanding, but basically it's just a number.

The number represents a memory address. You can't just make those up: you need to get a meaningful address from somewhere.

You can:

  • Take the address of an exiting variable: p = &a; or
  • Allocate some memory from the "heap": p = malloc(size)

That's it, once you have the address of some memory, you're done! You can now dereference the pointer using the * operator anywhere you like: *p = somedata.

Upvotes: 3

Tony Delroy
Tony Delroy

Reputation: 106106

int a = 10;    
int *p = &a; 

I was wondering if initializing a pointer to some address allocates memory to that pointer or not?

No. The a and p variables need to be allocated on the stack, or if they're outside a function they'd be allocated memory as global variables. The compiler does this for you automatically, even if you just have...

int a;    
int* p; 

...without the = <expression> to specify their initial value. What happens is basically that at some address in memory, a number of bytes (likely 4 or 8) will be reserved to store the value you're identifying as a, and at another address further memory (likely 4 or 8 bytes) will be reserved to store the value you call p.

The initialisation of p with &a simply copies the numeric address of a into the memory for p... it does not cause the allocation of either[1], nor move their memory.

It may help to visual it like this, using some made-up addresses...

MEMORY-ADDRESS            CONTENTS           NAME
1000                      10                 a
2000                      1000               p

Here, p "points" to a because the contents of p's memory hold a's address. But the addresses of a and p are chosen by the compiler irrespective of any initialisation or other changes to their value.

I think what's confusing you here is that we do often allocate memory when using pointers... something like:

p = new int;

What this does is find some memory for an int dynamically at runtime, loading the address of that memory into p so we can start referring to it as p and using it to store int values. When we're finished with it we can delete p to return the memory to the system, such that it may be recycled and used when another new is done. This type of allocation needs to be explicitly performed in your code (or in the code of some library function you call).

[1] - an optimiser may choose not to assign actual memory for a and/or p - using a CPU register instead, but that won't affect the functional behaviour of your program (it may affect the preformance).

Upvotes: 4

Manlio
Manlio

Reputation: 10865

However, I was wondering if initializing a pointer to some address allocates memory to it or not?

What do you mean by "allocates memory to it"? When you create a pointer you are allocating (statically) its memory, or, in other words, the memory needed for it to keep existing.

Apart from that, there is the memory where the pointer points to. When you say int a = 10 you are creating an int and allocating a memory space large enough to contain it.
Saying then p = &a means: "my pointer must point to the memory address where a is stored". Obviously you already allocated a, so you can safely use *p, but in general this is not true and you may have an uninitialized pointer pointing to some illegal address.

Upvotes: 1

Omkant
Omkant

Reputation: 9204

int a = 10;    
int *p = &a; 

*p = 20;  /* Is this a valid statement? */

The answer is YES in this particular case.

But when you do like this :

int *ptr;  //declaration of pointer variable
*ptr = 20;  // It means you are assigning value 20 to the variable where `ptr` points to.

But actually ptr is not pointing to anywhere, basically it means it has indeterminate value.

so doing *ptr = 20 will put the value 20 to the memory address pointed to by ptr. So it's called Undefined bahavior

In you case it's valid because &a is valid memory location and p started pointing to that variable when we do p= &a.

so *p = 20 means actually changing or assigning the value of a using the pointer p.

Upvotes: 5

Hossein Mobasher
Hossein Mobasher

Reputation: 4472

No, not needed. Because your pointer points to the address of a variable. So, when you use *p = 20, the value of a will change.

So, the answer of your question is YES. :)

Upvotes: 0

SomeWittyUsername
SomeWittyUsername

Reputation: 18358

No, it doesn't allocate memory. Your p actually points to the same location as &a and you modify the same memory in *p = 20;. This doesn't crash because you refer to the allocated memory (for a). If it's what you really want depends on you only.

Upvotes: 2

Related Questions