Linked Questions
38 questions linked to/from Why does malloc initialize the values to 0 in gcc?
0 votes
2 answers
541 views
C / Cython: Initial content of an malloc memory allocation [duplicate]
Code: cpdef values(int n): cdef size_t i cdef double * v = <double *> malloc(sizeof(double) * n) if v is NULL: abort() for i in range(n): print v[i] Output: >...
2 votes
1 answer
175 views
diferent behaviour in different environments for uninitialised malloced memory [duplicate]
I was implementing a program related to the use of dynamic allocation in C. Testing the same piece of code on Visual Studio 2017 and on other IDEs (Dev C ++, Codeblocks, etc.) I have different ...
2825 votes
30 answers
414k views
Should I cast the result of malloc (in C)?
In this question, someone suggested in a comment that I should not cast the result of malloc. i.e., I should do this: int *sieve = malloc(sizeof(*sieve) * length); rather than: int *sieve = (int *) ...
977 votes
14 answers
684k views
Difference between malloc and calloc?
What is the difference between doing: ptr = malloc(MAXELEMS * sizeof(char *)); And: ptr = calloc(MAXELEMS, sizeof(char*)); When is it a good idea to use calloc over malloc or vice versa?
304 votes
3 answers
72k views
Why malloc+memset is slower than calloc?
It's known that calloc is different than malloc in that it initializes the memory allocated. With calloc, the memory is set to zero. With malloc, the memory is not cleared. So in everyday work, I ...
9 votes
5 answers
31k views
How to initialize a dynamic int array elements to 0 in C
I created a dynamic array ,and i need to initialize all the members to 0. How can this be done in C? int* array; array = (int*) malloc(n*sizeof(int));
14 votes
6 answers
4k views
Are uninitialized values ever a security risk?
While learning C, I made some mistakes and printed elements of a character array that were uninitialized. If I expand the size of the array to be quite large, say 1 million elements in size and then ...
7 votes
5 answers
25k views
what is the default value after malloc in c? [closed]
I have the following code: int *p; p = (int*)malloc(sizeof(int) * 10); but what is the default value of this int array with 10 elements? are all of them 0?
17 votes
4 answers
758 views
Getting as much uninitialized memory as possible
I'm trying to create a C/C++ program that dumps as much uninitialized memory as possible. The program has to be run by a local user, i.e in user mode. It does not work to use malloc: Why does malloc ...
13 votes
3 answers
3k views
C: memcpy speed on dynamically allocated arrays
I need help with the performance of the following code. It does a memcpy on two dynamically allocated arrays of arbitrary size: int main() { double *a, *b; unsigned n = 10000000, i; a = malloc(...
4 votes
2 answers
13k views
cudaFree is not freeing memory
The code below calculates the dot product of two vectors a and b. The correct result is 8192. When I run it for the first time the result is correct. Then when I run it for the second time the result ...
8 votes
3 answers
1k views
Global variable seems to not occupy any memory space
I want to understand exactly where the global variables are stored in my program. On the stack? On the heap? Somewhere else? So for that I wrote this small code: int global_vector[1000000]; int main ...
4 votes
6 answers
2k views
Why is malloc( ) initializing the value of a newly allocated block of memory?
I'm starting to learn about dynamic memory allocation in C and so far everywhere I've read the malloc() function does NOT initialize the value of the newly allocated block. Has this been changed in ...
8 votes
3 answers
2k views
Copying elements from one character array to another
I wanted to transfer elements from a string to another string, and hence wrote the following program. Initially, I thought that the for loop should execute till the NULL character (including it i.e) ...
5 votes
6 answers
2k views
Is the memory chunk returned by malloc (and its cousins) initialized to Zero?
I wrote a code to test to stress test the memory management of Linux and Windows OS. Just for further tests I went ahead and checked what values are present in the memory returned by malloc(). The ...