Recursion in Visual Studio

霸气de小男生 提交于 2019-12-02 03:49:24

问题


When I run this very simple recursive code, I found out that when the function "recursCheck()" is using integer parameters my memory jumps to 200 MB in Win32 mode and 45 MB in x64 mode, and when the parameter is of type "short" the used memory is 4.7 MB Win32 mode and 1.7 MB for x64. sizeof(int) shows 4 bytes and sizeof(short) shows 2 bytes. How is this possible - 1 000 000 x 2 bytes = 2 MB and 1 000 000 x 4 bytes = 4 MB NOT 200 MB !!! So why this problem appers(short vs int) and why Win32 mode takes less memory than x64? I`m using Visual Studio 2013, 64 bit OS Windows7, CPU i5, 4 GB RAM. The same issue happens in Visual Studio 2008, but there even more memory is taken - 400 MB instead of 200 MB.

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#define SIZE_T_T 1000000
using namespace std;

void recursCheck(int i);

int main()
{
    //cout << sizeof(int) << endl;
    int i = SIZE_T_T;
    recursCheck(i);

    system("PAUSE");
    return 0;
}

void recursCheck(int i)
{
    //cout << i << endl;
    if (i != 0)
    {
        recursCheck(--i);
    }
    else
    {
        system("PAUSE");
    }
}

Win32 MODE WITH "i" of type "int": image 1 Win32 MODE WITH "i" of type "short": image 2

I also found out that before the second system("PAUSE") before the end of the main program the memory IS STILL in use. I know C++ doesnt manage memory, but shouldnt when all "recursCheck()" functions end the token memory be freed? I lose the point of LOCAL variable. How you see the parameter is passed BY VALUE. SHOULDN`T IT BE FREED AFTER FUNCTION END AUTOMATICALLY?

来源:https://stackoverflow.com/questions/32523171/recursion-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!