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

Can anyone point out why the error is occuring? Error is "expected "=",";","asm" or __attribute__ before "<"

What I am trying to accomplish here is a dictionary with linked list. There is an array of node pointers. I am trying to initialize each array pointer using malloc. When I remove the for loop, it works fine. #include #include…
kevin
  • 173
  • 3
  • 10
2
votes
2 answers

Compiler error when overriding operator new

I don't understand why I am getting compiler errors when trying to compile this: void* MemoryManagedObject::operator new(size_t size, bool UseMemPool) { Engine* engine = Engine::GetEngine(); void* alloc; alloc =…
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
2
votes
4 answers

Detecting memory leaks during d-tor of objects

My application is a dll based implementation and probably leaks memory during unload. I have noticed it during unload/reload cycles (when hosting process is not killed). the hosting process's virtual memory is increasing. i have went through a code…
NirMH
  • 4,769
  • 3
  • 44
  • 69
2
votes
1 answer

How much of a malloced memory page is usable?

#include (size_t) sysconf(_SC_PAGESIZE); sysconf(_SC_PAGESIZE) tells me that my memory page size is 4096 on my operating system and processor. Of the 4096 bytes in the memory page, how many can be used for data and how much is…
HaltingState
  • 1,810
  • 1
  • 20
  • 22
2
votes
1 answer

How to implement interleaved page allocation in a user-mode NUMA-aware memory allocator?

I am building a user-mode NUMA-aware memory allocator for linux. The allocator during its initialization grabs a large chunk of memory, one chunk per NUMA node. After this, memory pages requested by the user are met by giving as many memory pages…
nandu
  • 2,563
  • 2
  • 16
  • 14
2
votes
1 answer

Accessing malloc'd memory in assembly

I'm trying to access memory I have malloced in assembly but I keep just repeatedly getting segfault errors. What am I doing wrong in the following code, I'm sure it's simple but I just can't see it! EDIT: I am using 64 bit NASM assembly ; Allocate…
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
2
votes
5 answers

How to allocate a memory to a given pointer to an array in C++

I've tried to allocate a memory this way: main: X** a; func(a); func: func(X** a){ int size = 5; X* array = (X*)malloc(5*sizeof(X)); //some writing to the array.. a = &array; } If using the debugger I see that when I am in the function…
user550413
  • 4,609
  • 4
  • 25
  • 26
2
votes
6 answers

Getting array size in C. Cannot understand output

I am curious why I am getting the following behaviour in my code. #include #include int main(int argc, char *argv[]) { int M=24; int arr[M]; int N=24; int* ptr=(int*) malloc(sizeof(int)*N); /*Allocate memory…
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189
2
votes
3 answers

is there a substitute malloc and free library on solaris?

I am trying to find memory leaks in a very complicated legacy system that is written in C++/C on the Solaris operating system. the idea is to log every malloc and free and then postproccess the log. i was able to write a stub malloc and free…
yigal
  • 3,923
  • 8
  • 37
  • 59
2
votes
2 answers

Understanding C malloc and sbrk()

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…
Free Lancer
  • 1,000
  • 2
  • 16
  • 33
2
votes
1 answer

copy data which is allocated in device from device to host

I have a pointer which is dynamically allocated in device,then how can I copy it from device to host. #include #define cudaSafeCall(call){ \ cudaError err = call; \ if(cudaSuccess != err){ \ fprintf(stderr, "%s(%i)…
helena
  • 23
  • 3
2
votes
3 answers

use malloc leads to error

I am trying to translate some code from objective c to unmanaged c++ I have this operation Buffer* ir =malloc( sizeof( Buffer ) ); error: expression must have pointer-to type? the same error goes into this code ir->buffer = malloc( bufferSize…
curiousity
  • 4,703
  • 8
  • 39
  • 59
2
votes
3 answers

how to allocate very large fields in c++

I have a problem. I need to allocate few very large fields with billions of float elements. At the moment I'm using: float ****spaceE; int x,y,z; x = y = z = 100; spaceE = (float****)malloc(x*sizeof(int)); for (int i=0; i
adrianko69
  • 21
  • 1
2
votes
4 answers

Is It Necessary To Free Dynamic Pointers Within A Function?

I've read conflicting information on the internet about this. To the best of my knowledge all variables in a function only exist for the life time of the function and so this shouldn't be necessary. But with dynamic pointer arrays I'm not…
2
votes
9 answers

When to use Malloc instead of New

Duplicate of: In what cases do I use malloc vs new? Just re-reading this question: What is the difference between "new" and "malloc" and "calloc" in C++? I checked the answers but nobody answered the question: When would I use malloc instead of…
Martin York
  • 257,169
  • 86
  • 333
  • 562
1 2 3
99
100