Confusion in Pointers

Pages: 12
Jul 25, 2018 at 9:02am
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 <<" ";
  }

Code 2:
1
2
3
4
5
6
7
#include<iostream>
int main(){
      int t = 50;
      int *p;
      *p = t;
    std::cout << p <<" "<< *p <<" "<< *p+1 <<" ";
 }

Last edited on Jul 25, 2018 at 9:06am
Jul 25, 2018 at 9:17am
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.

This code demonstrates that you don't understand pointers. Learn about pointers; http://www.cplusplus.com/articles/EN3hAqkS/
Last edited on Jul 25, 2018 at 9:24am
Jul 25, 2018 at 12:03pm
@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 <<;
 }
Last edited on Jul 25, 2018 at 12:11pm
Jul 25, 2018 at 12:07pm
I mean what's wrong with that??


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.
Last edited on Jul 25, 2018 at 12:08pm
Jul 25, 2018 at 12:10pm
what's wrong with that??


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.
Last edited on Jul 25, 2018 at 12:13pm
Jul 25, 2018 at 12:13pm
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.
Jul 25, 2018 at 12:16pm
@Manga if you will run the code in my last comment it will not give error
Jul 25, 2018 at 12:19pm
it will not give error


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.
Last edited on Jul 25, 2018 at 12:20pm
Jul 25, 2018 at 12:22pm
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
Jul 25, 2018 at 12:26pm
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;
 }
Last edited on Jul 25, 2018 at 12:32pm
Jul 25, 2018 at 12:30pm
Try setting it to an address that's likely to be outside your memory;

1
2
3
4
5
6
7
8
9
#include<iostream>
using std::cout;
int main()
{
	int t = 50;
	int *p = (int*)(0xFFFFFFFF);
	*p = 7;
	std::cout << p << " " << *p << " " << t;
}
Jul 25, 2018 at 12:35pm
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.
Last edited on Jul 25, 2018 at 12:35pm
Jul 25, 2018 at 12:47pm
Everytime i runs this program, why it provides the same value of p ,it should be random?
1
2
3
4
5
6
7
#include<iostream>
int main(){
      int t = 50;
      int *p ;
      *p = t;
    std::cout <<p;
 }
Last edited on Jul 25, 2018 at 12:48pm
Jul 25, 2018 at 12:54pm
a point and the data it points to are 2 separate things. you cannot declare one and expect the other to be automatic.

you declare a pointer and leave it pointing at nothing.

if you coded int a; printf("%d",a) what do you think it would print? would the value have any meaning?

you are using the pointer without assigning a value to it, and that's a semantic error.

*p = 5 is not assigning to the pointer, it is using the pointer to assign 5 to an unknown memory location.
Jul 25, 2018 at 1:02pm
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.
Last edited on Jul 25, 2018 at 1:05pm
Jul 25, 2018 at 1:11pm
There's no way of knowing why that happens. The only people who know, are the people who wrote gcc.

It's undefined behaviour.
Jul 25, 2018 at 1:16pm
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.

PS: The code you posted here
( http://www.cplusplus.com/forum/beginner/240320/#msg1070108 )
does not even compile (bad syntax). So I can't even tell you what happens, because that is clearly not your actual code...

MikeyBoy wrote:
The only people who know, are the people who wrote gcc.
They'll probably just be able to guess a little bit better than we can as to what choices the compiler is making. ;)
Last edited on Jul 25, 2018 at 1:23pm
Jul 25, 2018 at 1:32pm

Just because this particular case happens to work with your particular compiler+OS doesn't mean it will work with another person's computer.


or even the next version of gcc.

pointer p is pointing to a random location but still it is a valid location.

no, its not a valid location. it could even be an address which is beyond your memory limits.
Last edited on Jul 25, 2018 at 1:35pm
Jul 25, 2018 at 1:42pm
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.
Jul 25, 2018 at 2:00pm
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.. :)
Last edited on Jul 25, 2018 at 2:01pm
Pages: 12