Code 1 works perfectly fine, then why not code 2.As, i can see *p+1 is not a valid operation in code 2, but why?? what is the difference between p =&t and *p =t ??How they are making a difference in these two codes??
Code 1:
1 2 3 4 5 6 7 8
#include<iostream>
int main(){
int t = 50;
int *p;
p = &t;
std :: cout << p <<" "<< *p <<" "<< *p+1 <<" ";
}
int *p;
This creates a new int-pointer. Where is it pointing? There is no way to know. It will have some random value. It will be pointing at some random memory value somewhere.
This did NOT create an int. It did NOT arrange some memory somewhere for an int to be written into.
*p = t;
This now attempts to write the value of t into that random memory. Writing over the top of random memory is very, very bad. This could crash. This could write over data. This is very very bad.
@Repeater , I agree with you...pointer p stores integer t in a random memory location..but isn't it true, that 'p' retains that random address and we can still de-reference p and copy an integer 't' into it and then while printing, add 1 to that integer stored in that address??? I mean what's wrong with that??
This code executes without a problem.
1 2 3 4 5 6 7
#include<iostream>
int main(){
int t = 50;
int *p;
*p = t;
std::cout << p <<" "<< *p <<;
}
Firstly, whatever data is in that memory location could be in use by some other variable, so you will be changing the value of some other variable, at random. I hope you understand why that could be bad.
Secondly, your running programme has been given a section of memory by the OS. If you try to read/write to some memory that the OS thinks it didn't give you (i.e. if the random value of the pointer is to memory that the OS didn't give your programme), the OS will kill your programme stone cold dead. On windows, it's usually called an "access violation". On Linux, a "segfault". Basically, your programme crashes because the OS won't let you read/write memory that it didn't give you.
how about when that random pointer points into your data region and all writes corrupt that data.
or perhaps it points somewhere into the current stack and causes a crash when function calls try to access parameters or return addresses
perhaps it points to some unused memory, but then that memory gets alloc'd to another variable. which corrupts what you thought you'd stored via the dodgy pointer.
don't forget also, that random address may well be different each time you run the program, making debugging an absolute nightmare as it corrupts different data each time you run it.
When I try to run that second code I get error. Uninitialized variable p being used.
This is because p is a pointer variable and needs to store an address. It does not get an address value by default. You must initialize p before trying to dereference it.
It's random. It's chance. What does YOUR compiler do? Does YOUR compiler, with YOUR settings, with YOUR memory how it was at the exact moment YOU ran it, mean that by chance it doesn't crash? Doesn't matter. The code in your last comment IS wrong. It IS erroneous.
This is something beginners sometimes struggle to understand. They say "But it compiles!" and they say "But it didn't crash when I ran it!" Still wrong. Still an error.
thanks a lot,I know it is erroneous by all your information, but everytime i run it ...it never crashes.how is that possible?..i m using codeblocks IDE with GNU GCC compiler
If i will make it point to NULL at the time of pointer declaration .will that still be erroneous??
I think my compiler is automatically making it point to NULL ,even if i don't make it point to NULL.
why is this program crashing
1 2 3 4 5 6 7 8
#include<iostream>
int main(){
int t = 50;
int *p ;
*p = t;
std::cout <<&t;
}
If i will make it point to NULL at the time of pointer declaration .will that still be erroneous??
Dereferencing a null pointer in C++ is undefined behaviour. The C++ standard doesn't care what your compiler does, so it could do anything; it's outside of C++. Very commonly, it causes a crash.
pointer p is pointing to a random location but still it is a valid location.
how is that memory location unknown, every time i run and compile program in my above comment,it prints the same location.
amiable, what exactly is it you want us to tell you?
We've told you what is wrong. Using an uninitialized pointer is illegal. When a pointer is dereferenced, it should only ever be pointing to vaild data that your program itself has made.
Just because this particular case happens to work with your particular compiler+OS doesn't mean it will work with another person's computer. Even if you get the same address every time you run it.
When coding in C++, you have to be aware of what can cause undefined behavior. If this is too annoying, note languages like C# (well, most of it) and Java don't allow for undefined behavior.
Everyone here is trying to help you learn to not make a programming mistake. If your compiler allows this, you might think that it is ok, but your programs won't work on an end users computer. It won't work on my computer for example. Learn from this and develop good programming habits.
Thanks a lot ,everybody :)
Kind of response i am getting as a beginner is highly appreciated..i aspire to learn a lot from this
community and hope to give it back someday.. :)