I have a very important question (for me)

How do i use <alloc.h>/<malloc.h>/<stdlib.h> for memory allocation?

i'm talking about: malloc(),calloc(),halloc(),realloc(),alloca(),heapwalk(),free()
etc?
i'm having a lot of trouble with these!

thank you!! :confused:

Dani AI

Generated

Quick, practical corrections and best practices. The standard C header for the allocator routines is <stdlib.h> (not the historic <malloc.h>/<alloc.h>), and malloc/calloc/realloc/free behave as documented there. Casting the result of malloc in C is unnecessary and can hide errors; in C++ prefer new/delete instead. Note that malloc takes a single size argument (so the two-argument call in an earlier example is wrong). (man7.org)

Use these safe idioms (check sizes, avoid overflow, handle OOM, and avoid losing the original pointer on realloc):

#include <stdlib.h>
#include <stddef.h>

/* safe realloc pattern */
int *safe_grow(int *p, size_t n) {
    if (n > SIZE_MAX / sizeof *p) return NULL;    /* prevent overflow */
    int *tmp = realloc(p, n * sizeof *p);         /* tmp==NULL -> p still valid */
    if (!tmp) { /* handle out-of-memory */ return NULL; }
    return tmp;
}

When freeing, free(ptr) is correct; optionally set ptr = NULL to avoid accidental use-after-free. For array allocations prefer calloc when you need zeroed memory or use reallocarray (or the overflow check above) to avoid nmemb * size overflow. (man7.org)

About the less-common names you mentioned: alloca() allocates on the stack (automatically freed when the function returns), is machine/compiler-dependent and discouraged for large or portable allocations. halloc() is a DOS-era/huge-pointer allocator (obsolete outside certain legacy toolchains). Heap-walking helpers exist but are platform-specific (Windows has HeapWalk / older CRTs had _heapwalk) and are mainly for debugging. Use those only when you know the target environment. (man7.org)

If you see crashes, memory corruption, leaks or mysterious behavior, run an instrumented build under AddressSanitizer or Valgrind — they find buffer overruns, use-after-free, double-free and leaks quickly and give stack traces. Use these tools early while developing. (clang.llvm.org)

Good start, calloc is useful for zeroing; and good thought, new is the right C++ tool, but for C use the patterns above.

Recommended Answers

All 2 Replies

These are used to allocate memory dynamically.With these you get memory from the OS and point to that memory address with a pointer.

Presonally I use C++ new operator.

Eg.

Some_data_type *object;

object = (Some_data_type*)malloc(sizeof(Some_data_type),1);

that is to assign memory.

Then when you are finished with it:

free(object);

That's the general way to use it.

Thx. I looked it up in a book.

BTW: Instead of using a vector: int a[100] you could use:

int *a;
a=(int*)malloc(100*sizeof(int)); OR
a=(int*)calloc(100,sizeof(int));

It's kind of useful. If you wanna read a vector of an undefined size (n), you have to aproximate and think within which intervals it is. If you use:

int n;
//reads the size
printf("n=");
scanf("%i",&n);
int *a=(int*)malloc(n*sizeof(int));

It's especially useful when you don't what to use up more memory than required! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.