Conversation started Jul 25, 2023 at 5:33.
Jul 25, 2023 05:33
Are you free?
I got some pointers related doubts :/
Yes I'm free :-)
So I know that pointer is a tool that helps in getting information about an object's address and type. (Analogically, it helps in providing the address of a house instead of making a copy of house)
But uhm
You know that a computer has memory - usually 8GB or 16GB. Yes?
The memory is arranged as an array of bytes.
So if we use m to be the array the first byte is at m[0], the next at m[1] and so on up to m[whatever 16GB is].
And all the variables your program uses are stored somewhere in this memory.
Jul 25, 2023 05:39
Yes
For example in most languages an int is stored as four bytes i.e. at bytes m[a] to m[a+3] for some number a.
Does this make sense so far?
So suppose we have some int i stored in the four bytes m[a] to m[a+3] and we define a pointer:
int* p = &i;
Then what is actually stored in the variable p i.e. what does the address of i actually mean? Would you like to speculate?
(Related doubt: Are int* p and int *p equivalent ?)
Yes. It's a matter of taste how you write it.
I like int* because to me that looks like you have a type int*.
Jul 25, 2023 05:44
@JohnRennie I think the address of i that is m[a],m[a+1]....,m[a+3]
The address is just a.
That is, the variable is stored in the four bytes starting at m[a] and the address, i.e. the pointer, is just the index a.
The pointer literally just contains the number of the first byte where the variable is stored.
@JohnRennie So it doesn't require the last range of address because it is smart enough and knows the size of I?
Correct :-)
An int is always four bytes so you only need to know where the first byte is.
So pointers may seem complicated but they are simpler than you think. They are just the number of a the byte in memory where the variable is stored.
`#include<cstdio>
int main() {
    int a=56;
    int *add=&a;
    printf("value of a is %d\n",*add);
    printf("address of a is %p\n",add);
    *add=234;
    printf("value of a is %d\n",*add);
    printf("address of a is %p\n",add);
    printf("value of a is %d\n",a);
    a=300;
    int *add2=&a;
    printf("value of a is %d\n",a);
    printf("address of a is %p\n",add2);
}`
@JohnRennie Yes
What is *add=234; doing here?
I know that it does the opposite of taking the address of object
(derefrencing)
add is a pointer to int i.e. type int* so it is the number of the first byte where an int is stored.
So the four bytes that make up the int are in m[add], m[add+1], m[add+2] and m[add+3]. Yes?
Jul 25, 2023 05:50
Yes
So in principle you could store the number 234 by splitting it into four bytes and storing each byte in the four memory locations.
But this would be considerable hassle, so C++ has a quick way of doing it:
*add = 234;
means: store the int 234 in the four bytes m[add] to m[add+3]
*add is a shorthand for this.
@JohnRennie When I am writing *add 234, it modifies just the pointer na? It is not changing the int a ?
The integer a is stored in four bytes and the pointer add holds the address of those four bytes.
If you change those four bytes then you are changing the value of a because a's value is in those four bytes.
So if I write:
int* add = &a;
*add = 666;
printf("%d", a);
It will print 666.
@JohnRennie Uhm
and the address doesn't change?
The statement:
*add = 666;
is not changing the pointer add. It's doing:
m[add] = first byte of 666
m[add+1] = second byte of 666
m[add+2] = third byte of 666
m[add+3] = fourth byte of 666
So the address stays the same. It's just like an index into an array of bytes.
What changes is the data stored in those bytes.
Jul 25, 2023 06:04
@JohnRennie Ooh
So whenever I print a or *add, they are gonna print the same?
Yes, because both are the data stored in the same four bytes.
And what happens when a=300; is processed?
@JohnRennie Same like this?
when you declare the variable a the compiler reserves four bytes to hold the value in a.
Then when you write a = 300 the compiler stores the value 300 in the four bytes it reserved for the variable a.
@Wolgwang Yes.
The value 300 is split into four bytes and then stored in the four bytes reserved for a.
and the address won't change, right?
Once a is declared the four bytes used to hold its value never change.
i.e. printf("%p", &a) will always print the same number.
Jul 25, 2023 06:13
IIRC this was a point of difference between old languages and new languages, address will change in python?
Python doesn't use pointers, or at least I don't think so.
I'm not sure how python stores ints ...
Ah! the id of variable changes in python when we assign new number
@JohnRennie In a program run, there can be many pointers but only one pointy?
Yes, you could write:
a = 666;
int* p1 = &a;
int* p2 = &a;
int* p3 = &a;
etc
The p1, p2 and p3 all contain the same address of the first byte of a.
@JohnRennie Can you please take a look at this 3 minute video?
At t=1:26 , why *y=13; doesn't work?
Jul 25, 2023 06:21
Downloading now ...
and also what is x=new int , I am unable to find 'new' in cpp documentation
When you write:
int a;
the compiler automatically reserves four bytes to hold the data stored in a. Those four bytes will be wherever there is some convenient bit of memory for the compiler to use.
Then we can use:
int* p = &a;
to get the address of those four bytes.
@Wolgwang OK so far?
But suppose we don't want to declare a variable a. We just want to use the pointer p. Then we need some way to ask for four bytes for p to point to.
OK so far?
Jul 25, 2023 06:28
That's exactly what new int does.
Okay
The function new reserves enough bytes to store the type being requested e.g. new int reserves four bytes to store an int.
new double would reserve eight bytes because it takes eight bytes to store a double.
So:
p = new int;
reserves four bytes and stores the address of the first byte in the pointer p.
Then you can use *p = 666 to store an int in those four bytes.
In the video we have:
So *y=13; gives error because we have not already reserved space for it?
Exactly, yes :-)
#include<cstdio>
int main() {
    int* x;
    int* y;
    x=new int;
    *x=42;
    *y=13;
}
@JohnRennie :)
@JohnRennie But I don't get an error when I run this.
What happens in your system when you run this?
Jul 25, 2023 06:33
When you declare a variable like:
    int* x;
    int* y;
The compiler reserves the memory for that variable, but it does not initialise the memory. So the memory reserved for a variable will hold some random data that depends on what the memory was being used for before it was assigned to the variable.
So the compiler reserves however many bytes it takes to hold a pointer (eight I think) for the pointers x and y and those bytes will hold random values.
OK so far?
So if you write:
*y = 123;
you are writing the four bytes for the value 123 into some random bit of memory somewhere in the computer's memory.
That is, suppose y happened to be zero then you'd be doing:
m[0] = first byte of 123
m[1] = second byte of 123
etc
Yes?
So you'll be changing some random bit of memory, and if that memory was being used by something else you could crash your program. But if that memory wasn't being used by something else you may well get away with it.
Typically when you do this you get random crashes.
Sometimes it will crash your program and sometimes it won't.
It just depends on what random bit of memory you overwrote.
Thanks :). Enough doubts for now :P
 
Conversation ended Jul 25, 2023 at 6:42.