Use of cudamalloc(). Why the double pointer?

All CUDA API functions return an error code (or cudaSuccess if no error occured). All other parameters are passed by reference. However, in plain C you cannot have references, that’s why you have to pass an address of the variable that you want the return information to be stored. Since you are returning a pointer, … Read more

malloc: *** error: incorrect checksum for freed object – object was probably modified after being freed

I have a big problem with my iOS App: it crashes sometimes without detailed debug error. The stack trace is empty. These are the only two lines in the stack trace: crash start in UIApplicationMain at “symbol stub for: -[_UIHostedTextServiceSession dismissTextServiceAnimated:]”. and report “libsystem_c.dylib`malloc_error_break”. in com.apple.main-thread. The error on Xcode debugger (with connected device): I … Read more

Memory Clobbering Error

You are incrementing ptr, therefore changing the address that it points to. You can’t do that. In your case, have a separate pointer, let’s say char * p = ptr and do your operations with p leaving ptr intact so you can free(ptr) later. EDIT Taking a second look at your code, I found that … Read more

difference between and

The <malloc.h> header is deprecated (and quite Linux specific, on which it defines non-standard functions like mallinfo(3)). Use <stdlib.h> instead if you simply need malloc(3) and related standard functions (e.g. free, calloc, realloc ….). Notice that <stdlib.h> is defined by C89 (and later) standards, but not <malloc.h> Look into /usr/include/malloc.h you’ll find there some non-standard functions (e.g. malloc_stats(3), etc…) – in addition of malloc…. And gcc don’t link header files, but libraries. Read Levine’s book about linkers & loaders for more. … Read more

How do malloc() and free() work?

OK some answers about malloc were already posted. The more interesting part is how free works (and in this direction, malloc too can be understood better). In many malloc/free implementations, free does normally not return the memory to the operating system (or at least only in rare cases). The reason is that you will get gaps in … Read more

How to properly malloc for array of struct in C

Allocating works the same for all types. If you need to allocate an array of line structs, you do that with: In your code, you were allocating an array that had the appropriate size for line pointers, not for line structs. Also note that there is no reason to cast the return value of malloc(). Note that’s it’s better style to use: … Read more

When and why to use malloc?

malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example when you don’t know the amount of memory during compile time. One example should clear this. Say you know there will be maximum 20 students. So you can create an array with … Read more