Click here to Skip to main content
14,356,021 members

C / C++ / MFC

 
QuestionRe: Event objects VS Global variables Pin
David Crow18-Jun-15 10:15
memberDavid Crow18-Jun-15 10:15 
AnswerRe: Event objects VS Global variables Pin
Richard Andrew x6418-Jun-15 10:48
professionalRichard Andrew x6418-Jun-15 10:48 
AnswerRe: Event objects VS Global variables Pin
Stephen Hewitt18-Jun-15 16:42
memberStephen Hewitt18-Jun-15 16:42 
AnswerRe: Event objects VS Global variables Pin
Frankie-C19-Jun-15 7:09
memberFrankie-C19-Jun-15 7:09 
Question[C] possible OOP approach Pin
lukeer17-Jun-15 22:17
memberlukeer17-Jun-15 22:17 
AnswerRe: [C] possible OOP approach Pin
CPallini17-Jun-15 22:25
mveCPallini17-Jun-15 22:25 
AnswerRe: [C] possible OOP approach Pin
Stefan_Lang18-Jun-15 21:36
memberStefan_Lang18-Jun-15 21:36 
AnswerRe: [C] possible OOP approach Pin
cth02721-Jun-15 13:08
membercth02721-Jun-15 13: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 7:08
memberKiran Satish17-Jun-15 7:08 
AnswerRe: Suggestions on real-time video display with drawing Pin
CPallini17-Jun-15 22:06
mveCPallini17-Jun-15 22:06 
GeneralRe: Suggestions on real-time video display with drawing Pin
Kiran Satish23-Jun-15 10:39
memberKiran Satish23-Jun-15 10:39 
GeneralRe: Suggestions on real-time video display with drawing Pin
DEmberton26-Jun-15 4:44
memberDEmberton26-Jun-15 4:44 
QuestionCString.Format anomaly Pin
ForNow17-Jun-15 4:02
memberForNow17-Jun-15 4:02 
AnswerRe: CString.Format anomaly Pin
Richard MacCutchan17-Jun-15 4:13
protectorRichard MacCutchan17-Jun-15 4:13 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:48
memberForNow17-Jun-15 8:48 
GeneralRe: CString.Format anomaly Pin
Richard MacCutchan17-Jun-15 9:07
protectorRichard MacCutchan17-Jun-15 9:07 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 9:26
memberForNow17-Jun-15 9:26 
AnswerRe: CString.Format anomaly Pin
Jochen Arndt17-Jun-15 4:27
mveJochen Arndt17-Jun-15 4:27 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 8:46
memberForNow17-Jun-15 8:46 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 9:00
memberjeron117-Jun-15 9:00 
GeneralRe: CString.Format anomaly Pin
ForNow17-Jun-15 9:11
memberForNow17-Jun-15 9:11 
GeneralRe: CString.Format anomaly Pin
jeron117-Jun-15 9:19
memberjeron117-Jun-15 9:19 
QuestionMultithread c Windows Pin
mosine16-Jun-15 3:22
membermosine16-Jun-15 3:22 
AnswerRe: Multithread c Windows Pin
Jochen Arndt16-Jun-15 3:33
mveJochen Arndt16-Jun-15 3:33 
GeneralRe: Multithread c Windows Pin
mosine16-Jun-15 3:47
membermosine16-Jun-15 3: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.