Skip to main content
Filter by
Sorted by
Tagged with
5 votes
1 answer
139 views

I am work a mature codebase, one of the signs of its age is lack of consts which make the code difficult to reason about when refactoring. I have found a function which takes an argument as a mutable ...
Dominik Kaszewski's user avatar
5 votes
2 answers
134 views

I have a simple struct in C and I used typedef to create an alias (db) for it: typedef struct { char* name; } db; I then allocated memory on the heap for a pointer: db *a = malloc(sizeof(db)); a-&...
Mahdi Hosseini's user avatar
1 vote
2 answers
201 views

I want to do pure type punning without memcpy in C using pre-allocated page which I made with mmap() on Linux, because I understand the architecture and how physical ram works, alignment & ...
abrar's user avatar
  • 21
2 votes
1 answer
167 views

If I wanted to replace large chunks of an array in C with items from another list, are there any solutions that don't involve for loops or writing code like the following? int64_t foo[size]; foo[value]...
Chubbachoo's user avatar
2 votes
3 answers
251 views

I'm trying to display the contents of some allocated memory byte by byte: #include <stdio.h> #include <stdlib.h> typedef struct { int len; int* vec[]; } vec_t; int main(void) { int ...
Mecki's user avatar
  • 99
Advice
0 votes
3 replies
94 views

(UPDATE: after various suggestions, some solutions were added at the end. It does seem to be possible to avoid pointers altogether, but it still is not completely ideal...) In the question Can I ...
Jos Bergervoet's user avatar
0 votes
4 answers
166 views

I was attempting the binary tree inversion question on Leetcode https://leetcode.com/problems/invert-binary-tree/ I came up with this solution TreeNode* invertTree(TreeNode* root) { if (root ==...
Raghav Wadhwa's user avatar
1 vote
0 answers
58 views

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&...
0x0pralad0x0's user avatar
1 vote
1 answer
144 views

My code should print the numbers given as command line arguments. Instead it prints random-looking values. What is going on and how can I fix it? #include <stdio.h> #include <pthread.h> #...
user31884282's user avatar
1 vote
0 answers
61 views

We are using Matlab to communicate with a C++ library, and are struggling with memory management. We create a libpointer pointing to an array, and then try to update its value in a loop. I don't think ...
RPM's user avatar
  • 1,809
0 votes
3 answers
101 views

static char* convert_to_string(const volatile uint8_t *ch) { static char read[40]; // static so pointer remains valid after return uint8_t i = 0; while (*ch != ESC || *ch != END_OF_TEXT ||...
Maaz Madha's user avatar
2 votes
1 answer
217 views

I have 2 maps using BidBook = std::map<float, int, std::greater<>>; // price -> qty using AskBook = std::map<float, int, std::less<>>; I have a struct that contain iterator ...
Huy Le's user avatar
  • 1,999
1 vote
2 answers
176 views

I've created the function void add_entry(char key[], void* value, char file[], ValueType type) and I want to use my generic function to get the typeof the pointer #define typeof(x) _Generic((x), \ ...
user31846615's user avatar
0 votes
1 answer
92 views

I am trying to use std::map to map numerical IDs to function handlers: All function handlers take two integers and return an integer. Handler 100 would calculate the product. When preparing the map, ...
Ralf's user avatar
  • 11
1 vote
2 answers
215 views

The following code is trying to set the test array in the first for loop, but it can't be used in the second for loop. int main() { int *test[3]; /** Try to set the array value one by ...
stackbiz's user avatar
  • 1,914
3 votes
1 answer
136 views

In Rust, both references and pointers to unsized types have metadata indicating the actual type of the reference. For example, a *mut [T] consist of a pointer to the start of a slice, plus a length ...
ais523's user avatar
  • 2,439
7 votes
6 answers
2k views

I have some code in which an array of strings is defined in a conditional statement. The array is out of scope where I need it. So I defined another pointer in the outer scope. In the conditional ...
Johannes Linkels's user avatar
2 votes
0 answers
88 views

I am trying to learn C, and I want to specifically work with arrays and matrices as I do scientific simulations (coming from python!!). After writing few basic 1-D array codes in C, I am going for ...
Sayantan4796's user avatar
2 votes
2 answers
129 views

I'm writing a little toy program to try to help myself better understand this language (AT&T syntax, x86_64 assembly language). Consider this code, if you'll be so kind: .section .data mystring: ....
Andrew Boone's user avatar
2 votes
4 answers
239 views

If two pointers are referencing the same memory location. Will it be possible to make one pointer null, if we deallocate that memory location? For example: #include <iostream> using namespace ...
GhostVaibhav's user avatar
1 vote
1 answer
274 views

During a job interview, I was asked a question about the differences between **a, *a, a, a* and a** (which I failed). I just asked ChatGPT about the difference between *a and a* and it came up with ...
Dominique's user avatar
  • 17.7k
4 votes
2 answers
225 views

I don't know how to reset the value field inside the Hello struct. It's a pointer pointed to an outside passed input argument. typedef struct Hello { void *value; } Hello; Hello* create_hello() { ...
stackbiz's user avatar
  • 1,914
-2 votes
3 answers
220 views

Can I declare a variable along with a pointer? //like this struct node n1,n2,n3,*start; //or do i have to do it separately struct node *start;struct node n1,n2,n3; to declare variable alongside ...
Sandeep's user avatar
  • 39
4 votes
1 answer
140 views

I’m new to C and I’m trying to understand how pointers work. In this piece of code, when I run it on Python Tutor (pythontutor.com) to see how the stack and heap work, I never see the pointer being ...
Bilal's user avatar
  • 173
5 votes
3 answers
335 views

I'm struggling to understand a core concept in C regarding Array-to-Pointer Decay when arrays are passed as function parameters. It is well-known that when an array is passed to a function, it "...
Nar.hhs's user avatar
  • 68
0 votes
0 answers
141 views

From another thread I found that indeed allocates enough storage to hold at least n elements, but it doesn't actually fill the container with any elements If elements are already allocated why ...
sat0sh1c's user avatar
6 votes
1 answer
170 views

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 ...
Movoid's user avatar
  • 63
-1 votes
1 answer
173 views

#include <iostream> using namespace std; int* apply_all(int* arr1, int size1, int* arr2, int size2){ int* on_the_heap = new int(size1 * size2); int temp = 0; for(int i {0}...
Adepa's user avatar
  • 17
0 votes
6 answers
277 views

I'm working (in C) with a struct that contains arrays of strings. How do I point to one of those strings? To make it a bit clearer, consider: struct Books { char title[MAX1][MAX2]; char author[MAX1][...
dehulst's user avatar
  • 69
3 votes
2 answers
171 views

The code below looks crazy, because I am interested in finding out the limits of what’s possible, and it isn’t exactly what I’m doing in my real code. It compiles without warnings and works as ...
njlarsson's user avatar
  • 2,540
5 votes
1 answer
241 views

How do strict aliasing rules apply to pointers-to-pointers-to-characters? For example, does the following contain undefined behavior? (godbolt) #include <iostream> #include <cstdlib> long ...
Sven Sandberg's user avatar
2 votes
3 answers
199 views

size_t hash(char const[restrict static 32]) [[reproducible]]; This is a function definition from Modern C by Jens Gustedt. I understand that this is a more preferred alternate notation to a 32 char ...
Puscas Raul's user avatar
-2 votes
1 answer
119 views

I was tasked to write up a C program to sort an array using quicksort, without using recursion. Original array: -25, 87, 12, -6, 91, 3, -48, 70, 19, -33, 55, 2, -18, 99, 41, -72, 63, 15, -90, 27, 8, -...
Thunder's user avatar
  • 21
3 votes
1 answer
95 views

A pointer can be safely cast to intptr_t (or uintptr_t) and back (on platforms where those types exist), but what about casting a pointer-to-intptr_t into pointer-to-pointer and using that to modify ...
njlarsson's user avatar
  • 2,540
-2 votes
1 answer
107 views

Can the same -- object construction -- be achieved without mixing pointer and value receivers as per official docs ? https://go.dev/tour/methods/8 The problem is Error() cannot be just made with ...
noname7619's user avatar
  • 3,844
2 votes
1 answer
207 views

int main() { const int length = 10000; double *A = new double[length]; double *first = A; double *last = A + length - 1; delete[] A; ptrdiff_t diff = last - first; } In C++, ...
chqrlie's user avatar
  • 153k
22 votes
1 answer
2k views

#include <stddef.h> #include <stdlib.h> int main(void) { const size_t length = 10000; double* a = malloc(length * sizeof *a); double* first = a; double* last = a + length-1; ...
Rasmus's user avatar
  • 443
2 votes
3 answers
197 views

I have a question about the performance tradeoffs between different ways of storing 2D arrays in memory in C. I know that if I declare a 2D matrix A with dimensions MxN by the following: int (*A)[N] = ...
Martin's user avatar
  • 85
4 votes
3 answers
173 views

void do_something(void *data) { t_my_type *mt; mt = (t_my_type *)data; //mt->my_var = ... //... } void do_something2(t_my_type *mt) { //mt->my_var = ... //.....
Atti's user avatar
  • 91
10 votes
3 answers
621 views

This is the code I have written for searching a node in a tree as a part of my assignment: typedef struct NODE { int key; struct NODE *left, *right; } Node; Node *search(Node *head, int key)...
M.B.'s user avatar
  • 279
1 vote
1 answer
173 views

I'm trying to wrap my head around the reachability precondition of std::launder in the context of (abstract) c++ pointer model. Here's a code snippet that makes me confused as to whether this ...
Die4Toast's user avatar
  • 123
4 votes
2 answers
139 views

I have a struct which holds some pointers to data it does not own, e.g.: struct s { int* x; int* y; }; I can then pass it as a const pointer itself: int foo(const struct s* s) { return *s-...
Dominik Kaszewski's user avatar
9 votes
1 answer
236 views

I was doing some reading on std::launder, and I thought of a scenario that the standard didn't seem to address. cppreference.com defines std::launder(T *p) like this: Formally, given the pointer p ...
Brian Smith's user avatar
6 votes
2 answers
265 views

I have the following structure in my .c file. #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Student { char* name; int age; int id; } Student; ...
iii's user avatar
  • 107
3 votes
2 answers
159 views

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 ...
js980's user avatar
  • 35
2 votes
2 answers
196 views

I have this class: class Socket { [...] pollfd* fdPtr; }; And I have a Manager class that creates the actual pollfd objects and adds them to a std::vector named pollFdsVec, to then poll on ...
desynchedneo's user avatar
1 vote
3 answers
88 views

I have pointer into a delay line that I wish to update. If the pointer (p) goes past the end of the buffer, I wish to wrap around to the start plus some value. _Complex float buffer[BUF_LEN]; ...
james's user avatar
  • 21
3 votes
1 answer
168 views

There are two classes A and B, each having functions f and g (with the same signatures). The function bool is_A(uintptr_t ptr) returns true if and only if ptr points to an instance of A, and ...
AustinBest's user avatar
1 vote
1 answer
79 views

I have an array of pointers to structs in C as typedef struct{ int elem1; char *elem2; ... }example_t; example_t *array[MAX_SIZE]; I did generic functions for add or delete pointers to ...
Daniel Muñoz's user avatar
0 votes
0 answers
59 views

Question Can you provide some insight what is the role of the rvalue_from_python_stage1_data convertible pointer and how is being deallocated (memory managed) in boost python? Background According to ...
user1221647's user avatar

1
2 3 4 5
1147