user9008839
user9008839

Reputation:

Does redefining variables over and over again increase memory?

For example, if I have a loop

for(int i = 0; i < N; i++) {
    int a, b, c, d, e;
    cin >> a >> b >> c >> d >> e;
    foo(a, b, c, d, e);
}

or

int a, b, c, d, e;
for(int i = 0; i < N; i++) {
    cin >> a >> b >> c >> d >> e;
    foo(a, b, c, d, e);
}

which one should be faster? In the first case, I define the five variables inside the for loop, and in the second one, I define it outside.

I've seen posts that talk about "which is faster" such as Difference between declaring variables before or in loop?, but I'm not sure which one takes less memory.

I don't really care about the complexity, but rather the amount of memory used up in my program.

Obviously, in this case, it doesn't really matter, but what if I have a multi-dimensional for-loop and I have to define variables millions of times? Or, what if I define a large vector with many elements inside multiple times?

I apologize if this question is really simple, as I am new to c++. Any help would be greatly appreciated. Thanks guys.

Upvotes: 0

Views: 507

Answers (2)

asimes
asimes

Reputation: 5894

I copy / pasted your code into two files named foo.cpp and bar.cpp:

foo.cpp:

#include <iostream>

void foo(int a, int b, int c, int d, int e) {
        std::cout << a << b << c << d << e << std::endl;
}

int main(int argc, char** argv) {
        for (int i = 0; i < argc; i++) {
                int a, b, c, d, e;
                std::cin >> a >> b >> c >> d >> e;
                foo(a, b, c, d, e);
        }
        return 0;
}

bar.cpp:

#include <iostream>

void foo(int a, int b, int c, int d, int e) {
        std::cout << a << b << c << d << e << std::endl;
}

int main(int argc, char** argv) {
        int a, b, c, d, e;
        for (int i = 0; i < argc; i++) {
                std::cin >> a >> b >> c >> d >> e;
                foo(a, b, c, d, e);
        }
        return 0;
}

I compiled them with no optimization like this (with gcc version 6.3 if it matters):

g++ -O0 foo.cpp -o foo
g++ -O0 bar.cpp -o bar

Both results, foo and bar, were identical even with no optimization. To be certain of this, you can compare the objdumps:

objdump -D foo > foo.txt
objdump -D bar > bar.txt
diff foo.txt bar.txt

However, some things to point out:

  • int is a primitive type, if these were say instead instances of a class then perhaps there could be a difference (in performance, not memory usage). This would be because each iteration of the loop would create and destroy the instances if the compiler (without optimization) did not make the results the same
  • Your variables are taking memory on the stack (because you did not use new). Even in the case where the compiler was not able to produce the same result from the two codes you would be using the same amount of memory
  • If you were to instead create variables with new and not delete them when they were created inside the for loop then the variables would take space on the heap instead of the stack and then the first code would use more memory. There is no reason to do this but if you did you could still use the same amount of memory by calling delete at the end of each loop

Upvotes: 1

SoronelHaetir
SoronelHaetir

Reputation: 15162

No, each loop iteration will use the same memory. And with primitive types (such as int) if you don't initialize them you won't even suffer that penalty. If you were to do that with a type that did require initialization the second might well be faster (though if you were using such a type whether it were inside the loop or not would likely be determined by what you were doing with the instances).

Upvotes: 0

Related Questions