57,322 questions
0
votes
0
answers
25
views
I made a todo list matrix now how am I supposed to make it pointer based?
How do you initialize matrix value in c but in pointer?
void addTodo(char *todo)
{
int j = 0;
for (j; j < strlen(todo); j++)
{
matrix[CurrentTodoIndex1][j] = todo[j];// I wanna ...
0
votes
4
answers
116
views
heap-use-after-free when incorrectly assigning pointers during binary tree inversion [closed]
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 ==...
1
vote
1
answer
127
views
Why does my multithreading/pointers code print random values?
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>
#...
1
vote
0
answers
56
views
Reassigning a Matlab pointer in a loop without a memory leak
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 ...
2
votes
1
answer
154
views
Are the iterators of `map<key, value, greater<>>` and `map<key, value, less<>>` guaranteed to be the same type?
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 ...
0
votes
1
answer
78
views
Using std::map to look up function handlers [duplicate]
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, ...
3
votes
1
answer
117
views
How do I cast a *mut T to a *mut [T]?
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 ...
-4
votes
1
answer
211
views
Visual studio throwing an error when I name my class "floor" [duplicate]
Ok so I was messing around with some OOPs in visual studio 2022 community edition in C++, when I came across such a baffling problem that I don't even know where to start searching for it.
#include &...
1
vote
0
answers
83
views
How to properly allocate dynamic memory for matrix operations in C [duplicate]
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 ...
2
votes
4
answers
236
views
Two pointers referencing the same memory location, possible to make null if we deallocate the space?
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 ...
4
votes
2
answers
224
views
How to free the value inside struct in c
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() {
...
4
votes
1
answer
133
views
Why don’t pointers appear in Python Tutor’s memory visualization in C?
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 ...
0
votes
0
answers
140
views
Does accessing the contents of the string after calling reserve causes UB? [duplicate]
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 ...
0
votes
1
answer
169
views
How to release dynamic memory on the heap in called function C++
#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}...
3
votes
2
answers
163
views
Accessing a struct member from what is not the same struct type
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 ...
2
votes
3
answers
188
views
What is the role of restrict here?
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 ...
3
votes
1
answer
93
views
Casting pointer-to-intptr_t to pointer-to-pointer
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 ...
0
votes
0
answers
85
views
Dodgy void pointer arithmetic in C [duplicate]
I want to write (in C) library routines which create and use arrays of items whose size is unknown at compile time. So I create an 'array':
void *item_array=malloc(arr_sz*item_sz);
and a function ...
22
votes
1
answer
2k
views
Is difference between two pointers pointing to the same deallocated array well-defined in C?
#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;
...
4
votes
3
answers
170
views
Passing a custom data type as a void pointer to a function
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 = ...
//.....
1
vote
1
answer
163
views
std::launder reachability precondition with data (void) pointers
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 ...
9
votes
1
answer
230
views
std::launder when there are two objects in the same memory location
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 ...
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
vote
3
answers
83
views
How do I update an address with a constant minus an address in a circular buffer
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];
...
1
vote
1
answer
77
views
Level of type casting of pointers?
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 ...