9,374 questions
Best practices
1
vote
12
replies
128
views
C memory initialization for a char** after a malloc
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*...
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&...
0
votes
1
answer
77
views
Returning memory from glibc malloc non-main arenas to the operating system
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 ...
Best practices
1
vote
7
replies
171
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
2
answers
152
views
Simple C malloc question that prints garbage on multiple invocations [closed]
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 ...
2
votes
2
answers
88
views
mallopt(M_PERTURB) does not perturb the memory on free
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.
...
-4
votes
0
answers
102
views
SIGSEV signal after method call - How do pointers work after pass-by-reference?
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 ...
-6
votes
2
answers
274
views
How to try catch finally in c
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";
...
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() {
...
1
vote
0
answers
80
views
dlmalloc mspace_trim in mspace with all object free
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 ...
0
votes
3
answers
266
views
What to use when malloc array length in c
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 ...
9
votes
4
answers
1k
views
Single-line initialization of array allocated by malloc()
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*...
4
votes
5
answers
260
views
What is the alternative to declare a Variable-Length array in C, that may exceed Stack size, without the need of Dynamic Memory allocation?
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, ...
3
votes
2
answers
128
views
How to do *user defined reduction* on *allocatable array* and *user reduction functions* with openMP in C?
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 ...
-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 ...