+ 1
why ++ on pointer does not override memory as all pointer have same size
Hi All We know that different datatype have different size but pointer has same size irrespective of data type it refers to. Refer attached code.... https://code.sololearn.com/c8a14a14A4a1 Question is written in code as code comment. To reiterate, does not it like pchr advances 1 byte but pchr itself occupies 8 bytes... How does this make sense? I know i made it messy in asking question.. please feel free to ask for concern on query once you go through it. Many thanks.
10 odpowiedzi
+ 2
No I don't think it goes only one byte.... It goes one byte for char pointer as size of char is 1...
If all pointer type (char pointer or int pointer ) moves one byte by doing +1, what is need of different data type pointer as all pointers hold same size of memory...only difference is how it shifts on additional one on pointer arithmetic
+ 2
p[0] is at address 100 // if p is of type char*
p[1] is at address 101 // if p is of type char*
p[0] is at address 100 // if p is of type int*
p[1] is at address 104 // if p is of type int*
p[0] is at address 100 // if p is of type double*
p[1] is at address 108 // if p is of type double*
.. there is no waste. Ketan Lalcheta
+ 2
here is proof:
#include <stdio.h>
int main()
{
char a[] = "ab";
printf("address of a[0]: %p\n", &a[0]);
printf("address of a[1]: %p\n", &a[1]);
printf("distance of a[1] - a[0]: %lu byte(s)\n", (&a[1] - &a[0]) * sizeof(char));
int b[] = {1, 2};
printf("\n\naddress of b[0]: %p\n", &b[0]);
printf("address of b[1]: %p\n", &b[1]);
printf("distance of b[1] - b[0]: %lu byte(s)\n", (&b[1] - &b[0]) * sizeof(int));
double c[] = {1.0, 2.0};
printf("\n\naddress of c[0]: %p\n", &c[0]);
printf("address of c[1]: %p\n", &c[1]);
printf("distance of c[1] - c[0]: %lu byte(s)\n", (&c[1] - &c[0]) * sizeof(double));
return 0;
}
// simple output on my run
address of a[0]: 0x7ffd6873ccbd
address of a[1]: 0x7ffd6873ccbe
distance of a[1] - a[0]: 1 byte(s)
address of b[0]: 0x7ffd6873ccb4
address of b[1]: 0x7ffd6873ccb8
distance of b[1] - b[0]: 4 byte(s)
address of c[0]: 0x7ffd6873cca0
address of c[1]: 0x7ffd6873cca8
distance of c[1] - c[0]: 8 byte(s)
// observe the ending 2 digits of the addresses
https://code.sololearn.com/c7A19A2587A1
+ 1
I don't know if I made it clearer or not. But if you try to use a void pointer then it may make more sense:
int main()
{
char cArr[] = {'A','B'};
char* pchr = cArr;
cout << *pchr << endl;
cout << *(++pchr) << endl;
cout << endl << endl;
int iArr[] = {1,2};
void* p = iArr;
char* pint = (char*)p;
cout << *((int*)pint) << endl;
cout << *((int*)(pint + (sizeof(int) * 1))) << endl;
return 0;
}
+ 1
So true...my bad.....
We both are on same point....
Now actual question is something as below :
Assume address I have is 100,101,102,103,104,105,106,107 and it is for pchar which holds address of first element (100)...now another char is at 101 as char holds one byte only....so why char pointer unnecessary occupy till 107....
+ 1
So pchr [0] is from 100 to 107 and pchr[1] is from 101 to 108....why overlap for this pointer
..same overlap doesn't come for double* I believe
+ 1
I just want to understand why pointer is different for different type.... It could have been only one pointer as all pointer just hold the adress and it does occupy same size irrespective of whose address it stores..what is need of different pointers ? It could have been solved by only void pointer considering whose address it is storing
0
when you do "(pointer + 1)", you are going 1 place right in the memory. I don't see it has anything to do with underlying data pointed by that "pointer".
0
Ketan Lalcheta i didn’t say it goes by 1 byte. the size depends on the pointer type.
0
I don't get why are you concerned about the size of the pointer? it's always 8 bytes long (in a 64-bit machine).
if you have an array of int:
int a[] = ....
// use (ptr + [index] to access the value)
*(a + 0...)
if it is char/struct or whatever
you can get the next value in the array by adding the index to the initial address.
don't know why you are mixing pointer size with pointer type.