4,184 questions
0
votes
1
answer
63
views
Unfreeable Memory when Using Capstone
I disassembled a 7,838,304-Byte portion of /usr/lib/x86_64-linux-gnu/dri/i965_dri.so using Capstone as shown below:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#...
1
vote
0
answers
57
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&...
Best practices
1
vote
7
replies
170
views
Memory allocation when using a linked list
So I'm making a linked list for one of my school assignments in C. I found this website that has some helpful code snippets, and I noticed that they use malloc() to allocate more memory every time ...
2
votes
0
answers
88
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 ...
0
votes
0
answers
26
views
How to preallocate memory for a map in odin?
I'm learning odin, and I'm writing a program where the map allocating memory, growing and doing its checks is a considerable part of my program running time, is there a way to have a map where I can ...
3
votes
4
answers
150
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 *));
...
-2
votes
2
answers
136
views
Facing issues with Structs and malloc in an assignment question [closed]
This was a question in a C Programming assignment given to me as a part of last week's assessment. Requesting everyone to kindly explain what needs to be done and how it needs to be done. Kindly note ...
1
vote
1
answer
123
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, ...
0
votes
0
answers
209
views
Jax unable to allocate memory despite memory being free?
I have been having issues allocating memory on the GPU with jax. I am running on a cluster that gives me access to a RTX6000 (24GiB vram) which jax is attempting to allocate to.
Output of jax....
1
vote
1
answer
74
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
16
views
How can I determine why clSVMAlloc failed?
Most OpenCL API calls return a status/error value, either directly or via an out-parameter (example: clCreateBuffer()). While that is not as informative as a long-form string description, it can tell ...
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....
11
votes
2
answers
367
views
Issue with aggregate initialization with the new operator
The following two codes behave as expected:
char* test1 = new char[20]{"abc"};
cout << test1;
Outputs: abc
int size = 20;
char* test1 = new char[size]{'a','b','c'};
cout << ...
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 ...
-3
votes
1
answer
171
views
Memory is not being allocated [closed]
I am sorry if the question is stupid. The problem is that memory for my dynamic array inside a static structure is not being allocated. The reason might be my pc (it is a really old notebook with only ...
-1
votes
1
answer
141
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 (...
0
votes
0
answers
19
views
Setup dynamic allocation for a spark job which is having data rate about 450k
How to setup dynamic allocation for a spark job which is having data rate about 450k?
I tried with the below configurations, but the executor pods are always running with the max executors and it's ...
5
votes
0
answers
124
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 ...
4
votes
3
answers
175
views
How memory address for pointer to arrays is same as an element in 2D array?
Consider the following code:
#include <stdio.h>
int main()
{
int **s = (int**)malloc(3 * sizeof(int*));
for (int i = 0; i < 3; i++)
{
printf("%d\n", s+i);
}
...
2
votes
0
answers
80
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. ...
0
votes
2
answers
110
views
How to dynamically create multiple instances of struct in loop in C? [duplicate]
I have the following code to create one-way list of user-specified number of elements
typedef struct Node{
int val;
struct * Node next;
} Node;
int main(){
int num;
Node * prev = NULL;...
4
votes
2
answers
209
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.
...
1
vote
1
answer
272
views
Why is copying more efficient in Zig remap?
The remap function in Zig's memory allocator interface is supposed to "attempt to expand or shrink memory, allowing relocation". In particular, it mentions that...
A null return value ...
3
votes
2
answers
153
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
2
answers
131
views
Why does realloc(NULL, size) call my malloc wrapper, but realloc(ptr, 0) does not call my free wrapper? [closed]
I have wrapped my dynamic memory functions (malloc, calloc, realloc, and free) to track memory usage in my program.
I have encountered the following:
Calling realloc(NULL, 5) goes directly to my ...
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.
...
0
votes
0
answers
121
views
Heap Memory Stair-Case rising in SwiftUI project
A simple keyboard extension app showing continuous heap memory allocation each time open the keyboard on screen.
For easier understanding see the image below...
Here is the KeyboardViewController:
...
1
vote
1
answer
68
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
103
views
Dynamic array first element remove complexity
I was reading about CPython’s implementation of list.pop(k) method and learned that it takes O(n-k) to remove the kth element of the list since it needs to move the posterior elements one position to ...
0
votes
2
answers
350
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 ...
4
votes
2
answers
137
views
How to fix a C Program bug, preventing Multiple File Creations in a single execution?
I am a beginner, working in VS, in a Microsoft environment, making a To Do List in C. After trying to make the program with the help of Stackoverflow, Chatgpt and a good friend, I manged to do it, ...
0
votes
2
answers
129
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() ...
1
vote
1
answer
177
views
Are objects created with new guaranteed to stay in the same memory location? If so, does new place things to minimize cache misses & fragmentation?
Say for example I create a vector with new like this:
std::vector<int>* pointer_to_vector = new std::vector<int>{2, 4, 6, -1};
Will pointer_to_vector remain valid (not dangling) for the ...
-2
votes
1
answer
164
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
vote
0
answers
17
views
What is the causing my buffer to overrun?
Somehow in one of or more of my loops are overrunning my buffer but I can't catch it. It's either that or somehow my capacity or size is at 0 when it shouldn't be but either way I can't find the issue ...
-1
votes
1
answer
56
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
...
0
votes
0
answers
35
views
valgrind tests failed in speller cs50?
My code failed valgring test.I tried so many times to figure out the error and I gave up.can someone help me to find the error ..
// Implements a dictionary's functionality
#include <ctype.h>
#...
3
votes
2
answers
146
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
99
views
Why the structure pointer "p" in the following code is not updating with the "temp" value assigned to it? [duplicate]
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
};
int main(int argc, char *argv[]) {
struct node *p;
p = NULL;
Append(&...
0
votes
1
answer
235
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
votes
2
answers
135
views
malloc & free in C with multidimensional arrays in C; Whats wrong with the code
I am a beginner and i am working on a code where the multidim arrays are allocated with the malloc, malloc variant. I have to add an Array with a higher dimension (3D instead of 2D).
I discovered a ...
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 ...
0
votes
0
answers
113
views
TLSF Allocator for #![no_std] environment
https://github.com/Lousifr-Illuminos/IlluminOS/tree/main
Little project I'm trying for the sake of learning. It works when attempting to allocate variables sized u8 through u128, as well as creating ...
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 ...
0
votes
0
answers
64
views
How can I access struct functions from external compiled .dll if those structs are not declared in the header file?
I cannot share the actual project code, but I will provide an exact code example for the problem I face.
I am trying to implement my own separate component (project) in the solution (Visual Studio ...
1
vote
1
answer
135
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 ...
2
votes
0
answers
85
views
How can I correctly manage memory block allocation in C without overwriting the main block?
I am working on a C project where the goal is to manage memory blocks using dynamic memory allocation function such as realloc and malloc and store their state in a file called block.dat. The main ...
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
1
answer
138
views
C++ operator << overloading for a 2D dynamic array allocation
I define the square matrix by a 2D dynamic array with the new operator following:
int n, i{0};
int value;
do {
cin >> n;
} while ((n <=0) || (n > 20));
int* pMatrix = new int [n*n];
...
0
votes
2
answers
159
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(&...