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
3
votes
4 answers

Unit testing involving C free()

I'm working in Unix/Linux with C. I have a basic understanding of how memory allocation works, enough to know that if I malloc() then free(), I'm not likely going to actually free an entire page; thus if I use getrusage() before and after a free()…
Michael Conlen
  • 1,959
  • 2
  • 17
  • 19
3
votes
2 answers

malloc for string pointers: how it can be disastrous?

In this Informit C++ guide, I read this: Using malloc() to create a non–POD object causes undefined behavior: //disastrous! std::string *pstr =(std::string*)malloc(sizeof(std::string)); I didn't understand 2 points here : Pointers are PODs. Then…
Swathi Appari
  • 193
  • 1
  • 1
  • 12
3
votes
3 answers

How to use __malloc_hook?

In GNU C Library Reference Manual, there is an example program(p.65), But I don't know what the three sentences: __malloc_hook = old_malloc_hook; old_malloc_hook = __malloc_hook; __malloc_hook = my_malloc_hook; mean. Especailly the second one, who…
Fei Xue
  • 1,995
  • 5
  • 19
  • 29
3
votes
4 answers

Struct initialization problem?

I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment; I want to initialize the struct, and using the malloc() method return a dynamic memory like this Fragment *frag=malloc(10*sizeof(Fragment)); Then I would…
Charlie Epps
  • 3,595
  • 9
  • 33
  • 38
3
votes
5 answers

Why are the contents pointed to by a pointer not changed when memory is deallocated using free()?

I am a newbie when it comes to dynamic memory allocation. When we free the memory using void free(void *ptr) the memory is deallocated but the contents of the pointer are not deleted. Why is that? Is there any difference in more recent C compilers?
Aviral Kumar
  • 814
  • 1
  • 15
  • 40
3
votes
4 answers

C generic linked-list

I have a generic linked-list that holds data of type void* I am trying to populate my list with type struct employee, eventually I would like to destruct the object struct employee as well. Consider this generic linked-list header file (i have…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
3
votes
1 answer

First element offset

Is it a WARRANTY, that offset of first element of structure is 0? To be more accurate, lets consider struct foo { int a; double b; }; struct foo *ptr=malloc(sizeof(struct foo)); int *int_ptr = &ptr->a; free(int_ptr) Is it garantied, that it is…
KAction
  • 1,977
  • 15
  • 31
3
votes
3 answers

A Struct with an Array of Structs with Arrays inside Them (and an Array) inside It: How would I malloc this?

I currently have no code, because I don't know how to do this at all. Could I just, by myself, calculate how many bytes is needed for each lower-level struct and malloc it to it? That's really terrible coding, isn't it. Here's the two structs I'm…
mszegedy
  • 185
  • 1
  • 9
3
votes
2 answers

free() crashes, but only if more than a certain size of memory is allocated

I have a strange problem freeing allocated memory in my mpi program: Here is a code sample that produces the error for me: void *out, *in; int cnt = 2501; //if cnt<=2500: works perfectly. cnt>2500: crashes at free! if((out = malloc(cnt *…
mort
  • 12,988
  • 14
  • 52
  • 97
3
votes
5 answers

How do I use malloc for a two dimensional array of structs? (bus error: 10)

I'm trying to implement a sudoku solver. To do this i am using a struct, shown below, to represent a cell on a sudoku board. I am then declaring a 9x9 array of these structs to represent the board. cell structure: struct cell{ char value; …
MRT89
  • 299
  • 3
  • 7
  • 16
3
votes
3 answers

GetTotalMemory allocation in C

I would like to get the total memory allocated before and after I call a function in order to determine if I have freed everything correctly or not. I'm doing this in C and I'm very rusty so forgive me if this is a naive question. I'm looking for…
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
3
votes
3 answers

sYSMALLOc: Assertion failed - How can I solve?

I'm writing some simple functions to manage graphs. When I run my program the following happens error: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk,…
Ruben Gonçalves
  • 61
  • 1
  • 2
  • 8
3
votes
4 answers

Intricacies of malloc and free

I have two related questions hence I am asking them in this single thread. Q1) How can I confirm if my OS is clearing un-"free"'ed memory (allocated using malloc) automatically when a program terminates? I am using Ubuntu 11.04, 32-bit with…
Abhinav
  • 1,882
  • 1
  • 18
  • 34
3
votes
3 answers

Segmentation Fault when trying to use scanf on a struct

I'm pretty new to c and I'm pretty frustrated at the moment as well. Here's the code I have: typedef struct { char* fName; char* lName; char* pNum; char* address; char* email; } contactInfo; void addContact(){ contactInfo *contact; contact =…
Novacane
  • 119
  • 3
  • 14
2
votes
2 answers

Will this malloc the second structure?

Will p = (users *)malloc(sizeof(users)); create memory for the playlist structure too? Also how can I reference playlist.album using p? struct playlist_ { int album; int track_num; struct playlist_ *next; }; struct users_ { int user_ID; …
Learning C
  • 679
  • 10
  • 27