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

8189 questions
2
votes
0 answers

Runtime undefined errors from my shared library

I face one issue in my customized malloc(). My software First of all, let me explain my software: My software is huge and is composed of multiple static libraries. As one of static libraries has the customized malloc(), whose main function is a…
tech-lover
  • 21
  • 2
2
votes
1 answer

Dynamic nested structure arrays

I am attempting to use a set of nested structures; one which defines a single co-ordinate in 3D (coordinate) and one which describes certain properties about a rectangle (rectangle). The number of rectangles dynamically changes at runtime and malloc…
2
votes
3 answers

How to use realloc to shorten a string array size

I've tried to take a certain string, have the string set for a certain size, 50 chars, then after all of the strings gets inputted, it will sort them, then realloc the sizes from 50 chars to the length of the string that the user wrote, if at first…
2
votes
2 answers

Why C standard library does not provide a function for knowing the size of allocated memory?

When some amount of memory is dynamically allocated via calling malloc(), the OS internally stores the amount of allocated memory somehow (to track the used memory etc.), so we only provide the pointer to free() when we don't need that memory chunk…
trolley813
  • 664
  • 6
  • 14
2
votes
2 answers

Why do we use bitwise operators & and ~ instead of mathematical operators % when implementing aligned malloc?

When studying about Memory Management of the Linux operating system, I see that the general solution to implement the aligned malloc function is the following code: void *aligned_malloc(size_t required_bytes, size_t alignment) { void *p1; //…
2
votes
1 answer

In Linux(RedHat) , C function malloc_stats() shows different values compared to /proc//stat resident memory size

e.g. For a process running in Redhat linux as per /proc/{pid}/stat's resident pages * page size => 30 GB as per malloc_stats() => 2.5 GB any idea why this happens ? Arena 0: system bytes = 465162240 in use bytes = 465037200 Arena 1: system…
aKumara
  • 181
  • 8
2
votes
1 answer

C++: Is memcpy to POD based sub-object UB?

Let's go to the code: extern "C" { #include "pod-struct-T.h" #include "malloc-and-initialize-one-T.h" } struct TCpp : T { TCpp() { T* ptr_t = malloc_and_initialize_one_T(); T* this_t = static_cast(this); …
Peregring-lk
  • 9,237
  • 6
  • 37
  • 86
2
votes
2 answers

What syscall does malloc use?

I am studying memory management and have a question about how malloc works. The malloc man page states that: Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2). When allocating blocks …
2
votes
3 answers

Can I constantly malloc memory without free?

Here is my code: struct movie { char movie_name[30]; float score; struct movie *next; }; typedef struct movie *p_movie; void print_all(struct movie *head); int main() { p_movie head = NULL; p_movie new_node =…
tohardtme
  • 57
  • 3
2
votes
1 answer

In glibc <= 2.23, why is `0;` used in mutex_lock macro?

For glibc <= 2.23, it looks like the generic definition of malloc's mutex_lock macro uses an int as a mutex. 1 means in use, and 0 means free. It defines this generic set of macros: typedef int mutex_t # define mutex_init(m) (*(m) = 0) #…
Omar Darwish
  • 1,222
  • 2
  • 11
  • 20
2
votes
1 answer

Allocating memory for structured pointer

Recently was handling with this exercise: Count the size of each element in an array. Create a function my_count_on_it, which receives a string array as a parameter and returns an array with the length of each string. Structures: typedef struct…
user13377488
2
votes
3 answers

main.c: warning: passing argument of ‘’ from incompatible pointer type

I'm trying to pass and return a two-dimension dynamic array by parameter, however it always shows this warning I mentioned in the question. I know there's something wrong with the array parameter, but I couldn't find in any book or a website how to…
ch3055
  • 29
  • 2
2
votes
4 answers

Do I still need to set ptr to NULL if I use memset earlier?

For example: struct sth { int t; char *p; struct sth *next; } And the init code: struct sth *data = malloc(sizeof(*data)); memset(data, 0, sizeof(*data)); data->t = 0; // A data->p = NULL; // B data->next = NULL; //…
vego
  • 545
  • 3
  • 11
2
votes
3 answers

trouble with structs and malloc()/free()

I'm trying to map a doubly linked list to a GUI. I basically create a button structure for every node in the list, map the nodes parameters to the buttons parameters, and then display them on the screen. I can add a lot of buttons, more than 500,…
rawbus
  • 27
  • 6
2
votes
3 answers

Why use fixed length allocation instead of static allocation?

I understand that what I'm asking may be quite simple for some of you, but bear with me, I'm trying to understand memory management. Since we use a certain fixed length (N) for the size, why we also do this: int *arr = (int*)malloc(N *…
karlgzafiris
  • 2,695
  • 1
  • 22
  • 28
1 2 3
99
100