Simple Question: Pointers and Uninitialized Variables Explained

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    Pointers
AI Thread Summary
The discussion revolves around the proper use of pointers in C++. The initial code fails because the pointer `p` is declared but not initialized, leading to an error when trying to dereference it. It is clarified that declaring `int* p;` only allocates memory for the pointer itself, not for the integer it points to. Until `p` is assigned a valid address, such as through `p = &x;`, it contains an undefined value. The conversation highlights that memory must be explicitly allocated using `new` if a pointer is to point to dynamically allocated memory. Various methods for initializing pointers and their associated memory are discussed, emphasizing that without proper initialization, dereferencing a pointer can lead to unpredictable behavior.
yungman
Messages
5,741
Reaction score
294
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
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.
 
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;.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top