hi,

Can anyone tell me the exact difference between memset,malloc and calloc.I am new to c++, Know this is simple question to ask in c++ but i am getting lot of confusions in using them and i want to knw whr exactly i can use these.

Regards,
Sharath.

Recommended Answers

All 8 Replies

malloc() is C's equivalent of C++'s new operator. It is usually defined in stdlib.h. It allocates memory for your program to use. It takes a size argument, which must be manually scaled depending on the size of whatever type of data you're trying to allocate. For example, to allocate 6 floats, you would have to use float* fptr = (float*)malloc(6 * sizeof(float));.

memset() is a function that sets all the bytes in a chunk of memory to a specific value. It takes three arguments:
1) A pointer to the first element you're going to change.
2) The value you're going to use to fill the chunk of memory.
3) The length of the chunk of memory, in bytes.

calloc() is like malloc(), except it takes two arguments (number of elements, and size of each element), and it automatically initializes all of the memory it allocates to zero.

i want to knw whr exactly i can use these.

You will never use malloc() or calloc() in C++. Ever. Just use new, it's simpler to use and you don't have to include any libraries to use it. As for memset(), you might end up using it, but I honestly can't think of a time where I've used it myself in a real program.

You will never use malloc() or calloc() in C++. Ever.

Wanna bet??? ;-)

I know of no good reason to use malloc() in a c++ program unless you are porting some old legacy program from C to C++. I don't think Tumlee said if can't be used, just that there is no reason to use it. In some cases malloc() can't be used.

Wanna bet??? ;-)

Well, I can't think of a good reason to. Maybe if you wanted to take advantage of realloc() for some sort of insignificant speed boost.

Tks for ur reply guys.....

Wanna bet??? ;-)

I was just listening to Herb Sutter's C++11 presentation for GoingNative 2012 where he argues that there isn't really much reason for the new operator to even appear in modern C++ code (and certainly not delete, which I haven't used in ages), let alone malloc/calloc/realloc. Considering that with the new standard, individual heap-based objects should be created into a smart-pointer directly (with make_shared or other forwarding factory functions, hiding the new call), that move-semantics makes passing-by-value much cheaper (reducing the need for heap-based objects), and that containers are made more flexible and powerful, all this makes for a modern C++ world in which new/delete is seldom needed (at least in higher-level code). But if you include low-level or library code, then of course you will find uses of malloc and other low-level memory functions. How do you think std::allocator is implemented? But this is arguably not really the kind of C++ code that most people would ever write (in other languages like Python, all the library code is in C or C++, so it is not Python. But in C++, the library code just happens to be in C++, but you could argue that this code is not really C++, if you get what I mean..). Today (not in legacy code), you should only find new/delete or malloc/free calls in the guts of some library code (standard or custom-made), but not in user-side code, and thus, if you write a library, you should design it such as to avoid making these memory allocation functions appear in the code of the users of your library.

Today (not in legacy code), you should only find new/delete or malloc/free calls in the guts of some library code (

That is very surprising! I haven't written professionally for about 5 years now, and I am surprised to find that it has changed that much. I have not read much about c++11 yet.

If you happen to want to override the default behaviour of new/delete which you might to monitor allocations or for a number of other reasons but you still want to allocate the memory returned from the heap then you will need malloc/free to implement your new/delete.

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.