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
1 answer

how to replace default malloc by code

I want to replace default malloc and add some statistics as well as leak detections and other behavious to malloc functions. I have seen some other imlementations like gperftool and jemlloc. They can replace the default malloc by linking with their…
gajia
  • 33
  • 1
  • 3
3
votes
3 answers

Is there any performance difference between malloc and new in C++

I've seen a couple of posts about the differences between malloc and new and I do understand that. However, none of those posts talks about performance, so I was wondering if there was any performance difference between those two or if the compiler…
OneMore
  • 1,139
  • 2
  • 9
  • 19
3
votes
4 answers

Allocating memory confusion in C

I've done alot of looking around, but still can't get my head around it, lets say I have struct: struct some_struct { int x; int y; char *some_string; }; Lets say we have the above struct, how would you allocate some memory for the…
chutsu
  • 13,612
  • 19
  • 65
  • 86
3
votes
5 answers

scanf() doesn't work in malloc struct members

I define a struct data type: typedef struct LinkNode LinkNode; struct LinkNode { char *name; LinkNode *next; }; And call it in main(): Example1: int main() { LinkNode *pnode = (LinkNode *) malloc(sizeof(LinkNode)); scanf("%s",…
fishiwhj
  • 819
  • 2
  • 11
  • 22
3
votes
1 answer

cuda and cudamalloc allocation large block of memory fails

I have a GTX570 with 2Gb of memory, when I try to allocate more memory with one cudamalloc call than about 804Mb I get into to trouble. Anyone any ideas to why that is? It is my first call so I doubt it is fragmentation. No problem: Memory…
Aktaeon
  • 189
  • 2
  • 14
3
votes
3 answers

Lowest possible memory address on modern OS

I've recently been pointed into one of my C programs that, should the start address of the memory block be low enough, one of my tests would fail as a consequence of wrapping around zero, resulting in a crash. At first i thought "this is a nasty…
Cyan
  • 13,248
  • 8
  • 43
  • 78
3
votes
7 answers

compilation break when reallocating the memory in C

I'm trying an example int main(){ int count = 5; char *a = "JOSUA"; char *c = "JANETALAE"; char *b = NULL; char *d = NULL; b = (char *)malloc(sizeof(char)*5); b = a; if(b == NULL){ printf("\n malloc…
Angus
  • 12,133
  • 29
  • 96
  • 151
3
votes
2 answers

Will pthread_detach manage my memory for me?

Suppose I have the following code: while(TRUE) { pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t)); pthread_create(thread, NULL, someFunction, someArgument); pthread_detach(*thread); sleep(10); } Will the detached thread free the…
jbleners
  • 1,023
  • 1
  • 8
  • 14
3
votes
1 answer

MALLOC_CHECK_ not reporting violations

I have enabled MALLOC_CHECK_ by setting it to 1 (tried with 2 and 3 as well) But I don't see it reporting any issues with the following c++ program: int n = atoi(argv[1]); std::cout<<"n = "<
user947158
  • 73
  • 4
3
votes
1 answer

Weird result when reading file

When I read information from a file into my phonebook program and try to view it via the contacts list, the list is blank the first time you try and view it, but if you choose to check it again, the content from the file is there. Even weirder, when…
Jcmoney1010
  • 912
  • 7
  • 18
  • 41
3
votes
3 answers

Allocate a 2d array in C with one dimension fixed

I want to dynamically allocate 1 dimension of a 2D array (the other dimension is given). Does this work: int NCOLS = 20; // nrows = user input... double *arr[NCOLS]; arr = (double *)malloc(sizeof(double)*nrows); and to free it: free(arr)
user308827
  • 21,227
  • 87
  • 254
  • 417
3
votes
2 answers

Malloc implementation with C

I am trying to implement Malloc manually in a C project. Here is my code: void *Mem_Alloc(int size) { struct Node *p, *prevp = head; if (fitPolicy == P_BESTFIT) { } if (fitPolicy == P_FIRSTFIT) { for (p = prevp->next;…
3
votes
2 answers

Realloc an int array

I'm trying to create an array to hold an int, then when another int is to be added increase it in size to hold another int.. and so on.. I know it's not an efficient use of realloc, but it's proof on concept more than anything else. Just to get it…
Draconian Times
  • 319
  • 1
  • 3
  • 12
3
votes
3 answers

How to create a dynamic array of strings in C using malloc

How to create an array of strings when there isn't a fixed length of items or characters. I'm new to pointers and c in general and I couldn't understand the other solutions posted on here so my solution is posted below. Hopefully it helps someone…
Cadell Christo
  • 3,105
  • 3
  • 21
  • 19
3
votes
3 answers

What does memcpy do exactly in this program?

I am writing a program where the input will be taken from stdin. The first input will be an integer which says the number of strings to be read from stdin. I just read the string character-by-character into a dynamically allocated memory and…
user235273