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
249
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 ...
1
vote
2
answers
156
views
Delphi: double indexing a pointer-of-array leads to “Array type required” error
A pointer of arrays is indexed once to get the array, then indexed again to get the element. This C-style trick should work with {$POINTERMATH ON}, but it definitely doesn't:
{$POINTERMATH ON}
...
...
1
vote
1
answer
83
views
Problem in using the delete operator for an array of objects allocated in heap using a FOR loop instead of using the delete[] array_of_objects keyword [closed]
#include <iostream>
class ArrayClass
{
private:
int *m_Arr;
int i;
public:
int Size;
ArrayClass() : Size(5), i(0), m_Arr(nullptr)
{
std::cout << "The ...
3
votes
4
answers
234
views
What in the C standard allows compilers to optimize `(((char *)p - 1) == NULL` to false?
Take the following snippet:
#include <stddef.h>
_Bool
foo(char * p) {
return (p - 1) == NULL;
}
Both GCC and LLVM optimize the result to false.
What in the standard allows the compilers to ...
1
vote
2
answers
124
views
How does the validity of pointers interacts with uninitialized pointer value?
I have seen that in the C++ abstract machines (if the are different), the mere act of forming a point to an invalid point in memory is undefined behavior.
For example
int* arr = new int[10];
int* last ...
2
votes
1
answer
38
views
Modification pgm fill 2-D Array using a pointer and it doesn't work
Initial pgm
void assign( int **mat, int n, int m ) {
int **p = mat;
int **p_end = p + n;
for ( ; p < p_end; ++p ) {
int *q = *p;
int *q_end = q + m;
for ( ; q < q_end; ++q ) {
printf( ...
1
vote
3
answers
215
views
different between *p[num] and (*p)num
In my C program book, I meet the question about (*p)[num2]
there is a 2 dimension array named a[num1][num2] and a (*p)[num2]
next,in the statement
for(p=&a[0];p<&a[num1];p++)
(*p)[i]=0;
...
1
vote
2
answers
378
views
Problem with DPTR arithmetic in 8051 assembly
We have a problem with a firmware project in 8051 assembly. It is used in an embedded system and now needs to be adapted to changes in the hardware. It contains a subroutine that sequentially reads ...
-1
votes
2
answers
186
views
What does the "!= data + arraySize" mean in C++? [closed]
I was looking for a way to find a given int in my array and i found this solution
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int data[] = {23, 45, 56, 12,...
1
vote
2
answers
333
views
How to interpret *(ptr) and *(ptr+2) in arrays?
I don't understand how pointers works exactly in arrays.
#include <stdio.h>
int main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
// ptr is assigned the address of the third element
ptr = ...
0
votes
4
answers
109
views
Program runs but Valgrind detecting a problem when attempting to write to malloc'd memory
To learn more of C, I'm trying to recreate basic data structures. Here's a minimal example of my attempt at an array, which compiles and runs but has a problem detected by valgrind:
#include <...
0
votes
5
answers
138
views
strange behaviour with pointer arithmetics in C
Please can someone explain to me this strange pointer behaviour. Did I miss something?!?
start first attempt
int *foo=(int*)malloc(sizeof(int)*4);//allocates memory for 4 integer with size of sizeof(...
-4
votes
1
answer
71
views
Subtraction of pointers in array should be index subtraction or address subtraction? [closed]
enter image description here
Shouldn't the answer be 2nd ques?
ptr1 saves the address of arr[0] and ptr2 saves the address of arr[3]. So (ptr2-ptr1) should be ((34(size of float)) -(04)) i.e., ...
0
votes
3
answers
683
views
C: Adding two 32-bit unsigned integers from raw memory bytes
In C, I have three memory areas that are several hundred bytes long. I want to take the ith pair of 32 bits of the two memory areas, add them as two unsigned 32-bit integers, and store the result in ...
4
votes
3
answers
411
views
Are XOR linked lists still allowed in C++17?
XOR linked lists use pointer arithmetic in a way that looks suspicious to me given the changes in semantics introduced in C++17 (and discussed e.g. in Is a pointer with the right address and type ...
0
votes
2
answers
88
views
C Asterisk Operator Using
im trying to learn pointers and im confused in second line, can someone explain how it works?
if we suppose 'a' base address is 100
int a[3][3] = {6, 2, 5, 0, 1, 3, 4, 9, 8};
printf("%p \n&...
2
votes
4
answers
215
views
What are use cases for writing (&var + 1) if var is not an array element?
Recently I learned from user "chux" that it is legal to add 1 to an address that doesn't represent an array element. Specifically, the following provision in the standard (C17 draft, 6.5.6 ¶...
0
votes
1
answer
64
views
Problem with matching datatypes using a dynamic float* array and dynamic matrix
Basically, I'm saving some coordinates in 2 arrays, arrayX and arrayY. Then, I want to change the value of a "blank" matrix in the coordinates that are saved in the previously mentioned ...