pointer-arithmetic

Set shared_ptr with new_pointer that is old_pointer + offset

南楼画角 提交于 2019-12-01 13:41:26
问题 Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows. Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data . This address is still in alocated memory. But how to

Can std::uintptr_t be used to avoid undefined behavior of out-of-bounds pointer arithmetic?

被刻印的时光 ゝ 提交于 2019-12-01 05:18:29
Now we know that doing out-of-bounds-pointer-arithmetic has undefined behavior as described in this SO question . My question is: can we workaround such restriction by casting to std::uintptr_t for arithmetic operations and then cast back to pointer? is that guaranteed to work? For example: char a[5]; auto u = reinterpret_cast<std::uintptr_t>(a) - 1; auto p = reinterpret_cast<char*>(u + 1); // OK? The real world usage is for optimizing offsetted memory access -- instead of p[n + offset] , I want to do offset_p[n] . EDIT To make the question more explicit: Given a base pointer p of a char array

Difference between two pointer variables [duplicate]

一世执手 提交于 2019-12-01 04:15:47
This question already has an answer here: Pointer/Address difference [duplicate] 3 answers i have asked this question in a written test. while running the below code on my lapi, i am getting 10 as output #include<stdio.h> int main() { int *i, *j;/* two pointer variable*/ i = (int *)60; j = (int *)20; printf("%d \n",i-j); return 0; } Output : 10 Can anyone tell me why the output is 10 . According to the C Standard (6.5.6 Additive operators) 9 When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is

How to avoid multiplication in pointer arithmetic?

我们两清 提交于 2019-12-01 03:01:30
问题 If I write int main(int argc, char *argv[]) { int temp[50][3]; return &temp[argc] - &temp[0]; } and compile it with Visual C++, I get back: 009360D0 55 push ebp 009360D1 8B EC mov ebp,esp 009360D3 8B 45 08 mov eax,dword ptr [argc] 009360D6 8D 0C 40 lea ecx,[eax+eax*2] 009360D9 B8 AB AA AA 2A mov eax,2AAAAAABh 009360DE C1 E1 02 shl ecx,2 009360E1 F7 E9 imul ecx 009360E3 D1 FA sar edx,1 009360E5 8B C2 mov eax,edx 009360E7 C1 E8 1F shr eax,1Fh 009360EA 03 C2 add eax,edx 009360EC 5D pop ebp

Difference between two pointer variables [duplicate]

我的未来我决定 提交于 2019-12-01 01:09:41
问题 This question already has answers here : Pointer/Address difference [duplicate] (3 answers) Closed 4 years ago . i have asked this question in a written test. while running the below code on my lapi, i am getting 10 as output #include<stdio.h> int main() { int *i, *j;/* two pointer variable*/ i = (int *)60; j = (int *)20; printf("%d \n",i-j); return 0; } Output : 10 Can anyone tell me why the output is 10 . 回答1: According to the C Standard (6.5.6 Additive operators) 9 When two pointers are

Using void pointer to an array

半腔热情 提交于 2019-11-30 21:55:44
I was just trying to use a void pointer to an integer array ,I tried to see if i can print the array back by casting it back into int. But it is giving me some random value. Can you tell me where i am going wrong? #include<stdio.h> #include<stdlib.h> int main(){ int a[5]; int x; int j; a[0]=1; a[1]=2; a[2]=3; a[3]=4; void *arr=a; for(j=0;j<4;j++){ x = *(int *)(arr+j); printf("%d",x); } return 0; } Output is this: 133554432131072512 Why is it not pinting elements of array a[] i.e 1,2,3,4 ? You need to cast arr before adding j . Here is a minimal fix: x = *(((int *)arr)+j); but I think it's

Using void pointer to an array

纵然是瞬间 提交于 2019-11-30 17:11:41
问题 I was just trying to use a void pointer to an integer array ,I tried to see if i can print the array back by casting it back into int. But it is giving me some random value. Can you tell me where i am going wrong? #include<stdio.h> #include<stdlib.h> int main(){ int a[5]; int x; int j; a[0]=1; a[1]=2; a[2]=3; a[3]=4; void *arr=a; for(j=0;j<4;j++){ x = *(int *)(arr+j); printf("%d",x); } return 0; } Output is this: 133554432131072512 Why is it not pinting elements of array a[] i.e 1,2,3,4 ? 回答1

What are convincing examples where pointer arithmetic is preferable to array subscripting?

余生长醉 提交于 2019-11-30 09:04:46
I'm preparing some slides for an introductory C class, and I'm trying to present good examples (and motivation) for using pointer arithmetic over array subscripting. A lot of the examples I see in books are fairly equivalent. For example, many books show how to reverse the case of all values in a string, but with the exception of replacing an a[i] with a *p the code is identical. I am looking for a good (and short) example with single-dimensional arrays where pointer arithmetic can produce significantly more elegant code. Any ideas? Getting a pointer again instead of a value: One usually uses

C weird array syntax in multi-dimensional arrays

帅比萌擦擦* 提交于 2019-11-30 08:56:25
I've known that this is true: x[4] == 4[x] What is the equivalent for multi-dimensional arrays? Is the following true? x[4][3] == 3[x[4]] == 3[4[x]] x[y] is defined as *(x + (y)) x[y][z] would become *(*(x + (y)) + z) x[y[z]] would become *(x + (*(y + (z)))) x[4][3] would become *(*(x + (4)) + 3) would become *(*(x + 4) + 3) 3[x[4]] would become *(3 + (*(x + (4)))) would become *(*(x + 4) + 3) 3[4[x]] would become *(3 + (*(4 + (x)))) would become *(*(x + 4) + 3) Which means they are all equivalent. Yes. In each case x is an array which decays to a pointer and then has pointer arithmetic

Why subtract null pointer in offsetof()?

牧云@^-^@ 提交于 2019-11-30 08:37:19
问题 Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() (http://en.wikipedia.org/wiki/Offsetof) defines it as: #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why subtract (char *)0 in the Wikipedia version? Is there any case where that would actually make a difference? 回答1: The first version converts a pointer into an integer with a cast, which is not portable. The second