pointer-arithmetic

How void pointer arithmetic is happening in GCC

流过昼夜 提交于 2019-11-28 11:46:15
int main() { int a; void *p; p = &a; printf("%ld\n",(long)p); p = p+1; printf("%ld\n",(long)p); } In this program, p+1 is just incrementing the value of p by 1. I know void pointer arithmetic is not possible in C , so GCC is doing it implicitly. And if yes, then is it taking it as char pointer . Also, why dereferencing is not possible for void pointer, if it is implicitly doing pointer arithmetic. C does not allow pointer arithmetic with void * pointer type. GNU C allows it by considering the size of void is 1 . From 6.23 Arithmetic on void- and Function-Pointers : In GNU C, addition and

Pointer arithmetic getting wrong output [duplicate]

懵懂的女人 提交于 2019-11-28 02:05:26
问题 This question already has an answer here: Pointer Arithmetic In C 2 answers In the following program, Here ptr Has been declared as a pointer to an integer pointer and assigned the base address of the array p[] , which has been declared as an array of integer pointer. Suppose ptr contains the address 9016 (suppose the starting address of p is 9016) before ptr is incremented and after ptr++ , it will contain the value 9020 (suppose int takes 4 bytes). So ptr-p should give the output as 4 i.e

C pointer arithmetic

落花浮王杯 提交于 2019-11-27 20:14:04
Given this code: int *p, *q; p = (int *) 1000; q = (int *) 2000; What is q - p and how? It's actually undefined, according to the standard. Pointer arithmetic is not guaranteed to work unless the pointers are both pointing to either an element in, or just beyond, the same array. The relevant section of the standard is 6.5.6:9 (n1362 draft of c1x but this hasn't changed since c99) which states: 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 the difference of the subscripts of the two array

What is the rationale for limitations on pointer arithmetic or comparison?

浪尽此生 提交于 2019-11-27 13:09:10
In C/C++, addition or subtraction on pointer is defined only if the resulting pointer lies within the original pointed complete object . Moreover, comparison of two pointers can only be performed if the two pointed objects are subobjects of a unique complete object . What are the reasons of such limitations? I supposed that segmented memory model (see here §1.2.1) could be one of the reasons but since compilers can actually define a total order on all pointers as demonstrated by this answer , I am doubting this. There are architectures where program and data spaces are separated, and it's

Pointer arithmetic when void has unknown size [closed]

…衆ロ難τιáo~ 提交于 2019-11-27 09:10:41
In Visual Studio C++ version 9 (and probably other versions too), the following code: int a = sizeof(void); void const *b = static_cast<void const *>("hello world"); b += 6; Generates these errors : error C2070: 'void': illegal sizeof operand error C2036: 'const void *' : unknown size This code works under GCC, which treats sizeof(void) as 1 . Is there some way around this limitation, as casting explicitly to char * for purposes of pointer arithmetic adds to the confusion ( void * is well recognised and used as a typeless pointer to raw memory). Update0 Please note, I'm well aware of the

Byte precision pointer arithmetic in C when sizeof(char) != 1

穿精又带淫゛_ 提交于 2019-11-27 07:37:27
问题 How can one portably perform pointer arithmetic with single byte precision? Keep in mind that: char is not 1 byte on all platforms sizeof(void) == 1 is only available as an extension in GCC While some platforms may have pointer deref pointer alignment restrictions, arithmetic may still require a finer granularity than the size of the smallest fundamental POD type 回答1: Your assumption is flawed - sizeof(char) is defined to be 1 everywhere. From the C99 standard (TC3), in section 6.5.3.4 ("The

Is pointer arithmetic on inactive member of a union UB?

最后都变了- 提交于 2019-11-27 06:42:38
问题 Let's consider this example code: struct sso { union { struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; const char* get_tag_ptr() const { return short_str+15; } }; In [basic.expr] it is specified that pointer arithmetic is allowed as long as the result points to another element of the array (or past the end of an object or of the last element). Nevertheless it is not specified in this setion what happens if the array is an inactive member of a union. I believe it is not

How void pointer arithmetic is happening in GCC

好久不见. 提交于 2019-11-27 06:29:57
问题 int main() { int a; void *p; p = &a; printf("%ld\n",(long)p); p = p+1; printf("%ld\n",(long)p); } In this program, p+1 is just incrementing the value of p by 1. I know void pointer arithmetic is not possible in C , so GCC is doing it implicitly. And if yes, then is it taking it as char pointer . Also, why dereferencing is not possible for void pointer, if it is implicitly doing pointer arithmetic. 回答1: C does not allow pointer arithmetic with void * pointer type. GNU C allows it by

Address canonical form and pointer arithmetic

浪尽此生 提交于 2019-11-27 04:42:39
On AMD64 compliant architectures, addresses need to be in canonical form before being dereferenced. From the Intel manual, section 3.3.7.1 : In 64-bit mode, an address is considered to be in canonical form if address bits 63 through to the most-significant implemented bit by the microarchitecture are set to either all ones or all zeros. Now, the most significat implemented bit on current operating systems and architectures is the 47th bit. This leaves us with a 48-bit address space. Especially when ASLR is enabled, user programs can expect to receive an address with the 47th bit set. If

Order of operations for dereference and bracket-ref in C

隐身守侯 提交于 2019-11-27 04:38:45
If I do *ptr[x] , is that equivalent to *(ptr[x]) , or (*ptr)[x] ? *(ptr[x]) See the Wikipedia operator precedence table , or, for a more detailed table, this C/C++ specific table . In C, all postfix operators have higher precedence than prefix operators, and prefix operators have higher precedence than infix operators. So its *(ptr[x]) t0mm13b Using the counter-clockwise movement of analyzing and parsing that simple example 1. starting with ptr, work in counter-clockwise until you hit asterisk operator 2. asterisk, in counter-clockwise until you hit subscript operator 3. we arrive here, at