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
24
votes
11 answers

Should I bother detecting OOM (out of memory) errors in my C code?

I've devoted a large number of lines of C code to cleanup-labels/conditionals for failed memory allocation (indicated by the alloc family returning NULL). I was taught that this was a good practice so that, on memory failure, an appropriate error…
cdleary
  • 69,512
  • 53
  • 163
  • 191
24
votes
2 answers

Allocating less memory than the specified size of a pointer-to-array

In C, is it "legal" to under-allocate memory to a pointer-to-array if we then only access elements that fall within the allocated memory? Or does this invoke undefined behavior? int (*foo)[ 10 ]; //Pointer to array of 10 ints foo =…
Jackson Allan
  • 727
  • 3
  • 11
24
votes
2 answers

malloc - invalid conversion from void* to double*

I want to write a function that creates a copy of a double array using pointers. This is my code so far: #include #include double* copy (double *array, int size) { double *v=malloc(sizeof(double)*size); for (int i=0;…
Diana
  • 1,417
  • 5
  • 25
  • 48
24
votes
7 answers

Trying to use free() to understand how it works

To understand the usage of free in the C programming language I tried running this code on Ubuntu, but on running the EXE file I am receiving a SIGABRT error. Why is the program not exiting normally? #include #include int…
user3314983
  • 405
  • 3
  • 11
24
votes
4 answers

Find malloc() array length in C?

Possible Duplicate: How to find the sizeof(a pointer pointing to an array) I'm learning how to create a dynamic array in C, but have come across an issue I can't figure out. If I use the code: int num[10]; for (int i = 0; i < 10; i++) { …
GCBenson
  • 516
  • 1
  • 3
  • 9
24
votes
17 answers

Why do you specify the size when using malloc in C?

Take the following code : int *p = malloc(2 * sizeof *p); p[0] = 10; //Using the two spaces I p[1] = 20; //allocated with malloc before. p[2] = 30; //Using another space that I didn't allocate for. printf("%d", *(p+1)); //Correctly prints…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
23
votes
4 answers

Which guarantees does malloc make about memory alignment?

I came across the following code: int main() { char *A=(char *)malloc(20); char *B=(char *)malloc(10); char *C=(char *)malloc(10); printf("\n%d",A); printf("\t%d",B); printf("\t%d\n",C); return 0; } //output-- …
Amol Sharma
  • 1,521
  • 7
  • 20
  • 40
23
votes
8 answers

Linked Lists in C without malloc

#include typedef struct node { int i; struct node *next; }node; node getnode(int a) { struct node n; n.i=a; n.next=NULL; return n; } main() { int i; node newtemp,root,temp; …
letsc
  • 2,075
  • 5
  • 24
  • 43
23
votes
6 answers

Scope of malloc used in a function

When a function returns, is the memory allocated via malloc freed? Or can it still be accessed in the main() function using pointers? eg. void function(int *a) { a=(int *)malloc(sizeof(int)); *a=10; } int main() { int *num; …
user191776
22
votes
1 answer

Call to malloc failing in gdb session

I am trying to debug a C program and gdb is telling me there is a segfault on line 329 of a certain function. So I set a break point for that function and I am trying to step through it. However, whenever I hit line 68 I get this complaint from…
Schemer
  • 1,635
  • 4
  • 19
  • 39
22
votes
5 answers

How to use a C++ string in a structure when malloc()-ing the same structure?

I wrote the following example program but it crashes with segfault. The problem seems to be with using malloc and std::strings in the structure. #include #include #include struct example { std::string data; }; int…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
22
votes
7 answers

When to allocate memory to char *

I am bit confused when to allocate memory to a char * and when to point it to a const string. Yes, I understand that if I wish to modify the string, I need to allocate it memory. But in cases when I don't wish to modify the string to which I point…
sdn_world
  • 263
  • 2
  • 10
22
votes
1 answer

"Semantic issue: Implicitly declaring library function 'malloc' with type 'void *(unsigned long)'"

I have a block of code where I'm trying to grab an expression inside of parentheses and then use it. At the point where the below code begins, I am in the middle of iterating through a character array and pcc is the pointer to the current character,…
Lily Carter
  • 273
  • 1
  • 2
  • 5
22
votes
2 answers

what happens when we call Malloc with negative parameter?

7.22.3.4 The malloc function The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate. Prototype: void *malloc(size_t size); I tried passing a negative value as a parameter:…
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
22
votes
5 answers

CPU and memory usage of jemalloc as compared to glibc malloc

I had recently learnt about jemalloc, it is the memory allocator used by firefox. I have tried integrating jemalloc into my system by overriding new and delete operator and calling the jemalloc equivalents of malloc and free i.e je_malloc and…
deb
  • 631
  • 1
  • 5
  • 15