751 questions
0
votes
0
answers
52
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&...
6
votes
1
answer
165
views
Is pointer arithmetic on a pointer that points to a destroyed array element well-defined?
In Cppreference the rules for pointer arithmetic include:
If P points to the i-th element of an array object x with n elements, given the value of J as j, P is added or subtracted as follows:
– P + J ...
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 *...
3
votes
2
answers
152
views
Understanding static/dynamic array access with pointer arithmetic in C
I'm trying to understand how pointer arithmetic works in C.
Say if I have two arrays, one static and the other dynamic like so,
int a[10];
int * b = malloc(sizeof(int) * 10);
And lets assume each ...
-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 ...
3
votes
1
answer
148
views
What do these quotes say about pointer arithmetic?
No matter how hard I try, I can't understand these two quotes in C Standard:
If the pointer operand is not null, and the pointer operand and
result do not point to elements of the same array object ...
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 ...
4
votes
1
answer
144
views
lifetime of an array after placement new
This is a follow-up on this question and my answer
#include <new>
struct A {
int x{42};
int y{24};
};
static_assert(sizeof(A) == 2 * sizeof(int));
static_assert(alignof(A) == alignof(...
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 ...
1
vote
2
answers
126
views
Invalid optimization for pointer comparison in GCC? (introduced in GCC 7.1)
I have a puzzling problem regarding optimization and pointer equality that seems to happen only in GCC from version 7.1 and higher until the latest GCC version.
The Problem can be shown with one ...
2
votes
4
answers
299
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 &...
2
votes
2
answers
116
views
Two-Dimensional Array Subscripting (Stack-Based vs Heap-Based)
When I compile this program:
/* Create a 3 x 3 2D integer array */
int* matrix = calloc(3 * 3, sizeof(int));
/* This will cause an error since we
* can't use double subscript on a
* single pointer */...
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 ...
-4
votes
3
answers
242
views
Why can't I change the value of a constant using pointers in C++?
I'm working on code examples that demonstrate how you can "shoot yourself in the foot" using pointers in C++.
It's easy to create code that crashes. But now I'm trying to write code 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 ...
4
votes
1
answer
192
views
Incrementing pointers in bytes for indexing strided array
I wrote a library whose interface references coordinate data from client side. Clients are expected to provide an array of points that each contains 3 contiguous floats. I have decided to use a ...
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}
...
...
2
votes
2
answers
171
views
What type is actually used for array indexing and pointer arithmetic in C?
If I index an array with an unsigned int or a long, is it being implicitly cast into an int, or is the type somehow maintained? If I use large unsigned int as index, is it cast into a negative int? Is ...
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 ...
-1
votes
1
answer
105
views
Iterating over array of polymorphic objects
Consider the following code:
#include <iostream>
struct B {
char i;
B(char i) : i(i) {};
void bar() {};
};
struct D : B {
int y;
D(char i, int y) : B(i), y(y) {};
};
void ...
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
139
views
Is there anything wrong with moving pointer before start of array?
#include <stdio.h>
int strend(char *s, char *t);
int main()
{
char a[] = "hello world";
char b[] = "orld";
int i = strend(a,b);
printf("%d",i);
...
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 ...
0
votes
2
answers
100
views
Is the code below valid C? (Pointer arithemetic on null pointers)
I've recently been reading the C standard ISO/IEC 9899:2018 specification. Wherein, Section 6.5.6 (Additive operators) describes constraints on the + operator. Rule [8] says:
When an expression that ...
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( ...
0
votes
1
answer
74
views
Unexpected value when dereferencing pointer in C
The following code
#include <stdio.h>
int main()
{
long long data = 0xFFFEABCD11112345;
char *pData = (char *)&data;
printf("Value at address %p is %x\n", pData, *...
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;
...
0
votes
3
answers
85
views
Why does pointer subtraction yield a number unrelated to the values pointed to?
#include <stdio.h>
void main() {
int arr[] = {10,20,30,40};
int *p1 = arr;
int *p2 = &arr[2];
printf("%d",p2-p1);
}
Output : 2
I was suprised to see the output ...
1
vote
2
answers
376
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 ...
4
votes
3
answers
102
views
Can I move between contiguous sequences of fields of the same type in a struct using pointer arithmetic without alignof?
I am aware that there are other similar questions. I have read through these and don’t think this question has been answered.
Can I move between a consecutive (contiguous in the declaration sequence) ...
-1
votes
2
answers
182
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,...
0
votes
2
answers
52
views
calling an array from mid in a multidimensional array
#include<stdio.h>
void storeTables(int arr[][10] , int n ,int number);
int main() {
int tables[2][10];
storeTables(&tables[1],1,2);
storeTables(tables,0,3);
for(int i = 0 ;...
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 = ...
2
votes
2
answers
153
views
Pointer comparison in C
If I allocated something like
size_t n = ???;
unsigned char* s = malloc(n);
will it be perfectly defined behaviour to compare pointers to locations s + i for 0 <= i < n in the sense that s + ...
0
votes
4
answers
107
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 <...
5
votes
2
answers
132
views
Is unconditionally creating a pointer to the last element of a vector legal?
I have the following C++ code:
void bar(int&);
void baz();
void foo(std::vector<int>& v) {
int* pointer_to_last = v.data() + (v.size() - 1);
if (v.size() > 0 && *...
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(...
1
vote
2
answers
336
views
Correctly using type-punning and erasure for array of objects
My goal is to have a memory pool non-template class that is used to store arrays of objects.
The same memory pool object must be reusable for a different array (difference size, different type and/or ...
-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., ...
3
votes
1
answer
849
views
p1 and p2 are pointers to ints, if p2>p1 is valid, is p2-p1 valid?
Pointers to class member variables can be compared and the results depend on the declaration sequence. See this in the spec For instance this example in compiler explorer is valid and returns true (1):...
0
votes
3
answers
682
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 ...
1
vote
3
answers
126
views
What is the difference between (pointer + n) and pointer[n]?
In this code, I don't understand why the input character value is being put into (ptr + i) but then we print out ptr[i]. I know they are different because I am printing out the values in the first for ...
4
votes
3
answers
409
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 ...
3
votes
3
answers
274
views
Trouble understanding char* and string in CS50
So I know that a string is just an array of characters that are stored consecutively in a computer's memory.
I also know that in order to find out the location of a string, you just have to go to the ...
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&...
1
vote
1
answer
122
views
How to copy element from one array to other element of array using pointers?
I am attempting to copy the elements of one dynamically-allocated array (addVec) into another (array) in memory, after passing a pointer to the array from main to a function (changeArray) that assigns ...
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 ¶...
1
vote
2
answers
87
views
void* pointer arithmetic, cannot swap float values
recently, i was curious on how qsort() sorts without passing specific type, thus i'm trying to reverse an array without knowing / caring it's type.
i was told you can do pointer arithmetic if you cast ...
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 ...
0
votes
2
answers
73
views
What does [] do when called on an pointer in C?
What does [] do when called on an int* pointer?
E.g. in this code:
int* someIntPointer = 4000; //pointer points to byte 4000 in memory
++someIntPointer; //pointer points to byte 4004 in ...