Click here to Skip to main content
15,884,715 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralSOLVED Re: Passing function with parameters as parameter Pin
Vaclav_19-Apr-20 9:03
Vaclav_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
Vaclav_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
Vaclav_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
Vaclav_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.
C++
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
Vaclav_18-Apr-20 6:30 
Questioncomputational cost of math functions Pin
Calin Negru17-Apr-20 5:06
Calin Negru17-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
Calin Negru17-Apr-20 6:58
Calin Negru17-Apr-20 6:58 
GeneralRe: computational cost of math functions Pin
leon de boer17-Apr-20 7:04
leon de boer17-Apr-20 7:04 
GeneralRe: computational cost of math functions Pin
Calin Negru17-Apr-20 8:14
Calin Negru17-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
kalberts17-Apr-20 11:47
kalberts17-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
kalberts17-Apr-20 13:03
kalberts17-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
kalberts17-Apr-20 12:37
kalberts17-Apr-20 12:37 
GeneralRe: computational cost of math functions Pin
harold aptroot17-Apr-20 16:12
harold aptroot17-Apr-20 16:12 
AnswerRe: computational cost of math functions Pin
Calin Negru18-Apr-20 8:56
Calin Negru18-Apr-20 8:56 
Questionmfc LoadBitmap hangs my application... Pin
charlieg15-Apr-20 12:21
charlieg15-Apr-20 12:21 
AnswerRe: mfc LoadBitmap hangs my application... Pin
_Flaviu16-Apr-20 1:37
_Flaviu16-Apr-20 1:37 
GeneralRe: mfc LoadBitmap hangs my application... Pin
charlieg16-Apr-20 4:30
charlieg16-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.