1. What happen if delete a CWnd without calling CWnd::DestroyWindow()
After performing the following steps, what happen to my application or the
system:
1. My application has a class, namely CMyWnd, which is inherited from CWnd.
2. Instantiate a CMyWnd object by: CMyWnd* pMyWnd = new CMyWnd(...).
3. Call pMyWnd->CreateEx(...) to create the window.
4. (**DO NOT perform this step** Call pMyWnd->DestroyWindow() to destroy the
window.)
5. Release the CMyWnd object by: delete pMyWnd;
Please be note the destructor of CMyWnd does not called DestroyWindow() .
Here is declaration of the CMyWnd
class CMyWnd : public CWnd
{
CMyWnd()
{
}
~CMyWnd()
{
// do nothing
}
}
2. What happens if I use delete [] to a pointer from a "new char('\0')"
3. Mixing new/delete and operator new/delete?
Is it allowed by the C++ standard to mix the use of 'new/delete' and
'operator new/delete'?
(a) alloc (version 1) + free (version 2)
(b) alloc (version 2) + free (version 1)
// VERSION 1 ALLOC (using operator new + placement new)
type* data = (type*)operator new(n * sizeof(type));
for (type* p = data; p != data + n; ++p)
new ((void *)p) value_type();
// VERSION 1 FREE (using calling destructor + operator delete)
for (type* p = data; p != data + n; ++p)
p->~type();
operator delete(data);
// VERSION 2 ALLOC (using new)
type* data = new type[n];
// VERSION 2 FREE (using delete)
delete [] data;
4. Delete Objects Even Without "New"?
The following code for a singleton implementation is recommended in a
website:
class GlobalClass
{
int m_value;
static GlobalClass *s_instance;
GlobalClass(int v = 0)
{
m_value = v;
}
public:
int get_value()
{
return m_value;
}
void set_value(int v)
{
m_value = v;
}
static GlobalClass *instance()
{
if (!s_instance)
s_instance = new GlobalClass;
return s_instance;
}
};
// Allocating and initializing GlobalClass's
// static data member. The pointer is being
// allocated - not the object inself.
GlobalClass *GlobalClass::s_instance = 0;
void foo(void)
{
GlobalClass::instance()->set_value(1);
cout << "foo: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';
}
void bar(void)
{
GlobalClass::instance()->set_value(2);
cout << "bar: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';
}
int main(void)
{
cout << "main: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';
foo();
bar();
}
Is the author at fault for the lack of delete statements? Are there
memory-leak issues?
Thanks a lot,
Paul Epstein
6. When this statement without &, what will happen?
7. after create a new file use filemapping ,i could not delete the new file
hi,all
in my c programme ,i created a new file used the following
function(a,b,c,d,e,f,g),
after writed bytes to the file,i use CloseHandle to close the file and
MapFile,
CloseHandle return success,but i could not delete the new file until exit
the programme.
Anyone knows it?
thanks
Yongge
a)hFile = CreateFile(lpFileName,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
b)DWORD dBytesWritten=1024; // number of bytes written
char szCharBuff[1024];
memset((void *)szCharBuff,0,1024);
WriteFile (hFile, (void *)szCharBuff, 1024,&dBytesWritten, NULL);
c)hMapFile = CreateFileMapping(hFile, // current file handle
NULL, // default security
PAGE_READWRITE, // read/write permission
0, // size of mapping object, high
0, // size of mapping object, low
NULL); // name of mapping object
d)lpMapAddress = MapViewOfFile(hMapFile, // handle to mapping object
FILE_MAP_ALL_ACCESS, // read/write permission
0, // high-order 32 bits of file offset
dwFileMapStart, // low-order 32 bits of file offset
dwMapViewSize); // number of bytes to map
e)UnmapViewOfFile();
f)b=CloseHandle(hMapFile); // close the file mapping
g)b=CloseHandle(hFile); // close the file
8. delete() and new() on elements of an array created by new[]()