Click here to Skip to main content
14,638,345 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
GeneralSOLVED Re: Passing function with parameters as parameter Pin
Vaclav_19-Apr-20 9:03
MemberVaclav_19-Apr-20 9:03 
AnswerRe: Passing function with parameters as parameter Pin
Richard MacCutchan18-Apr-20 21:21
mveRichard MacCutchan18-Apr-20 21:21 
QuestionMath problem with C code Pin
Vaclav_18-Apr-20 6:47
MemberVaclav_18-Apr-20 6:47 
AnswerRe: Math problem with C code Pin
k505418-Apr-20 7:00
mvek505418-Apr-20 7:00 
GeneralSOLVED Re: Math problem with C code Pin
Vaclav_18-Apr-20 8:16
MemberVaclav_18-Apr-20 8:16 
QuestionRe: Math problem with C code Pin
Richard MacCutchan18-Apr-20 7:02
mveRichard MacCutchan18-Apr-20 7:02 
QuestionPass a struct as a function parameter or just use it as a global variable? Pin
Vaclav_18-Apr-20 5:23
MemberVaclav_18-Apr-20 5:23 
AnswerRe: Pass a struct as a function parameter or just use it as a global variable? Pin
k505418-Apr-20 6:17
mvek505418-Apr-20 6:17 
Unless you have a good, compelling argument otherwise (and there are a few), you should prefer to pass structs as parameters. If its small (e.g. something like a struct timeval), and you don't need to modify the members, its OK to pass by value. If its large, then pass by reference (pointer). If you're not going to modify the struct in the function then you should mark the reference as const.
struct small_obj {
   char description[16];
   int count;
};  // size = 20 bytes

struct large_obj {
    char text[1024][1024];
    double factor[1024][1024];
}; // size = 9 MB

void f1(small_obj arg);  // OK, puts 20 bytes on the stack, f1 gets a copy
                                // of the struct. Any modifications to the struct are
                                // local ot f1

void f2(small_obj* arg);  // stack is 4 bytes only. changes to f2 members will not
                                 // be seen by caller

void f3(const small_obj *arg); // stack is 4 bytes only. attempts within f3 to modify members
                               // should produce compiler warnings

void f4(small_obj& arg);   // similar to f2(), but uses C++ reference type

void f5(const small_obj& arg);   // similar to f3, but uses C++ reference type

void f6(large_obj arg);   // questionable.  This copys 9 MB onto the stack, which might
                          // be more than the OS can handle, and might incur performance
                          // penalties you can avoid by passing as a pointer or reference

The problem with global values, especially global values that are not local to a single file (e.g. declared as static), is it becomes increasingly more difficult to reason about where the variable gets modified. Given a function say print_item(void) you might not expect your global Item to get modified, but print_item() calls foo(void), which calls bar(void) which calls frobnicate(void), which modifies Item. If you are trying to trace down a bug, trying to see where Item gets modified can be difficult. On the other hand, calling print_item(const Item&), you can be (more or less) sure that Item doesn't get modified during the lifetime of print_item() (more or less because print_item() could be nasty and cast away constness, but that's an evil for another tale). Alternatively, you might have print_item(Item foo), which gets a local copy of Item, so the caller knows that whatever changes print_item might make to its copy, the callers Item remains unchanged.
Keep Calm and Carry On

GeneralRe: Pass a struct as a function parameter or just use it as a global variable? Pin
Vaclav_18-Apr-20 6:30
MemberVaclav_18-Apr-20 6:30 
Questioncomputational cost of math functions Pin
CalinNegru(fearless_)17-Apr-20 5:06
MemberCalinNegru(fearless_)17-Apr-20 5:06 
AnswerRe: computational cost of math functions Pin
OriginalGriff17-Apr-20 5:10
mveOriginalGriff17-Apr-20 5:10 
GeneralRe: computational cost of math functions Pin
CalinNegru(fearless_)17-Apr-20 6:58
MemberCalinNegru(fearless_)17-Apr-20 6:58 
GeneralRe: computational cost of math functions Pin
leon de boer17-Apr-20 7:04
Memberleon de boer17-Apr-20 7:04 
GeneralRe: computational cost of math functions Pin
CalinNegru(fearless_)17-Apr-20 8:14
MemberCalinNegru(fearless_)17-Apr-20 8:14 
GeneralRe: computational cost of math functions Pin
Joe Woodbury17-Apr-20 10:43
professionalJoe Woodbury17-Apr-20 10:43 
GeneralRe: computational cost of math functions Pin
Member 798912217-Apr-20 11:47
MemberMember 798912217-Apr-20 11:47 
GeneralRe: computational cost of math functions Pin
Joe Woodbury17-Apr-20 12:53
professionalJoe Woodbury17-Apr-20 12:53 
GeneralRe: computational cost of math functions Pin
Member 798912217-Apr-20 13:03
MemberMember 798912217-Apr-20 13:03 
AnswerRe: computational cost of math functions Pin
CPallini17-Apr-20 7:04
mveCPallini17-Apr-20 7:04 
AnswerRe: computational cost of math functions Pin
Member 798912217-Apr-20 12:37
MemberMember 798912217-Apr-20 12:37 
GeneralRe: computational cost of math functions Pin
harold aptroot17-Apr-20 16:12
Memberharold aptroot17-Apr-20 16:12 
AnswerRe: computational cost of math functions Pin
CalinNegru(fearless_)18-Apr-20 8:56
MemberCalinNegru(fearless_)18-Apr-20 8:56 
Questionmfc LoadBitmap hangs my application... Pin
charlieg15-Apr-20 12:21
Membercharlieg15-Apr-20 12:21 
AnswerRe: mfc LoadBitmap hangs my application... Pin
_Flaviu16-Apr-20 1:37
Member_Flaviu16-Apr-20 1:37 
GeneralRe: mfc LoadBitmap hangs my application... Pin
charlieg16-Apr-20 4:30
Membercharlieg16-Apr-20 4:30 

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.