Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C language to declare a pointer variable, the system will automatically allocate memory? If it's not initialized?

I am a freshman who has only learned C language

Statement of int variables at the same time, there was a piece of memory in the computer, if there is no initialization, stored in the memory is rubbish, but still have the memory, my understanding is that statement pointer variable, should also opened up a piece of memory, but if no initialization, stored in the memory is not well-known address (spam), like int variable declaration.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

int a;
is an uninitialized int variable. The compiled code will reserve a piece of memory for that variable. It’s content is not specified. It can contain anything when reading it. Reading it is Undefined Behaviour.

int* b; is also an unitialized variable, but of type int*. the compiled code will also reserve a piece of memory to hold that variable. Since it is a pointer, it has the ability to be dereferenced. However, this pointer is not initialized to a valid address of an int.
The same rules apply to pointers here. Reading it before it is initialized invokes Undefined Behaviour.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading