memset() doesn't allocate memory. It's used to set memory to byte values (usually zero). For example, this is a very common way of calling it: memset(a,0,sizeof(a));
This sets all bytes of a to zero, regardless of the type of a. I can't stress enough that it sets bytes, not elements.
Actually what you have tried to implement doesn't make sense.
To use memset you need to allocate some memory using new or malloc.
memset is used to copy characters to some buffer(int array, char array, byte array etc.) not to assign 0 to class object( u have used class object here)
After allocating memory for memSetGreeter using new if you write the code below
memset(memSetGreeter,0,sizeof(someClass));
you will loose the pointer you got by "new"ing
i hope following example may help you to understand memset
@filipe: memset() is only initializing the array. The allocation occurred on the stack when you declared it, not when you used memory. memset() works only on memory you already have; it doesn't get you memory.
i said "To use memset you need to allocate some memory using new or malloc". i meant in this particular case. since Karthick22 was discussing about new, malloc and memset. He has declared a poinetr object. i just continued from there.
@all..
u guys have made this thread a superb reference..
@ Reannah
u got everything u said but not "you loose pointer you got after newing".. is it because new adds some kind of header info to maintain the state of the object??
Or in other words.. how ll the object look like in memorY??
#include<iostream>
class someClass
{
char* greeting;
public:
someClass()
{
greeting="hello.. Welcome to C++ World";
}
void greet()
{
std::cout<<greeting;
}
};
int main(int argc,char* argv[])
{
someClass class1;
class1.greet();
someClass* memSetGreeter=NULL;
memset(memSetGreeter,0,sizeof(someClass));
memSetGreeter->greet();
}
Even if you allocate the memory, memset still doesn't make sense in this example. You'd simply be resetting the pointer to NULL and greet() would print nothing. That wouldn't be a valid use of memset and you also shouldn't use memset on non-POD types. Use the constructors and interface functions of the class instead.
To fill an array of integers with zero use std::fill.
void memsetsample()
{
char* pch = newchar[10];
memset( pch, 'A', 10 );
int* pn = newint[10];
// memset works but not as intuitive as std::fill and much more error prone. Also it only works with
// zero.
// memset( pn, 0, sizeof(int)*10 );
// see how much easier that is? Don't need to calculate the size in bytes. Just specify the number of elements.
std::fill(pn, pn + 10, 0);
for( int nCount = 0; nCount < 10; nCount++ )
{
cout<<pch[nCount]<<" "<<pn[nCount]<<"\n";
}
// use std::generate if you have a predicate function to pick values. In this case, I use rand from
// the C library
std::generate(pn, pn + 10, rand);
for( int nCount = 0; nCount < 10; nCount++ )
{
cout<<pch[nCount]<<" "<<pn[nCount]<<"\n";
}
}
But that doesn't overwrite the pointer. memset(&memSetGreeter,0,sizeof(memSetGreeter)); does. What he's doing in that example is merely an illegal write.
greet() would print nothing
No, the program would crash when trying to do ((const char *)0)[0].