Can someone please explain to me how dynamic memory allocation actually works? For example whast going on in the piece of code written below? If you can respond asap, that would be great. Thanks in advance.

p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?

Dani AI

Generated

Brief practical summary tied to the thread: is right — memory you get from malloc/calloc comes from the heap (not the stack or globals). correctly noted that calloc clears returned memory. is also right that in modern C++ you should prefer RAII — avoid raw malloc unless you have a specific reason.

Key points and cautions

  • malloc hands you uninitialized bytes; calloc hands you zeroed bytes (byte-wise zero). That byte-zeroing often yields 0 for integers, 0.0 for IEEE-754 floats, and a null pointer on most platforms — but the C standard does not guarantee those mappings for every possible implementation. If portability matters, explicitly initialize values.
  • Always check for allocation failure: in C malloc/calloc return NULL; in C++ plain new throws std::bad_alloc (use nothrow if you need NULL behavior).
  • Beware integer overflow when computing sizes. For example, check that n is not so large that n * sizeof(...) wraps.

Safer practices

  • Do not mix allocators: memory obtained with malloc/calloc/realloc must be released with free. Memory from new / new[] must be freed with delete / delete[]. Mixing is undefined behavior.
  • Prefer container/RAII solutions in C++: std::vector<float> or std::unique_ptr<float[]> manage lifetime and avoid leaks. If you need zeroed storage, explicitly fill it rather than relying on platform representations.

Small examples

/* size overflow check */
if (n > SIZE_MAX / sizeof(float)) {
    /* handle too-large allocation */
}
/* RAII: manage a dynamic array and zero it */
std::vector<float> v;
v.resize(n);
std::fill(v.begin(), v.end(), 0.0f);

Debugging: use tools like Valgrind or AddressSanitizer to find leaks and invalid accesses.

Recommended Answers

All 4 Replies

>>And whats the difference in malloc and calloc?
calloc() calls malloc() then initializes the data to all 0s.

The code you posted is allocating an array of n floats

whats the difference in malloc and calloc/?

Can someone please explain to me how dynamic memory allocation actually works? For example whast going on in the piece of code written below? If you can respond asap, that would be great. Thanks in advance.

p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?

>>And whats the difference in malloc and calloc?
calloc() calls malloc() then initializes the data to all 0s.

Please read answers given to you. And don't quote yourself...

A computer program is loaded into several parts of memory. There is the code (functions and the like), data (global variables and the like), the stack (local variables and the like), and the heap.

The heap is just a big block of unused memory that your program is allowed to use. When you call malloc() it takes a chunk of the heap and marks it as 'allocated', and returns a pointer to the part of the heap memory you can use. When you call free() that piece of heap memory is unmarked and available for malloc() to use again.

So for your example, you ask for n *sizeof( float ) bytes of the heap. The pointer p points to the first part of the heap you are allowed to use for your array of floats.

http://c-faq.com/malloc/calloc.html
Note in particular that using calloc to initialise your floats to 0.0 is NOT portable.

Also, since this is C++, you should really be using p = new float[n]; If this were a C program, I would be telling you NOT to cast the result of malloc.

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.