751 questions
0 votes
0 answers
56 views
How does i in 'scanf("%d", ptr + i);' counts to 4? [duplicate]
I mean that i is incremented by 1 right, then how does ptr + i equals ith block of memory since int size is 4? int i, n; printf("Enter the number of Integers: "); scanf("%d&...
5 votes
5 answers
252 views
Is array pointer arithmetic undefined behavior in C for separate translation units?
There are several source code files of a program. File file_a.c has an array in the global scope, and a function is provided that returns a pointer to its beginning: static int buffer[10]; int *...
-1 votes
2 answers
152 views
Dynamic arrays and pointer arithmetic
As far as I understand, pointer arithmetic is defined mainly in expr.add. But it seems defined only for static arrays, not dynamic ones: 4 When an expression J that has integral type is added to or ...
1 vote
1 answer
183 views
Correct way to get an array through low-level allocation functions
Most low-level allocation functions return void *. If I want to perform pointer arithmetic on the obtained memory and/or allocate an array, is this the correct way to do it without invoking undefined ...
1 vote
0 answers
90 views
Pointer arithmetic within array using a pointer to objects created placement-new
I ran to an over-specialized container somewhere in production code. It was C++98 upgraded to C++11, but I wonder how many UBs (or implementation-defined behaviors) are in it. Basically, without any ...
2 votes
4 answers
302 views
Using Pointers In Loops - C Programming
I am currently learning about pointers in C programming as part of my training. I am particularly curious about using pointers directly within "for" loops. For example, in the following &...
3 votes
1 answer
173 views
How can I get the memory distance in bytes between any two objects (if this measurement exists in a meaningful way)?
Pointer arithmetic is only defined behavior for pointers to elements of the same array (or one past the array). Is there any defined behavior to get the distance in memory between two elements that ...
2 votes
1 answer
143 views
Would P1839 make it possible to access subobjects from offsets into object representations?
This is in one sense an extension to Is it UB to access a subobject by adding byte offset to the address of the enclosing object? under the assumption that P1839 is adopted. Consider the following ...