pointer-arithmetic

Subtracting registers with an LEA instruction?

老子叫甜甜 提交于 2020-11-25 02:22:26
问题 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

Subtracting registers with an LEA instruction?

大城市里の小女人 提交于 2020-11-25 02:18:48
问题 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

Subtracting registers with an LEA instruction?

时间秒杀一切 提交于 2020-11-25 02:18:21
问题 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

Why does dereferencing a pointer to string (char array) returns the whole string instead of the first character?

 ̄綄美尐妖づ 提交于 2020-06-26 18:07:19
问题 Since the pointer to array points to the first element of the array (having the same address), I don't understand why this happens: #include <stdio.h> int main(void) { char (*t)[] = {"test text"}; printf("%s\n", *t + 1); // prints "est text" } Additionally, why does the following code print 2 then? #include <stdio.h> int main(void) { char (*t)[] = {1, 2, 3, 4, 5}; printf("%d\n", *t + 1); // prints "2" } 回答1: All other answers at the moment of writing this answer were incorrect. Moreover your

Pointer dereference array index

旧街凉风 提交于 2020-06-01 07:36:45
问题 Having this: #include <stdio.h> #include <stdlib.h> struct Test { char c; } foo; int main (void) { struct Test **ar; ar=malloc(16); *(ar+1) = &foo; ar[1]->c = 'c'; //this work (*(*ar+1)).c = 'c'; //this does't work return 0; } //(**(ar+1)).c='c'; --> first case Why the above works only the variant with array entry and not pointer dereference? struct Test { char c; } foo; int main (void) { struct Test **ar; ar=malloc(16); *ar=malloc(0); *(ar+1) = &foo; //(**(ar+1)).c='c'; (*(*ar+1)).c='c'; //

C null pointer arithmetic

旧街凉风 提交于 2020-03-16 06:35:38
问题 I noticed this warning from Clang: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] In details, it is this code which triggers this warning: uint8_t *end = ((uint8_t*)0) + sizeof(uint8_t) * count; Why would arithmetic on a null pointer be forbidden when doing the same on a non-null pointer obtained from an integer different than zero does not trigger any warning ? And more importantly, does the C standard explicitly forbid null

What does “possibly-hypothetical” mean in the pointer arithmetic rules?

邮差的信 提交于 2020-01-24 04:27:46
问题 In the standard's specification for pointer arithmetic ([expr.add]/4.2, we have: Otherwise, if P points to an array element i of an array object x with n elements ([dcl.array]), the expressions P + J and J + P (where J has the value j ) point to the (possibly-hypothetical) array element i + j of x if 0 ≤ i + j ≤ n and the expression P - J points to the (possibly-hypothetical) array element i − j of x if 0 ≤ i − j ≤ n . What does "possibly-hypothetical" mean here? The passage already

Pointer address Arithmetic and Hex/Dec Conversion

耗尽温柔 提交于 2020-01-16 00:57:29
问题 I have a pointer address I obtained from the extern char etext , end and edata . I also obtained address of variables using &<Variable Name> . Both are hexadecimal pointer addresses. I need to do arithmetic on those addresses in decimal. How do I do so? I need to convert the address into an int in decimal so I can find size of memory segments by subtracting addresses. 回答1: Math is math. It doesn't matter what base you do it on. The computer is working only in base 2 anyway. Only during input