All Questions
Tagged with dynamic-allocation or dynamic-memory-allocation
4,183 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&...
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 ...
3
votes
4
answers
143
views
What's wrong with this code, which gets content (chars) from file and stores it in char**?
I have a small project (ed-like text editor for myself. Just want to edit some config files using it) and I have some problems with it.
workspace_file->flc = malloc(sizeof(char *));
...
1
vote
1
answer
121
views
dynamically resizing array in queue implementation [closed]
I'm trying to write a queue that will store strings. Using GDB I can tell that I make a memory allocation error in the function resizeQueue.
Here's the exact message:
Program received signal SIGTRAP, ...
1
vote
1
answer
73
views
Memory management issues for a linear regression program in C
I am planning to make a Linear Regression model using C. It takes a set of m points as input from stdin using scanf. The points are defined as a struct:
typedef struct{
double x;
double y;
} ...
0
votes
0
answers
103
views
Use array slicing to avoid memory allocation [duplicate]
Consider the following excerpt from Prometheus source code:
func (ls Labels) String() string {
var bytea [1024]byte // On stack to avoid memory allocation while building the output.
b := bytes....
1
vote
1
answer
91
views
uninitialized memory when using realloc for an array of structs [closed]
I have a parent struct that has an array of child structs and the length of it, and within each child struct is an array of numbers and the length of it. all of the arrays are defined using pointers ...
-1
votes
1
answer
136
views
Why is there a heap-after-use error here? I am trying to solve a Leetcode problem in C++. I don't see any dangling pointers or refs to freed memory
I am trying to solve LeetCode 2487: Remove Nodes From Linked List, and I’ve implemented a solution in C++ that first reverses the list, then removes nodes that have a greater value node to their left (...
5
votes
0
answers
121
views
C++ std::pmr::unsynchronized_pool_resource for larger pool blocks
I would like to frequently allocate and reuse buffers of up to multiple hundreds of MBs. I want to use a memory pool for this because I know the target machines possess enough memory capacity and I ...
2
votes
0
answers
77
views
How do I resolve the recursive dependency in my page frame allocator (custom OS)?
I’m developing a custom OS and facing a chicken-and-egg problem with my page frame allocator. I need to map a specific page, but if the corresponding PML4 entry is NULL, I must allocate a PDPT. ...
4
votes
2
answers
206
views
Proper Memory Handling for User Input in C
I am trying to correctly handle user input in C, particularly when reading a file path from the user.
However, I have some concerns:
How do I refactor this code to handle dynamic memory allocation.
...
3
votes
2
answers
149
views
How to manage memory for chained array operations in C?
I'm just starting to program in c, so i don't have much experience with manual memory management. Coming from python, syntax like the following is very pleasant:
import numpy as np
b = np.array([1,2,...
0
votes
1
answer
87
views
Segmentation fault when reallocating a variable in C [closed]
Goal: Enable user to input a huge amount of data (text type) in cmd.
I have somewhere in my code a function input_buff(buf, CMD_SIZE); buf is dynamically allocated buffer and CMD_SIZE is fixed to 16.
...
1
vote
1
answer
67
views
String appearing "broken" into parts after being printed in C code
I wrote this code in C where you type a word or sentence (char s[]) and two numbers (l for left and r for right). It's supposed to seperate the string into 3 parts: 1. from start to position l-1 ,2. ...
0
votes
2
answers
335
views
How is memory allocated to class object and it's data members in C++
So we have 2 main memories stack and heap. When I create an simple object of class(A obj) which is heap allocated and if we have data members which require heap allocation and stack allocation then ...
0
votes
2
answers
122
views
Freeing a dynamically allocated string with an internal null byte
The following code allocates 256 bytes for a character array and then replaces the space with \0 (similar to what strtok and strsep do).
#include <stdlib.h>
#include <string.h>
int main() ...
-2
votes
1
answer
161
views
New Operator with and without Parentheses [duplicate]
What is the main difference between using the new operator to create an array with trailing parentheses and without? That is, the difference between the following declarations
void* ptr = new int[5]();...
-1
votes
1
answer
51
views
how to make a linked list with template
i have problems with distributing template. i tried with different syntax, but not a successful way with the error text:
error C2512: 'node': no appropriate default constructor available
for the code
...
3
votes
2
answers
144
views
std::string concatenation in C++
What is the principle of std::string concatenation in C++ ? How does it works in memory allocation ?
I found out while exploring a leetcode card that in Java:
"concatenation works by first ...
0
votes
1
answer
233
views
Arduino ESP32 Guru Meditation Error / stack overflow
I have a Arduino script running for a few years now. Recently I wanted do some minor updates, but I've been running into problems ever since. Even when I run the older version (the version running ...
1
vote
1
answer
111
views
Does malloc assign memory in the same location if you use the same variable name again on every iteration of a loop?
I am writing code to take in a weighted adjacency list in C. Each edge is stored in the form of a struct. I created an array of pointers where each pointer leads to the list for a node. Here's what I ...
3
votes
4
answers
181
views
Handling clean-up in function with multiple allocations
Say I have a function that allocates some struct (object) and has to allocate additional data when setting its fields. Since each allocation can fail, the object creation may only partly succeed. What ...
1
vote
1
answer
129
views
Mismatched allocation/deallocation error using Intel Inspector
Consider the following minimal working example:
program p
type t
integer,allocatable::i
end type
type(t),allocatable::o
allocate(o)
deallocate(o)
end
This code was ...
10
votes
2
answers
1k
views
How to allocate for later placement new "as if by new"
We need to separate allocation and initialization of some heap storage. Unfortunately, client code uses delete p; to delete the pointer. If we had control of the deletion, we could allocate with ::...
0
votes
2
answers
158
views
Using compound literals inside loop in C
Consider this code:
typedef struct my_struct { int a; int b; int c; } my_struct;
void some_function(my_struct *value)
{
// do something
}
int main(void)
{
for (;;) {
some_function(&...