Click to See Complete Forum and Search --> : Overhead
Erik Wiggins
December 23rd, 2004, 03:31 PM
Just a few questions.
1. C++ vs C
Does it add overhead to create a class? Would the program be smaller if functions and struc's where used instead?
2. #include
Does including one of the STL's include the entire file in your program? If so wouldn't it make your program smaller if you just rewrote the functions you need to your program and not added all the extra.
3. Dialogs
Does a dialog take up more room then creating the window yourself using API calls?
If any one could answer these questions for me it would be greate. I'm from the school of thaught that you should always make your programs as small as possible. Thanks again for all the help guys.
Kheun
December 23rd, 2004, 08:48 PM
I can only answer 1 and 2.
1. No, in normal case, the size of a class is the same as a struct that has exactly the same members. The size of the class will only be larger when it has a virtual function or the base class is being virtual inherited. This is because for virtual function to work, the compiler needs to regenerate some form of virtual function table for your class. Even though the size becomeslarger, it actually save us the trouble from repeatedly code switch-case statement, as well as saving byte size, to do the same task that what a virtual function do.
2. Even through the entire header file is being included, it doesn't really cause your program to be bloated. This is because C++ compilers are designed to only incur the cost for what you only used. Therefore, only the class or function that you used will be compiled.
Paul McKenzie
December 23rd, 2004, 09:34 PM
Just a few questions.
3. Dialogs
Does a dialog take up more room then creating the window yourself using API calls?
Dialogs are windows, so I don't understand your question.
Regards,
Paul McKenzie
Paul McKenzie
December 23rd, 2004, 09:52 PM
Just a few questions.
1. C++ vs C
Does it add overhead to create a class? Would the program be smaller if functions and struc's where used instead?There is no difference between a struct and a class in C++ except that a
1) struct has by default public members, and a class by default has private members.
2) By default you derive from a struct publicly, and by default you derive from a class privately.
Other than that a struct is a class in C++.
Also, if I want to write a linked list class, or write functions, what difference is there between the two? Hardly any in terms of code size, and a whole lot of difference in terms of maintenance and ease of use.
Does including one of the STL's include the entire file in your program?No. The linker takes care of those details. To show you the proof of this:
struct X
{
static int a;
static int b;
static int c;
void DoSomething() { }
};
int main()
{
X x;
x.DoSomething();
}
(I used structs to illustrate my first point that a struct is a class).
I defined 3 static int's in the struct. Note that in the main() program, I don't use any of those ints. The linker doesn't complain that I haven't defined those static members somewhere because I never used them. So the linker optimizes them away, and they never show up in the executable. If I now change the main() to this:
int main()
{
X x;
X::a = 1;
x.DoSomething();
}
Now the linker complains, because now I'm referenced 'a' in my main() function, but I did not define the static variable. In this case the linker needs to include the definition of 'a' in the executable.
// make linker happy
//...
int X::a=0;
int main()
{
X x;
X::a = 1;
x.DoSomething();
}
The same thing with functions. If you never use a function, the linker has the option of discarding it from the final executable.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.