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
5 answers

Cannot understand return value of sizeof()

I have this in my code: int x = 4; char* array = malloc(x*sizeof(char)); size_t arraysize = sizeof (array); printf("arraysize: %zu\n", arraysize); This code prints out, arraysize: 8 Why is it 8 and not 4? (Since 4*sizeof(char) = 4 * 1)
Chris
  • 3,619
  • 8
  • 44
  • 64
3
votes
5 answers

How to pass malloced variable into a struct to a newly created struct in C

Let's take this example: struct args{ char *fname; } int main(void){ struct args tArg; tArg.fname = malloc(10); strcpy(tArg.fname, "ciao"); pthread_create(&p1, NULL, thread, (void *)&tArg); pthread_join(p1, NULL); …
polslinux
  • 1,739
  • 9
  • 34
  • 73
3
votes
1 answer

malloc in Release vs Debug (VC 2012)

I wrote a quick and dirty program to leak memory by calling malloc repeatedly. I noticed when I ran my program in Debug configuration (in VS 2012) my program correctly consumes gigabytes of memory and keeps going until the page file is full (the…
Dai
  • 141,631
  • 28
  • 261
  • 374
3
votes
5 answers

Malloc syntax in C

In the books I read that the syntax for malloc is malloc(sizeof(int)) but in one of doubly linked list program I see the following: newnode=(struct node *)malloc(sizeof(struct node)) What is (struct node*) doing here? What is this entire code…
Shy Student
  • 99
  • 1
  • 1
  • 7
3
votes
1 answer

Malloc all available memory for a hash table

I want to construct a hash table that's as large as reasonably possible on a machine. I was thinking that at initialization time I would claim a large block of memory for use by the hash table, but it's not clear to me what the best way to do this…
Michael McLoughlin
  • 1,062
  • 1
  • 11
  • 14
3
votes
5 answers

Array in C and malloc

1.How the two dimension stored in the memory, are they consecutive? (I mean the int[M][N], not the dynamic allocation, I think the int[M][N] happened in the stack area, so continual, isn't it?) 2.Does the area allocated by malloc must is…
iLeoDo
  • 397
  • 3
  • 11
3
votes
2 answers

Freeing pointers

This is just a general question, but for example on windows, if i create a pointer to a hostent struct to use with gethostbyname() do i have to dealocate memory of that pointer or is it handled for me. I am under the assumption that since I did not…
randy newfield
  • 1,221
  • 3
  • 25
  • 38
3
votes
1 answer

C malloc valgrind invalid write of size

I've had my fair share malloc invalid writes (and the many examples on this site) but I still have trouble pointing out what's causing some. Here I have an adjacency matrix to use for graphs and when allocating, I get the invalid write from valgrind…
Pete Jodo
  • 465
  • 1
  • 7
  • 19
3
votes
2 answers

Create and resize dynamic array without C libraries?

Is there a way to actually create dynamic arrays in C without having to use the stdlib? Malloc requires the stdlib.h library, I am not allowed to use this in my project. If anyone has any ideas, please share? Thanks.
Scott
  • 41
  • 1
3
votes
3 answers

C array of pointers and malloc

I'm trying to get data from std stored into an array, using pointers. The main declares d as int *d; and the function is called using x = getdata(&d); When I step through it with gdb it dies at the first iteration of the for loop, when it tries…
AdamTopi
  • 63
  • 1
  • 3
3
votes
2 answers

Is it bad to use a malloc loop to guarantee a result from malloc?

Is it bad practice to allocate memory like this?: FOO *foo; while (!(foo = malloc(sizeof(FOO)))) ;
Matt
  • 21,026
  • 18
  • 63
  • 115
3
votes
2 answers

Odd malloc crash

I have the following code that has been working for the last couple of months, but have recently started to crash on occasion (when running in a multi-threaded application): struct some_struct { char* m_str1; char* m_str2; } struct…
3
votes
4 answers

segfault while sending buffer pointer as pass-by-value to a function, inside which reallocate happens on that buffer

If I have the following code, where I allocate my memory in the main function and then pass it to a function, which fills it for me like this: main() { char *bar = (char*) malloc(sizeof(char)); while(1) { foo(bar); …
Effarem
  • 43
  • 2
3
votes
3 answers

Any function to query the size of an allocated block?

I realize that any such function is likely to be non standard, but that's ok for my use case. Basically, I need a method (even if it's only exposed through glibc's syscall() interface) that I can pass a pointer to (a pointer that was returned by a…
dicroce
  • 45,396
  • 28
  • 101
  • 140
3
votes
2 answers

Why does this implementation of the C++ 'new' operator work?

I've found out that the C++ compiler for AVR uCs doesn't support the new and delete operators, but also that there is a quick fix: void * operator new(size_t size) { return malloc(size); } void operator delete(void * ptr) { free(ptr);…
corazza
  • 31,222
  • 37
  • 115
  • 186