Click here to Skip to main content
15,887,453 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Event objects VS Global variables Pin
David Crow18-Jun-15 9:15
David Crow18-Jun-15 9:15 
AnswerRe: Event objects VS Global variables Pin
Richard Andrew x6418-Jun-15 9:48
professionalRichard Andrew x6418-Jun-15 9:48 
AnswerRe: Event objects VS Global variables Pin
Stephen Hewitt18-Jun-15 15:42
Stephen Hewitt18-Jun-15 15:42 
AnswerRe: Event objects VS Global variables Pin
Frankie-C19-Jun-15 6:09
Frankie-C19-Jun-15 6:09 
Question[C] possible OOP approach Pin
lukeer17-Jun-15 21:17
lukeer17-Jun-15 21:17 
AnswerRe: [C] possible OOP approach Pin
CPallini17-Jun-15 21:25
mveCPallini17-Jun-15 21:25 
AnswerRe: [C] possible OOP approach Pin
Stefan_Lang18-Jun-15 20:36
Stefan_Lang18-Jun-15 20:36 
AnswerRe: [C] possible OOP approach Pin
cth02721-Jun-15 12:08
cth02721-Jun-15 12:08 
Hi Lukeer !

Returning a struct is indeed valid C. It's even given as example in section 6.8.6.4 of the C11 standard, which deals with the return statement.

The return is done with a dumb bit by bit copy. This works very well for struct composed of fundamental types. But as soon as you have pointers in the struct, you need to be extremely careful about who owns an object and shall invoke (manually) the destructor: there is a high risk of nasty memory deallocation errors here !

The more efficient approach would be to return pointers as you suggested. But then, you'll have a high risk of memory leakage, in case the caller is not interested in the returned value (in other functions than the constructor).

The safer way would be to design the API to encourage the caller to provide a pointer to the struture to be used, that he is responsible of (whether it's mallocated or local):
class_animal a,*pa; 
newClassAnimal(&a);   // reuse the existing structure
pa = newClassAnimal (NULL); // or allocate a new one. 
// here the caller is aware that he has to manage the object

I used very long time ago these kind of structures. The function pointers prove to be very efficient way to implement polymorphism in a non object language like C. However the management of the object has its drawback: no automatic destruction, and hence high risk of leakage for more complex execution flows, and especially if you use setjmp/longjmp.
QuestionSuggestions on real-time video display with drawing Pin
Kiran Satish17-Jun-15 6:08
Kiran Satish17-Jun-15 6:08 
AnswerRe: Suggestions on real-time video display with drawing Pin
CPallini17-Jun-15 21:06
mveCPallini17-Jun-15 21:06 
GeneralRe: Suggestions on real-time video display with drawing Pin
Kiran Satish23-Jun-15 9:39
Kiran Satish23-Jun-15 9:39 
GeneralRe: Suggestions on real-time video display with drawing Pin
DEmberton26-Jun-15 3:44
DEmberton26-Jun-15 3:44 
QuestionCString.Format anomaly Pin
ForNow17-Jun-15 3:02
ForNow17-Jun-15 3:02 
AnswerRe: CString.Format anomaly Pin
Richard MacCutchan17-Jun-15 3:13
mveRichard MacCutchan17-Jun-15 3:13 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 7:48
ForNow17-Jun-15 7:48 
GeneralRe: CString.Format anomaly Pin
Richard MacCutchan17-Jun-15 8:07
mveRichard MacCutchan17-Jun-15 8:07 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:26
ForNow17-Jun-15 8:26 
AnswerRe: CString.Format anomaly Pin
Jochen Arndt17-Jun-15 3:27
professionalJochen Arndt17-Jun-15 3:27 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 7:46
ForNow17-Jun-15 7:46 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 8:00
jeron117-Jun-15 8:00 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:11
ForNow17-Jun-15 8:11 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 8:19
jeron117-Jun-15 8:19 
QuestionMultithread c Windows Pin
mosine16-Jun-15 2:22
mosine16-Jun-15 2:22 
AnswerRe: Multithread c Windows Pin
Jochen Arndt16-Jun-15 2:33
professionalJochen Arndt16-Jun-15 2:33 
GeneralRe: Multithread c Windows Pin
mosine16-Jun-15 2:47
mosine16-Jun-15 2:47 

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.