Click here to Skip to main content
15,916,846 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDeque using elements from a class Pin
PolarFuzz4-Oct-07 5:10
PolarFuzz4-Oct-07 5:10 
QuestionRe: Deque using elements from a class Pin
David Crow4-Oct-07 5:33
David Crow4-Oct-07 5:33 
AnswerRe: Deque using elements from a class [modified] Pin
George L. Jackson4-Oct-07 5:54
George L. Jackson4-Oct-07 5:54 
GeneralRe: Deque using elements from a class Pin
led mike4-Oct-07 6:20
led mike4-Oct-07 6:20 
GeneralRe: Deque using elements from a class Pin
George L. Jackson4-Oct-07 6:32
George L. Jackson4-Oct-07 6:32 
GeneralRe: Deque using elements from a class Pin
led mike4-Oct-07 7:20
led mike4-Oct-07 7:20 
AnswerRe: Deque using elements from a class Pin
led mike4-Oct-07 6:17
led mike4-Oct-07 6:17 
AnswerRe: Deque using elements from a class Pin
John R. Shaw4-Oct-07 12:24
John R. Shaw4-Oct-07 12:24 
At a guess, I would say your Patient needs to define an “<<” operator that uses “std::stringstream” internally to convert the data to a string that is returned for “cout” to process. I could be wrong as do not remember doing that before, so look around for an example.

What worries me more is what the Patient’s destructor does, as you did not show it. In your code you have two Patients’ and they both contain memory pointers to the same address. You have the original and the copy in the “deque”, when they go out of scope then the destructor will be called for both of them. That means that the same memory will be being freed twice, always a bad idea. Also if the original Patient was created in a different scope and goes out of scope before the “deque” does, the pointers in the copy, contained in the “deque” will be pointing to unallocated memory or memory that is being used my someone else; very, very bad thing.

You need a copy constructor the makes a copy of the original by allocating new memory for the data. This can be done by simply duplicating the code in “Patient(char*,char*,char*)” within the copy constructor.

Additionally you need to make sure that the “char*” passed points to something before calling “_strdup”, as I do not know what will happen if it points to NULL. Also before you assign a new address to a pointer in your methods/functions, you need to make sure that if the pointer is already assigned to a different address that it is freed before making the new assignment.

Of course you could eliminate all that pointer stuff by simply using the “std::string”, instead of handling yourself. It will handle all the memory allocation and copying for you and make the code much cleaner and easier to read.

Properly applied, usage of the STL can eliminate most, if not all, of the memory allocations you need. Take full advantage of “std:string”, “std::vector”, and other containers; after all that are what they are for.

Well that ends today’s lesson. I hope it helped.


INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

GeneralRe: Deque using elements from a class Pin
PolarFuzz5-Oct-07 8:35
PolarFuzz5-Oct-07 8:35 
GeneralRe: Deque using elements from a class Pin
John R. Shaw5-Oct-07 21:57
John R. Shaw5-Oct-07 21:57 
GeneralRe: Deque using elements from a class Pin
PolarFuzz8-Oct-07 2:05
PolarFuzz8-Oct-07 2:05 
Questionwhat is the API that MS Office is calling to save a file under a different name ? Pin
carabutnicolae12344-Oct-07 5:07
carabutnicolae12344-Oct-07 5:07 
QuestionRe: what is the API that MS Office is calling to save a file under a different name ? Pin
David Crow4-Oct-07 5:34
David Crow4-Oct-07 5:34 
AnswerRe: what is the API that MS Office is calling to save a file under a different name ? Pin
carabutnicolae12344-Oct-07 5:52
carabutnicolae12344-Oct-07 5:52 
GeneralRe: what is the API that MS Office is calling to save a file under a different name ? Pin
David Crow4-Oct-07 6:38
David Crow4-Oct-07 6:38 
AnswerRe: what is the API that MS Office is calling to save a file under a different name ? Pin
Mark Salsbery4-Oct-07 5:53
Mark Salsbery4-Oct-07 5:53 
GeneralRe: what is the API that MS Office is calling to save a file under a different name ? Pin
carabutnicolae12344-Oct-07 6:06
carabutnicolae12344-Oct-07 6:06 
GeneralRe: what is the API that MS Office is calling to save a file under a different name ? Pin
Mark Salsbery4-Oct-07 6:13
Mark Salsbery4-Oct-07 6:13 
GeneralRe: what is the API that MS Office is calling to save a file under a different name ? Pin
carabutnicolae12344-Oct-07 6:15
carabutnicolae12344-Oct-07 6:15 
QuestionHow to draw bitmaps without access to HDC or HWND Pin
hvanzyll4-Oct-07 4:39
hvanzyll4-Oct-07 4:39 
QuestionRe: How to draw bitmaps without access to HDC or HWND Pin
Mark Salsbery4-Oct-07 5:56
Mark Salsbery4-Oct-07 5:56 
AnswerRe: How to draw bitmaps without access to HDC or HWND Pin
hvanzyll4-Oct-07 6:05
hvanzyll4-Oct-07 6:05 
GeneralRe: How to draw bitmaps without access to HDC or HWND Pin
Mark Salsbery4-Oct-07 6:19
Mark Salsbery4-Oct-07 6:19 
GeneralRe: How to draw bitmaps without access to HDC or HWND [modified] Pin
hvanzyll4-Oct-07 6:27
hvanzyll4-Oct-07 6:27 
GeneralRe: How to draw bitmaps without access to HDC or HWND Pin
Mark Salsbery4-Oct-07 6:54
Mark Salsbery4-Oct-07 6:54 

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.