Questions tagged [malloc]

The malloc function performs dynamic memory allocation in C and is part of the standard library. Use this tag for questions about usage, behavior and implementations of malloc.

The tag is used for questions related to the use of the malloc() function, part of the C standard library.

malloc(), along with free(), calloc(), and realloc(), is used to explicitly manage memory in C.

malloc() returns the address of a newly allocated block of memory, or NULL if memory allocation fails. The memory returned by malloc is uninitialized, and should be assumed to be filled with random data.

Wikipedia

References

Related Tags

9145 questions
72
votes
8 answers

How do free and malloc work in C?

I'm trying to figure out what would happened if I try to free a pointer "from the middle" for example, look at the following code: char *ptr = (char*)malloc(10*sizeof(char)); for (char i=0 ; i<10 ; ++i) { ptr[i] =…
user238082
  • 723
  • 1
  • 6
  • 7
71
votes
16 answers

How can I get the size of an array from a pointer in C?

I've allocated an "array" of mystruct of size n like this: if (NULL == (p = calloc(sizeof(struct mystruct) * n,1))) { /* handle error */ } Later on, I only have access to p, and no longer have n. Is there a way to determine the length of the array…
Joel
  • 11,431
  • 17
  • 62
  • 72
70
votes
8 answers

Will malloc implementations return free-ed memory back to the system?

I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc…
osgx
  • 90,338
  • 53
  • 357
  • 513
68
votes
8 answers

malloc() vs. HeapAlloc()

What is the difference between malloc() and HeapAlloc()? As far as I understand malloc allocates memory from the heap, just as HeapAlloc, right? So what is the difference?
TCS
  • 5,790
  • 5
  • 54
  • 86
68
votes
8 answers

Using malloc for allocation of multi-dimensional arrays with different row lengths

I have the following C code : int *a; size_t size = 2000*sizeof(int); a = malloc(size); which works fine. But if I have the following : char **b = malloc(2000*sizeof *b); where every element of b has different length. How is it possible to do the…
asel
  • 1,099
  • 5
  • 14
  • 18
68
votes
3 answers

Free memory allocated in a different function?

I'm trying to learn C and I'm currently trying to write a basic stack data structure, but I can't seem to get basic malloc/free right. Here's the code I've been using (I'm just posting a small part here to illustrate a specific problem, not the…
Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
66
votes
3 answers

Why does the speed of memcpy() drop dramatically every 4KB?

I tested the speed of memcpy() noticing the speed drops dramatically at i*4KB. The result is as follow: the Y-axis is the speed(MB/second) and the X-axis is the size of buffer for memcpy(), increasing from 1KB to 2MB. Subfigure 2 and Subfigure 3…
foool
  • 1,462
  • 1
  • 15
  • 29
66
votes
11 answers

What happens to memory after '\0' in a C string?

Surprisingly simple/stupid/basic question, but I have no idea: Suppose I want to return the user of my function a C-string, whose length I do not know at the beginning of the function. I can place only an upper bound on the length at the outset,…
Erika Electra
  • 1,854
  • 3
  • 20
  • 31
63
votes
6 answers

In C, how would I choose whether to return a struct or a pointer to a struct?

Working on my C muscle lately and looking through the many libraries I've been working with its certainly gave me a good idea of what is good practice. One thing that I have NOT seen is a function that returns a struct: something_t make_something()…
Dellowar
  • 3,160
  • 1
  • 18
  • 37
62
votes
10 answers

Maximum memory which malloc can allocate

I was trying to figure out how much memory I can malloc to maximum extent on my machine (1 Gb RAM 160 Gb HD Windows platform). I read that the maximum memory malloc can allocate is limited to physical memory (on heap). Also when a program exceeds…
Vikas
  • 1,422
  • 2
  • 12
  • 16
60
votes
4 answers

Dynamically create an array of strings with malloc

I am trying to create an array of strings in C using malloc. The number of strings that the array will hold can change at run time, but the length of the strings will always be consistent. I've attempted this (see below), but am having trouble, any…
Chris
  • 7,996
  • 11
  • 66
  • 98
59
votes
5 answers

What does the first "c" stand for in "calloc"?

A student asked the question and I didn't know for sure. Guesses include: "counted", "clearing", "chunked", "complete", ... The standard library documentation doesn't say what it stands for and there aren't similarly named functions that would…
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
59
votes
4 answers

Xcode Guard Malloc and on Device Debugging: 'libgmalloc.dylib' image not found

I enabled memory checking in Xcode (Edit Scheme -> Options). I'm now getting the following when I perform on device debugging: dyld: could not load inserted library '/usr/lib/libgmalloc.dylib' because image not found. The measure was taken…
jww
  • 97,681
  • 90
  • 411
  • 885
58
votes
5 answers

Time complexity of memory allocation

What is the time complexity of dynamic memory allocation using new, malloc, etc.? I know very little about how memory allocators are implemented, but I assume the answer is that it depends on the implementation. Therefore, please answer for some…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
57
votes
10 answers

How to dynamically allocate memory space for a string and get that string from user?

I want to read input from user using C program. I don't want to use array like, char names[50]; because if the user gives string of length 10, then the remaining spaces are wasted. If I use character pointer like, char *names; then I need to…
Dinesh
  • 16,014
  • 23
  • 80
  • 122