Click here to Skip to main content
15,921,793 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalextracting the value from a database(ADO) Pin
nss4-Aug-02 14:10
nss4-Aug-02 14:10 
GeneralRe: extracting the value from a database(ADO) Pin
Christian Graus4-Aug-02 14:20
protectorChristian Graus4-Aug-02 14:20 
GeneralRe: extracting the value from a database(ADO) Pin
RChin5-Aug-02 4:39
RChin5-Aug-02 4:39 
Generalcopying one array into another -simple question Pin
nss4-Aug-02 13:38
nss4-Aug-02 13:38 
GeneralRe: copying one array into another -simple question Pin
Christian Graus4-Aug-02 14:22
protectorChristian Graus4-Aug-02 14:22 
GeneralRe: copying one array into another -simple question Pin
nss4-Aug-02 15:05
nss4-Aug-02 15:05 
GeneralRe: copying one array into another -simple question Pin
Ravi Bhavnani4-Aug-02 14:53
professionalRavi Bhavnani4-Aug-02 14:53 
GeneralRe: copying one array into another -simple question Pin
Daniel Lohmann5-Aug-02 3:56
Daniel Lohmann5-Aug-02 3:56 
Christian already suggested STL vectors Smile | :)

One thing left: What's the difference between delete[] and delete.

C++ developer, even experienced ones, often tell that in C++ pointers and arrays are the same. However, this is wrong! C/C++ does syntactically not distinguish between pointers and arrays. Semantically pointers and arrays are quite different things! And this is the reason for two versions of operator delete:

CString a[] = new CString[ 10 ];
CString b[] = new CString[ 10 ];

delete a;    // destroys only a[0] and then frees memory block
delete[] b;  // destroys a[0] - a[9] and then frees memory block


Because arrays and pointers share the same syntax, both a and b in the above code could be interpreted as pointer or as array. However, as I wrote above, they are not sematically the same thing:

- The first delete statement (delete a) interprets <coda>a as a pointer. It therfore destroys the object (calls the destructor) to which the pointer refers - this is a[0]. Then the buffer itself is freed. The destructors of a[1] to a[9] are not called. Because CString objects manage their own string buffer and free in the destructor, these buffers are never freed for a[1] to a[9] and cause memory leaks.

- The second delete statement (delete[] b) interprets b as an array. It first calls the destructor for every single array element and then frees the buffer.

Technically the one and only difference is that if you use operatore delete[] the destructor of each array element is called, while operator delete calls only the destructor of the first element. Therfore for object types that do not have an user defined destructor, both are semantically equivalent:

TCHAR szBuffer = new [MAX_PATH];
...
delete szBuffer; // No problem here, TCHAR has no destructor to call



--

Daniel Lohmann

http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there Big Grin | :-D )
GeneralOO design question Pin
David Wulff4-Aug-02 12:55
David Wulff4-Aug-02 12:55 
GeneralRe: OO design question Pin
Ravi Bhavnani4-Aug-02 15:00
professionalRavi Bhavnani4-Aug-02 15:00 
GeneralRe: OO design question Pin
David Wulff4-Aug-02 15:32
David Wulff4-Aug-02 15:32 
GeneralRe: OO design question Pin
Shog94-Aug-02 19:45
sitebuilderShog94-Aug-02 19:45 
GeneralRe: OO design question Pin
David Wulff5-Aug-02 1:33
David Wulff5-Aug-02 1:33 
GeneralRe: OO design question Pin
Shog95-Aug-02 5:14
sitebuilderShog95-Aug-02 5:14 
GeneralRe: OO design question Pin
David Wulff5-Aug-02 6:46
David Wulff5-Aug-02 6:46 
GeneralRe: OO design question Pin
Stan Shannon5-Aug-02 8:47
Stan Shannon5-Aug-02 8:47 
GeneralRe: OO design question Pin
Stan Shannon5-Aug-02 2:04
Stan Shannon5-Aug-02 2:04 
GeneralRe: OO design question Pin
David Wulff5-Aug-02 2:29
David Wulff5-Aug-02 2:29 
GeneralRe: OO design question Pin
Stan Shannon5-Aug-02 3:43
Stan Shannon5-Aug-02 3:43 
GeneralRe: OO design question Pin
Ravi Bhavnani5-Aug-02 3:37
professionalRavi Bhavnani5-Aug-02 3:37 
GeneralRe: OO design question Pin
Tomasz Sowinski4-Aug-02 23:51
Tomasz Sowinski4-Aug-02 23:51 
GeneralRe: OO design question Pin
David Wulff5-Aug-02 1:22
David Wulff5-Aug-02 1:22 
Generalusing CBitmapButton Pin
Mavrock4-Aug-02 11:58
Mavrock4-Aug-02 11:58 
GeneralRe: using CBitmapButton Pin
Ravi Bhavnani4-Aug-02 15:00
professionalRavi Bhavnani4-Aug-02 15:00 
GeneralGetting selected text globally Pin
Christian Skovdal Andersen4-Aug-02 11:19
Christian Skovdal Andersen4-Aug-02 11:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.