Simple Question: Pointers and Uninitialized Variables Explained

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Pointers
Click For Summary

Discussion Overview

The discussion revolves around the behavior of pointers and uninitialized variables in C++. Participants explore the implications of declaring pointers, the necessity of initializing them, and the differences between stack and heap memory allocation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions why dereferencing an uninitialized pointer results in an error, suggesting that declaring a pointer allocates memory for the integer it points to.
  • Another participant clarifies that declaring a pointer only allocates memory for the pointer itself, not for the integer it points to, which remains uninitialized until assigned a valid address.
  • A later reply discusses alternative methods for initializing pointers, including dynamic memory allocation using the "new" keyword, and notes that newly allocated memory also starts with an uninitialized value.
  • Participants mention various ways to initialize pointers and the implications of each method, emphasizing that without proper initialization, dereferencing a pointer can lead to undefined behavior.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of initializing pointers before use, but there are nuances in the methods of initialization and the implications of uninitialized memory that remain open for discussion.

Contextual Notes

There are limitations regarding the understanding of memory allocation and initialization, as well as the potential for undefined behavior when dealing with uninitialized pointers.

Who May Find This Useful

This discussion may be useful for individuals learning about pointers in C++, particularly those interested in memory management and the implications of uninitialized variables.

yungman
Messages
5,741
Reaction score
291
Why this doesn't work?
C++:
#include<iostream>
using namespace std;
int main()
{
    int* p;
    *p = 1;//error said uninitialized variable p used.
    count << *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;
    count << *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
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   Reactions: yungman
Thanks Jtbell, that clarified for me.
 
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;.
 

Similar threads

Replies
7
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
5
Views
2K
Replies
12
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
4
Views
2K
Replies
12
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K