malloc

How to properly malloc item in struct array hashmap in C

こ雲淡風輕ζ 提交于 2020-01-16 09:11:30
问题 In the following code, I'm using malloc for adding a new item to a hashmap. I thought I've checked all the boxes for properly using malloc, but valgrind says I've got a memory leak on them. Can someone point me to where I've gone wrong? #include <stdlib.h> #include <string.h> typedef struct node { char content[46]; struct node* next; } node; typedef node* hashmap_t; int main (int argc, char *argv[]) { hashmap_t hashtable[1000]; node *n = malloc(sizeof(node)); if(n == NULL) return 0; hashmap_t

Dynamic Allocation of an Array of Strings in C

半世苍凉 提交于 2020-01-15 20:17:54
问题 For home-work, I need to define a function that allocate memory to an array of strings (which is into a struct). The length of each string is given: MAX_WORD_LEN+1 (=10+1) I have to allocate memory for len number of strings, len is recieved in the input. Struct with the array of strings definition (given): struct dict{ int len; char (*dict0)[MAX_WORD_LEN+1]; char (*dict1)[MAX_WORD_LEN+1]; }; I don't understand the declaration char (*dict0)[MAX_WORD_LEN+1]; The function declaration is also

Linux c application memory usage

半城伤御伤魂 提交于 2020-01-15 10:52:25
问题 I have C Linux application which continuously allocates and frees memory (around 200 alloc/free per sec) using malloc, calloc, realloc & free functions. Even though all allocated memory are freed (verified by wrapping *alloc and free), the VmSize, VmRSS & VmData numbers are keep on increasing and finally application is getting killed by OOM killer. Why the VmSize, VmRSS & VmData are keep on increasing? if it is Memory management issue, any pointers to avoid this? I saw this Problem usage

Is there a way to overwrite the malloc/free function in C?

微笑、不失礼 提交于 2020-01-14 09:43:11
问题 Is there a way to hook the malloc/free function call from a C application it self? 回答1: Yes you can. Here's an example program. It compiles and builds with gcc 4.8.2 but does not run since the implementations are not functional. #include <stdlib.h> int main() { int* ip = malloc(sizeof(int)); double* dp = malloc(sizeof(double)); free(ip); free(dp); } void* malloc(size_t s) { return NULL; } void free(void* p) { } 回答2: malloc() and free() are defined in the standard library; when linking code,

Understanding C malloc and sbrk()

余生颓废 提交于 2020-01-14 03:55:09
问题 I am trying to understand the difference between malloc and sbrk in C and how they relate to each other. From what I understand malloc and sbrk are pretty much the same thing but I read that malloc uses sbrk in allocating memory. This is really confusing can someone explain it to me? For example in this program does malloc call sbrk? if so does it simply call sbrk each time it gets called, so for this example 10 times? int main(int argc, char **argv) { int i; void *start_pos, *finish_pos;

Dynamically allocating memory for const char string using malloc()

[亡魂溺海] 提交于 2020-01-13 11:07:06
问题 I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is getaddrinfo() . So, I want to write PCSTR ReadFromIni() . To return a constant string, I plan on allocating memory using malloc() and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file. Is that technique okay? I don't really know what else to do. The following

Basic Malloc/Free

喜你入骨 提交于 2020-01-11 09:52:33
问题 If I have a snippit of my program like this: struct Node *node; while(...){ node = malloc(100); //do stuff with node } This means that every time I loop through the while loop I newly allocate 100 bytes that is pointed to by the node pointer right? If this is true, then how do I free up all the memory that I have made with all the loops if I only have a pointer left pointing to the last malloc that happened? Thanks! 回答1: Please allocate exactly the size you need: malloc(sizeof *node); -- if

Malloc and scanf

馋奶兔 提交于 2020-01-11 09:25:11
问题 I'm fairly competent in a few scripting languages, but I'm finally forcing myself to learn raw C. I'm just playing around with some basic stuff (I/O right now). How can I allocate heap memory, store a string in the allocated memory, and then spit it back out out? This is what I have right now, how can I make it work correctly? #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char *toParseStr = (char*)malloc(10); scanf("Enter a string",&toParseStr); printf("%s"

So malloc doesn't invoke any syscall?

爱⌒轻易说出口 提交于 2020-01-11 05:13:06
问题 Related code: write(-1, "test", sizeof("test")); void * p = malloc(1024); void * p2 = malloc(510); write(-1, "hi", sizeof("hi")); Related strace output: write(4294967295, "test\0", 5) = -1 EBADF (Bad file descriptor) brk(0) = 0x601000 brk(0x622000) = 0x622000 write(4294967295, "hi\0", 3) = -1 EBADF (Bad file descriptor) I'm surprised such low level operation doesn't involve syscall? 回答1: Not every call to malloc invokes a syscall. On my linux desktop malloc allocates a space in 128KB blocks

Why does malloc allocate more memory spaces than I ask for? [duplicate]

一笑奈何 提交于 2020-01-11 04:06:05
问题 This question already has answers here : Malloc vs custom allocator: Malloc has a lot of overhead. Why? (3 answers) Closed 3 years ago . I found that malloc() allocates more memory spaces than I ask for. struct { void *ptr; int var; } msg; // 16 bytes (checked by sizeof()) for (int i = 0; i < 100000000; i++) malloc(sizeof(msg)); As the aforementioned code, malloc() actually allocate 32 bytes per function call (calculated by top ), but valgrind shows only 16 bytes per call indeed. Why does