Linked Questions

0 votes
2 answers
541 views

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: >...
embert's user avatar
  • 7,652
2 votes
1 answer
175 views

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 ...
arst3k's user avatar
  • 1,017
2825 votes
30 answers
414k views

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 *) ...
Patrick McDonald's user avatar
977 votes
14 answers
684k views

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?
user105033's user avatar
  • 19.7k
304 votes
3 answers
72k views

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 ...
kingkai's user avatar
  • 3,759
9 votes
5 answers
31k views

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));
Salih Erikci's user avatar
  • 5,095
14 votes
6 answers
4k views

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 ...
EMiller's user avatar
  • 2,862
7 votes
5 answers
25k views

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?
user2131316's user avatar
  • 3,327
17 votes
4 answers
758 views

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 ...
user144437's user avatar
  • 1,173
13 votes
3 answers
3k views

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(...
angainor's user avatar
  • 11.8k
4 votes
2 answers
13k views

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 ...
ZviBar's user avatar
  • 10.1k
8 votes
3 answers
1k views

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 ...
user avatar
4 votes
6 answers
2k views

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 ...
Italo Grossi's user avatar
8 votes
3 answers
2k views

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) ...
Ranjan Srinivas's user avatar
5 votes
6 answers
2k views

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 ...
coder's user avatar
  • 183

15 30 50 per page