Discussion Board

Results 1 to 2 of 2

Thread: Stack and scope

  1. #1
    Registered User torui's Avatar
    Join Date
    Mar 2003
    Posts
    26
    Question about stack and C++ scope.
    I wrote long function with lots of scope {} for error handling.
    On the top of function, stack is allocated for all variables
    even inside of scope.

    On Console examples.

    LOCAL_C void doExampleL() {
    int rc;
    console->Printf(_L("Start Example\n"));

    {
    FILE* fp = fopen("test.txt", "w");
    if (fp == NULL) {
    char msg1[1024];
    // .. error report
    }
    }
    }

    On entering doExampleL() function stack should be allocated for "int rc"(4byte) and return pointer(4byte). But stack is allocated for fp(200byte?) and msg1(1024byte).

    I tested WINS build for VC7. I use Visual Studio.NET as bugger.
    When entering doExampleL() function, Stack pointer(ESP register) consume over 1K byte.

    That means scoping does not help for reducing stack size.
    Do you know how to reduce stack size with scoping?

    I use S60_2nd_FP2 SDK.

  2. #2
    Registered User laa-laa's Avatar
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122
    Generally, {} only applies to visibility scoping, not allocation scoping. A stack allocation frame for a function is calculated at compile time (usually) and it applies to the whole function, not a single {} block.

    To save on stack usage, put portions of your function in separate functions, or simply use heap-based buffers.

    Lauri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved