-
memory ?
I'm new to C++ so first programs naturally include a calculator.
My memory question is this:
I have a function for each math procedure(add, sub, mul, div,etc)
I have globally declared double a, b, ; for use with all the functions. I have read many places to keep the scope of variables as small as possible...So do the values of a, b, accumulate in memory by declaring them globally?.....ie :
0x00001 = a for first function
0x00002 = b for first function
0x00003 = a for second function
0x00004 = b for second function
or would they over write each other therefore using the same memory space??
A little confused by this ........appreciate any thoughts.....:^)
-
Don't declare variables globally. You should declare your variables a and b in the main() function. At the end of the main function, the memory set aside for a and b will be given back to the computer. Look up the reserved words auto, static, and register.
-
when you assign something to the variable it overwrites the previous value... You might want to try not using globals for example...
Code:
#include <stdio.h>
double add(double a, double b)
{
return a+b;
}
int main()
{
double x = 25.367, y = 86.254;
printf("%.3f + %.3f = %.3f\n", x, y, add(x, y));
return 0;
}
-
Re: mem?
joshdick & Rog
Thanks for your replys.........
I looked up those keywords and the info on static answered my question. Thanks again.....:D