Helpful Information
 
 
Category: C Programming
memset vs just using alloc

I am just wondering opinions on which do you think is better, just using alloc or using the mem* functions?

It depends on what you're doing. If you want to allocate memory for usage, use malloc(). If you want to fill a previously-allocated buffer with a certain value, then use memset().

Perhaps I don't understand exactly what you're asking... :cool:

I know the uses. What I was asking was an opinion. You don't have to use the mem* functions, was just wanting to know how many people actually use it and what they thought about it.

I don't find many places that I use mem* functions, but they do come in handy every now and again. Was wonding what other peoples opinions was of this.

Well I'm using memset.....I guess that s what i've been schooled with, cant really give u a good explanation, but hey...at least its an oppinion ;)

Oh... well in that case... ;)

If I need to zero out a struct or a class object, or some other chunk of memory, I just memset() 0 into that object. Likewise, when working with network data, in order to handle both binary and ascii data correctly, I use memcpy() to copy the data into and out of my send/recv buffers.

I'm not really a big fan of malloc() and its relatives, though they do have their uses. I tend to do mostly C++ programming, so I use new and delete.

Why use malloc when you can use new and delete? I don't do any C programming anymore, only C++.

Sometimes I don't have a choice and have to use C. Then malloc() is indispensable.

New and Delete do serve their purpose for C++, but I find that most of my programs do not benefit from OOP but are easily managed in a more modular format, so I mainly stick to C instead. If I need to do something that would benefit from OOP then I have no problem using C++, but most of the time the programs I write are so isolated from each other that there really isn't much to link to each other.

I though I knew C programming untill I read this thread. One question ...


With the mem* functions (let's suppose memcpy) do the library allocates the space for the dest pointer ? I never thought it could.

I've always used malloc and memcpy, but now I'm lost.

NOTE: Maybe I'm misunderstanding this post and asking about something nobody said.

Originally posted by Thrasher
With the mem* functions (let's suppose memcpy) do the library allocates the space for the dest pointer ? I never thought it could.

memcpy() just copies memory, it doesn't allocate it. You must create space to store the copied data, whether you declare a variable or static buffer (ex: char buff[1024]) or whether you create space dynamically with malloc().

yeah, the mem* functions do not create memory, only manipulate it.

Originally posted by Taradino
Why use malloc when you can use new and delete? I don't do any C programming anymore, only C++.

If you use malloc,etc. to create object instances in C++, creating/destroying the object doesn't call the constructor/destructor functions of the instance. With new and delete, it does.

OK, that's what I though. So, what is this thread about ? Shouldn't it has been called "Do you initialize your structs to zero?" then ?

The rest of the discussion is meaningless. New and delete are only available for C++, so it's the same as to debate about using the dot in php and using C's strcat. If you are programming in C (which produces better code) you'll use malloc, and if you do it in C++ you'll use new (exept in some situations in which malloc is a better solution).

Another question is if people is programming in C++ in its pure state, or they do it C-style. Note: the best example is file managing. Do you use the streams or use fopen ?

* Bump *

I am just wondering opinions on which do you think is better, just using alloc or using the mem* functions?
After reading through the posts I'm not sure anyone really understood your question (I think perhaps you meant calloc?)

calloc() is the same as malloc() except it will also zero out the memory it allocates. So

void* a = calloc(10);is the same as
void* a = malloc(10);
memset(a, 0, 10);
To answer your question...
I personally like to use memset(), but I suspect calloc() was implemented for a reason and is most likely a faster alternative.










privacy (GDPR)