pointer-arithmetic

Generated Assembly For Pointer Arithmetic

▼魔方 西西 提交于 2021-02-05 08:11:09
问题 This is a simple question but I just came across it. In the code snippet below I create three pointers. I know the three will exhibit equivalent behavior (all point to the same thing), but I honestly thought the third action in the code was the most "efficient", meaning that it would generate less assembly instructions to accomplish the same thing as the other two. I assumed that the first two have to first deference a pointer, and then take the memory address of the thing that was

Generated Assembly For Pointer Arithmetic

情到浓时终转凉″ 提交于 2021-02-05 08:10:08
问题 This is a simple question but I just came across it. In the code snippet below I create three pointers. I know the three will exhibit equivalent behavior (all point to the same thing), but I honestly thought the third action in the code was the most "efficient", meaning that it would generate less assembly instructions to accomplish the same thing as the other two. I assumed that the first two have to first deference a pointer, and then take the memory address of the thing that was

How to cast char array to int at non-aligned position?

杀马特。学长 韩版系。学妹 提交于 2021-01-28 06:24:00
问题 Is there a way in C/C++ to cast a char array to an int at any position? I tried the following, bit it automatically aligns to the nearest 32 bits (on a 32 bit architecture) if I try to use pointer arithmetic with non-const offsets: unsigned char data[8]; data[0] = 0; data[1] = 1; ... data[7] = 7; int32_t p = 3; int32_t d1 = *((int*)(data+3)); // = 0x03040506 CORRECT int32_t d2 = *((int*)(data+p)); // = 0x00010203 WRONG Update: As stated in the comments the input comes in tuples of 3 and I

Pointer arithmetic and casting

て烟熏妆下的殇ゞ 提交于 2021-01-28 02:01:34
问题 short x = 5; short*ptr = &x; short *ptr2 = ptr+5; cout << ptr2 - ptr << endl; cout << (long) ptr2 - (long)ptr << endl; I understand that pointers store addresses, but I don't understand why the answer for both lines isn't 10. Isn't ptr2 = address of pointer + sizeof(short) * 5? 回答1: Pointer arithmetic is expressed in terms of elements of the type that is being pointed at. ptr+5 increments ptr by 5 * sizeof(short) bytes. The result of ptr2 - ptr is 5, because the compiler knows that ptr and

Can we subtract NULL pointers?

穿精又带淫゛_ 提交于 2021-01-27 05:29:34
问题 Since pointer arithmetic is defined within the same array I'm in doubt if we can subtract NULL from another NULL . I'm concerned about the implementation of: //first and second can both either be from the same array //or be both NULL prtdiff_t sub(void *first, void *second){ //Do I really need this condition? if(!first && !second) return (ptrdiff_t) 0; return second - first; } 回答1: Subtracting two NULL pointers is not allowed. Section 6.5.6p9 of the C standard states: When two pointers are

Three questions: Is NULL - NULL defined? Is (uintptr_t)NULL - (uintptr_t)NULL defined? [duplicate]

↘锁芯ラ 提交于 2021-01-05 06:00:46
问题 This question already has answers here : Is the behavior of subtracting two NULL pointers defined? (4 answers) Closed last month . 1.Is NULL - NULL defined.? Is (char *)NULL - (char *)NULL defined.? Is (uintptr_t)NULL - (uintptr_t)NULL defined? I know that it works in all used by me implementations. But how does it look like from the standard point of view? I cant find the clear answer. Edit: From the dupe I assume that the question one answer is: YES. What about the second and third

Is incrementing/decrementing or adding an integer value to a pointer that is not pointing to an element in a sequence Undefined Behavior?

一曲冷凌霜 提交于 2020-12-29 03:51:25
问题 I know that pointers (to array element) and iterators can be incremented/decremented to walk a sequence of elements and can jump back-and-for elements in the sequence. But what will happen if I increment a pointer to a single object or add to it an integer value? is it undefined behavior or it is OK but we cannot access that memory? int x = 551; int* p = &x; ++p; --p; std::cout << *p << '\n'; Because I've already read that we should not increment/decrement a pointer that doesn't point to an

Can you compare nullptr to other pointers for order? Is it always smaller?

∥☆過路亽.° 提交于 2020-12-08 06:54:24
问题 This question is based on code that I found that monitors possible memory leaks, so it contains some code that you probably don't want to see in regular programs like ordering pointers. However, I saw that a pointer was set to nullptr and then the pointer was compared to a maximum address. Is it guaranteed by the C++ standard that nullptr is always smaller than other pointers for operator< ? 回答1: Can you compare nullptr to other pointers for order? Yes . But whether the result is useful is

Accessing struct data members via pointer arithmetic

不羁岁月 提交于 2020-12-05 05:13:20
问题 If I have a simple tensor class like this struct Tensor { double XX, XY, XZ; double YX, YY, YZ; double ZX, ZY, ZZ; } Is it undefined behavior to use pointer-arithmetic (see below) to access its elements? double& Tensor::operator[](int i) { assert(i < 9); return (&XX)[i]; } 回答1: Yes, it is undefined behavior. The data members are not in an array, and thus are NOT guaranteed to be stored back-to-back in contiguous memory, as pointer arithmetic would require. There may be indeterminate padding

Subtracting registers with an LEA instruction?

岁酱吖の 提交于 2020-11-25 02:22:44
问题 Does the LEA instruction support negative displacement? mov rax, 1 lea rsi, [rsp - rax] When I use the above code in my asm file I got the error: $ nasm -f macho64 test.asm $ error: invalid effective address I Know that we can do pointer arithmetic like this in C: void foo(char *a, size_t b) { *(a - b) = 1; } then I assume that: lea rsi, [rsp - rax] will work. And I also try to see what the GCC compiler do by using: $ gcc -S foo.c // foo.c has the function foo(above) in it but my asm