Skip to main content
Filter by
Sorted by
Tagged with
Best practices
0 votes
3 replies
46 views

I've been researching this for a day and I've found a lot of conflicting information on multiple websites. I'm writing a simple test that will allocate and initialize a char** (array of strings, char*...
Maryann's user avatar
  • 39
1 vote
0 answers
57 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
0 votes
1 answer
77 views

I simulated a scenario in Java that leads to severe glibc memory fragmentation. Step one: simulate a multithreaded environment with 600 threads. Step two: every second, start two new threads that ...
Forever's user avatar
  • 41
Best practices
1 vote
7 replies
170 views

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 ...
Klumpy7's user avatar
  • 123
2 votes
2 answers
152 views

I'm refreshing my C skills (been decades) and ran across something I don't quite understand. I'm working on some code that involves a lot of bit shifting, masking, etc. I have one function that ...
Maryann's user avatar
  • 39
2 votes
2 answers
88 views

I am trying to catch memory-related bugs such as use-after-free by mallopt(M_PERTURB, <value>). According to the doc, the memory will be initialized to value when it has been released by free. ...
davidhcefx's user avatar
-4 votes
0 answers
102 views

While implementing a Dijkstra-Search on a Graph, I call a seperate method to perform the search, handing it a pointer to my nodes and direct values for start/endpoint and array size. Notably,t he ...
Tom Tom's user avatar
-6 votes
2 answers
274 views

I want to try catch the free(hello); so that the free(world); can still be executed for freeing all allocated memory in the main program. int main() { const char *hello = "Hello World"; ...
stackbiz's user avatar
  • 1,914
4 votes
2 answers
224 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
1 vote
0 answers
80 views

We use the mspace feature of Doug Lea's Malloc often referred to as dlmalloc. We have an mspace with 250 MB of objects and free all the objects. After calling mspace_trim() the size is 150 MB. We were ...
brian beuning's user avatar
0 votes
3 answers
265 views

When the array type is a "void *", then it can be allocated by: void **void_array; void_array = malloc(sizeof(void*) * 1); But, if the array type is a struct Hello *, then what should be ...
stackbiz's user avatar
  • 1,914
9 votes
4 answers
1k views

I used malloc to define an index because each line had a different number of elements (simplified example below): int** Index=malloc(2*sizeof(int*)); Index[0]=malloc(2*sizeof(int)); Index[1]=malloc(3*...
Anonymus Anonyma's user avatar
4 votes
5 answers
259 views

I have the following code snippet in C that declares multiple one-dimensional and two-dimensional arrays of type double. double func(double alphas[], double betas[], double rhos[], double **X, ...
sonny's user avatar
  • 49
3 votes
2 answers
128 views

I have written some programs with OMP reduction directive in Fortran and in C. But the data types were simple (int, float, arrays with fixed size, ...) and reduction-identifiers used were implicitly ...
Stef1611's user avatar
  • 2,515
-2 votes
2 answers
136 views

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 ...
Gargi Chaturvedi's user avatar
2 votes
1 answer
112 views

I am trying to implement a custom global allocator for use in my Rust application. The goal is to track memory usage and align to 64 bytes. My allocator uses libc::malloc and libc::free for allocation ...
Ronika Kashyap's user avatar
6 votes
2 answers
263 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
2 votes
3 answers
206 views

I want to write a computer programme that will do the following things : 1a. It will make an array 3 characters long. £££ 2a. It will then initialize the array with the string "{_}" and ...
uran42's user avatar
  • 469
3 votes
2 answers
166 views

Is it safe to call free in the example below: size_t len = 10; char* buffer = static_cast<char*>(malloc(len)); std::string_view sview(buffer, len); free(sview.data()) Why do I need this? I have ...
Njrslv-yndx-cloud's user avatar
0 votes
1 answer
46 views

In a microcontroller without any OS, how does the microcontroller keep track of where a malloc will point to in the heap? char *x; char *y; char *z; x=(char*)malloc(10); y=(char*)malloc(10); free(x); ...
hal's user avatar
  • 1
0 votes
1 answer
138 views

Is it possible to change malloc or new behavior for a somewhat limited scope to actually allocate out of a given buffer that is already allocated? Eg. something like: char buffer[1024]; void main(...
dronus's user avatar
  • 11.4k
2 votes
1 answer
173 views

Suppose I have the following: struct S { char b[256]; }; struct S *buf = malloc(128); buf->b[0] = 1; You may notice that the allocation size is smaller than the structure size, but at no ...
CPlus's user avatar
  • 5,110
0 votes
0 answers
72 views

I am trying to write a C program that should fail due to memory crunch. It just keeps on malloc-ing without free-ing. After each malloc I check if the returned value is zero. If so, I stop with a &...
Arnab Chakraborty's user avatar
31 votes
2 answers
5k views

In case of failure, malloc returns a null pointer. In the following code the latest GCC and clang assume malloc never fails and simple remove the branch #include <cstdlib> int main() { if (!...
Dmitry's user avatar
  • 1,649
3 votes
2 answers
127 views

I've been following an article that walks through implementing primitive versions of malloc and free. After finishing the allocator, I wanted to test it using Valgrind, so I added the following lines ...
younessBct's user avatar
1 vote
1 answer
183 views

I’m investigating how C’s runtime memory allocator behaves in a long-running process that repeatedly: allocates many blocks of variable size with malloc(), finishes its work, and calls free() on all ...
bla-ce's user avatar
  • 13
0 votes
0 answers
85 views

I'm creating a C++ allocator that should over-align the allocated memory by a runtime-determined alignment value. According to https://en.cppreference.com/w/cpp/memory/c/aligned_alloc, the std::...
tmlen's user avatar
  • 9,230
3 votes
1 answer
203 views

I am experimenting with malloc in C to better understand how memory alignment and allocation actually work behind the scenes. Here is the simple code I am running: #include <stdio.h> #include &...
Navid Fayezi's user avatar
1 vote
0 answers
145 views

I'm new to Linux kernel development and need help understanding a memory allocation issue. I defined a structure in my driver like this: typedef struct MyStruct { int a; int b; int c; ...
kaiserlee.xy's user avatar
12 votes
3 answers
698 views

Excerpt from the book "C++ memory management" by Patrice Roy The std::memcpy() function For historical (and C compatibility) reasons, std::memcpy() is special as it can start the lifetime ...
alex_noname's user avatar
  • 33.2k
1 vote
1 answer
91 views

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 ...
Sterticc's user avatar
3 votes
2 answers
124 views

Suppose I have a database in memory that I want to modify. Let's say the tables are phone information and items. The phone information consists of a name up to 99 characters and a character code. The ...
Mike S's user avatar
  • 33
1 vote
2 answers
121 views

I'm not sure why my valgrind is spitting heap errors and I've been tracing my code left and right. I have some code, but I'm not sure if more code is needed. My jobs.h #include "piper.h" #...
minyoung heo's user avatar
0 votes
2 answers
161 views

I have a code where I need to delete last element of array. I made this code but I am wondering is there a more optimised way of doing this? My version: void DeleteLast(int** array, int* size) { ...
Safarov Arthur's user avatar
3 votes
1 answer
193 views

Requirement Track the total memory allocated (malloc) and freed (free). Capture backtraces of all malloc and free calls (for now, logging can be ignored). Current Approach For malloc: I need every ...
Puspaul Halder's user avatar
0 votes
0 answers
137 views

Trying to set the include directory path for llvm build Hi, I am trying to build llvm with snmalloc allocator and as per the cmake options and instructions, I have set LLVM_INTEGRATED_CRT_ALLOC flag ...
Jenet Scaria's user avatar
0 votes
0 answers
43 views

I am very new to allocating. Below is a simplefied version of my code. I need to allocate a struct in which variable data needs to be stored that has a set length when it passes through several ...
Desert Wind's user avatar
1 vote
0 answers
74 views

My C++ application is using own library for memory management. It defines malloc and free and it is statically linked. My application uses few other 3rd party libraries also. I got a crash in one of ...
sampathA's user avatar
4 votes
1 answer
125 views

_malloca from the Windows SDK does a stack allocation when the requested size is small and a heap allocation when it exceeds a certain size, because of that, _malloca requires a call to _freea. I'd ...
Khanh H's user avatar
  • 43
1 vote
2 answers
173 views

I have 3 files. 1.cpp has a function map that uses mmap to do some fixed memory mapping. 2.cpp is a malloc interceptor and 3.cpp has the main function. The problem is ... depending on from where map ...
Preetam Das's user avatar
-2 votes
4 answers
201 views

I am trying to increase the amount of memory I have allocated using malloc. Is there a way to increase the size from 128 bytes to 256 bytes by allocating an additional 128 bytes next to the original ...
Mohd Afzal's user avatar
3 votes
1 answer
119 views

I'm just confused as to why this keeps giving me an error. If I just fread the file in and don't assign the last index to the null terminator, I can free the memory after using it. However, if I do ...
Aidan's user avatar
  • 87
2 votes
1 answer
101 views

I am trying to add memory of my dynamic array using realloc. But in my code, realloc is added extra memory to the array. Suppose initially I want to create array of 5 items than I want to add 3 more ...
user4221591's user avatar
  • 2,236
1 vote
2 answers
51 views

Question: Why do mincore failed with "Invalid argument" in the following code? int main(int argc, char **argv) { char *ptr; unsigned int num_of_byte_allocated = 4; size_t ...
Tran Triet's user avatar
  • 1,349
5 votes
6 answers
531 views

Suppose that I need to malloc three times and I would like to make an early return if one of the malloc calls have failed. My first try: void test(void) { int *a, *b, *c; a = malloc(sizeof(*...
Doohyeon Won's user avatar
0 votes
1 answer
112 views

I'm new to C, coming from another language and I'm getting this error that I understand now but couldn't find how to fix it inside of my code. It's pretty simple but even when debugging with printf or ...
Jimmy's user avatar
  • 45
0 votes
1 answer
60 views

I wanted to create a dynamic stack just for learning purposes, but I ran into a problem: I'd like to push() function be able to handle if the dstack isn't initialized, and it works when I go through ...
rat's user avatar
  • 41
0 votes
3 answers
222 views

I am currently attempting to create a triple pointer to point to and update the contents of a statically allocated 3d array in C. The updating of the elements of the array will be completed by a ...
frank's user avatar
  • 41
0 votes
4 answers
185 views

As far as I know, malloc does not initialize the allocated memory. However, on macOS arm64, all values appear as zero. Why does this phenomenon occur? Is this related to ASLR? #include <stdio.h> ...
Jinwoo Kim's user avatar
0 votes
1 answer
142 views

I'm having an issue where, when I run my already compiled program ./main.exe (gcc main.c -o main.exe) through a Makefile, a realloc call fails when the size goes over 40 bytes. However, if I run ./...
iab's user avatar
  • 3

1
2 3 4 5
188