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

invalid application of 'sizeof' to incomplete type list struct C

I'm trying to implement an replacement algorithm that deals with page faults. So i'm trying to creat a circular linked list using malloc and im getting the following error: "invalid application of sizeof' to incomplete typepageInMemory'.following is…
joseph
  • 353
  • 3
  • 6
  • 10
3
votes
5 answers

How to copy local std::string to const char *?

const char* getString() { std::string myString = "Hello!"; return myString.str().c_str(); } How can I get this function to return a const char * which will live past the local scope in which it is declared? I believe I need to use malloc, but…
RouteMapper
  • 2,484
  • 1
  • 26
  • 45
3
votes
5 answers

Address of a pointer is equal to a pointer to pointer?

I'm trying to understand how the pointers in this piece of code works: void allocateArray( int **arr, size_t size, int value ) { *arr = malloc( size * sizeof( int )); if ( *arr != NULL ) for ( size_t i = 0; i < size; ++i ) …
nvmd
  • 51
  • 1
  • 8
3
votes
2 answers

Why does my program stop crashing if I call malloc instead of GetMem?

I am calling a C DLL from a Delphi 2009 application and I keep getting errors when memory allocated by GetMem or AllocMem is passed to the DLL. The only way around I could avoid these errors was by using malloc from msvcrt.dll. What is malloc doing…
user207886
3
votes
3 answers

C Casting a variable of type double to a variable of type char when freeing memory

While dealing with a library, in the provided example source code I found the following code: x = (double *) malloc (narcs * sizeof (double)); dj = (double *) malloc (narcs * sizeof (double)); pi = (double *) malloc (nnodes * sizeof…
Pedro Dias
  • 78
  • 2
  • 4
3
votes
3 answers

malloc results in segmentation fault after mprotect

I'm getting a segmentation fault the first time I call malloc() after I protect a memory region with mprotect(). This is a code sniplet that does the memory allocation the the protection: #define PAGESIZE 4096 void* paalloc(int size){ // Allocates…
hanno
  • 6,401
  • 8
  • 48
  • 80
3
votes
2 answers

Allocate two-dimensional array

How can I allocate a bi-dimensional array using malloc? This is my current code: typedef struct object product, *pprod; struct object{ int type; int quantity; pprod next; }; pprod t[4][3]; Thanks a lot for your help.
3
votes
3 answers

Dynamic Memory Allocation for Arrays in C

So I'm trying to create a dynamic array in C, using malloc, but for some reason it's not working out. Here's my code: int* test = (int*) malloc(20 * sizeof(int)); printf("Size: %d\n", sizeof(test)); The console outputs 8 when I run this…
Mr Cherno
  • 315
  • 3
  • 10
3
votes
2 answers

Allocating memory for array in struct (in C)

I need to define a type-struct in C that contains an array to be malloc'd as: #include #include typedef struct mine { int N; double *A; } mine; int main(int argc, char** argv) { int i; mine…
3
votes
7 answers

Does "ptr=malloc(sizeof(char)*10)" use many times,one after another allocate pointer to same memory block or cause memory leak?

Suppose I have the following piece of code in my program: char *ptr; ptr=malloc(sizeof(char)*10); ptr=malloc(sizeof(char)*10); ptr=malloc(sizeof(char)*10); ptr=malloc(sizeof(char)*10); Will a pointer to the same memory block be assigned to ptr each…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
3
votes
1 answer

Using pthreads and malloc

I asked a question Using sockets in multithread server yesterday. In this question I described segmentation fault under Solaris in multithreaded server. Now I have found the core of error and written code, that shortly demonstrates it: #include…
Vladimir Berlev
  • 502
  • 4
  • 16
3
votes
4 answers

C free variables declared inside function

Imagine this code: #include #include #include #define MAXSTRSIZE 2048 int main() { char *str; str = get_string(); return 0; } char * get_string() { char temp[MAXSTRSIZE], *str; …
pasadinhas
  • 353
  • 2
  • 6
  • 22
3
votes
1 answer

malloc not allocating properly with pointer to pointer

I am trying to create a function that allocates memory of a certain specified size. In the main, I create the pointer and then send the pointer and the size to the function for memory to be allocated. For some reason it causes all sorts of problems.…
AYR
  • 1,139
  • 3
  • 14
  • 24
3
votes
4 answers

In C programming: Do I need to consider '\0' (null terminator) when mallocing space for a new string

For example, suppose I want to copy thing the string "str1" to a new string, "str2": void function(const char* str1){ char* str2; str2 = (char *) malloc(sizeof(char) * (strlen(str1) + 1)); strcpy(str2, str1); ... } Should the…
sicklybeans
  • 299
  • 3
  • 11
3
votes
2 answers

Protecting allocated memory

I need to allocate dynamically some portions of memory, each with some protection - either RW or RX. I tried to allocate memory by malloc, but mprotect always returns -1 Invalid argument. My sample code: void *x = malloc(getpagesize()); mprotect(x,…
Ari
  • 3,101
  • 2
  • 27
  • 49