Simple Question: Pointers and Uninitialized Variables Explained

  • Thread starter yungman
  • Start date
  • Tags
    Pointers
In summary, when you declare a pointer, you already allocate memory to store the address of that pointer. If you try to use that pointer without first assigning it a valid address, you get an error.
  • #1
yungman
5,718
241
Why this doesn't work?
C++:
#include<iostream>
using namespace std;
int main()
{
    int* p;
    *p = 1;//error said uninitialized variable p used.
    cout << *p << endl;
    return 0;
}

I know if I do this, it works:
C++:
#include <iostream>
using namespace std;

int main ()
{
    int* p;
    int x = 10;
    p = &x;
    cout << *p << endl;

  return 0;
}
I thought when you declare a pointer int*p; you already allocate a memory for an integer pointed by p already.

Or int*p; only allocate memory for pointer p to store the address, that the address is not valid until using p = &x; to write the address of x into p?
Thanks
 
Last edited:
Technology news on Phys.org
  • #2
yungman said:
Or int*p; only allocate memory for pointer p to store the address, that the address is not valid until using p = &x; to write the address of x into p?
Correct. Compare this to writing simply int x;. This says simply that x is a variable that is intended to contain an int. It doesn't store any particular value into x. At this point, x contains, in effect, a random bit pattern.

When you write simply int *p; this says that p is a variable that is intended to contain a pointer to an int. It doesn't store a particular value into p, that is, it doesn't make p point to any memory location in particular. At this point, p contains, in effect, a random bit pattern.
 
  • Like
Likes yungman
  • #3
Thanks Jtbell, that clarified for me.
 
  • #4
yungman said:
the address is not valid until using p = &x; to write the address of x into p?
That's one way to put a valid address into p. Another way, of course, is to allocate "new" memory explicitly. p = new int; allocates memory intended to hold an int and stores its address in p, but doesn't store any particular value in the newly-allocated memory, so in effect, *p gives you a random bit pattern.

To put something into the newly-allocated memory, you can initialize it when you allocate it: int *p = new int(x); declares p to be a pointer intended to point to an int, allocates an int-sized chunk of memory, and finally copies the value of x into the newly-allocated memory. Or you can initialize the new memory later: int *p = new int; *p = x;. Or you can separate all three steps: int *p; p = new int; *p = x;.
 

1. What are pointers and how do they work?

Pointers are variables that store memory addresses of other variables. They allow us to indirectly access and manipulate data by pointing to the memory location where the data is stored.

2. Why do we need to initialize variables before using them?

Uninitialized variables contain random or garbage values, and using them can lead to unexpected results or errors in our code. Initializing variables gives them a specific value that we can use and manipulate.

3. How do we initialize pointers?

Pointers can be initialized by assigning them the memory address of another variable, using the address-of operator (&), or by using the NULL keyword to assign them a null or empty value.

4. What is the difference between a null pointer and an uninitialized pointer?

A null pointer is a pointer that points to nothing or contains an empty value, while an uninitialized pointer points to a random or garbage memory address. Accessing an uninitialized pointer can lead to errors, while accessing a null pointer will not.

5. Can we use uninitialized pointers safely in our code?

No, using uninitialized pointers can lead to unpredictable and potentially dangerous results. It is important to always initialize pointers before using them in order to avoid errors and ensure our code runs as intended.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
885
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
990
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
Back
Top