Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Management with variables

Memory Management with variables

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehardwarehelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IceBerG71
    wrote on last edited by
    #1

    Just a question void main(void) { int i; for(i=0;i<100;i++) { Check(1); } } void Check(int i) { char szMsg[100]; } if the Check function will be called 100 times, will the szMsg be 1) created and disallocated 100 times as well 2) created once and remained till the Main ended 3) created once and not freed at all 4) created 100 times and not freed at all if the answer is 1 or 2, then what is the difference / advantages if i use malloc or LocalAlloc instead? will there be performance problem? I am doing a project on embedded device and memeory managment is crucial. I thanx anyone who gave me expertise answer.

    S G 2 Replies Last reply
    0
    • I IceBerG71

      Just a question void main(void) { int i; for(i=0;i<100;i++) { Check(1); } } void Check(int i) { char szMsg[100]; } if the Check function will be called 100 times, will the szMsg be 1) created and disallocated 100 times as well 2) created once and remained till the Main ended 3) created once and not freed at all 4) created 100 times and not freed at all if the answer is 1 or 2, then what is the difference / advantages if i use malloc or LocalAlloc instead? will there be performance problem? I am doing a project on embedded device and memeory managment is crucial. I thanx anyone who gave me expertise answer.

      S Offline
      S Offline
      Stefan Pedersen
      wrote on last edited by
      #2

      Short answer: 1 But note that it is created on the STACK and not on the HEAP (like malloc would). In an ideal world this wouldn't take any time at all (the SP has to be adjusted anyway) but (and I'm not 100% sure about this) the compiler might decide that the memoryblock needs to be zeroed out which ofcourse takes time. By using malloc instead you would be going through the memorymanager which tracks the allocated blocks which is pretty slow. The fastest way (IF the compiler clears the block) would be to declare the block static. And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows

      I 1 Reply Last reply
      0
      • I IceBerG71

        Just a question void main(void) { int i; for(i=0;i<100;i++) { Check(1); } } void Check(int i) { char szMsg[100]; } if the Check function will be called 100 times, will the szMsg be 1) created and disallocated 100 times as well 2) created once and remained till the Main ended 3) created once and not freed at all 4) created 100 times and not freed at all if the answer is 1 or 2, then what is the difference / advantages if i use malloc or LocalAlloc instead? will there be performance problem? I am doing a project on embedded device and memeory managment is crucial. I thanx anyone who gave me expertise answer.

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        According to the C++ language definition, the variable szMsg is allocated on entry to the Check() function, and deallocated on exit. Typically, and in the Windows environment, local memory used in this way is from the stack. Stack allocations and deallocations are inexpensive, since they usually amount to moving the stack pointer by the size of the value. In the embedded environment, however, things may be different. Depending on the CPU architecture, local memory may be managed via a heap or some other structure. The cost of local memory depends upon the CPU and how the compiler is implements it. PIC microcontrollers, for example, typically only store return addresses on the stack. Local memory is managed via a heap or statically through call graph analysis.


        Software Zen: delete this;

        S 1 Reply Last reply
        0
        • S Stefan Pedersen

          Short answer: 1 But note that it is created on the STACK and not on the HEAP (like malloc would). In an ideal world this wouldn't take any time at all (the SP has to be adjusted anyway) but (and I'm not 100% sure about this) the compiler might decide that the memoryblock needs to be zeroed out which ofcourse takes time. By using malloc instead you would be going through the memorymanager which tracks the allocated blocks which is pretty slow. The fastest way (IF the compiler clears the block) would be to declare the block static. And if the paths that I have followed/have tread against the flow/there is no need for sorrow I am coming home Return, Crüxshadows

          I Offline
          I Offline
          IceBerG71
          wrote on last edited by
          #4

          In Window CE devices, Will there be any difference in creating on the stack and heap? I have a current program that sometimes during usage by client, will cause the program to crash.Went through the codes and debugging, still cannot find the bug. It only happens randomly but particularly at a specific places where there is a lot of loading of GUI on the screen. Hence, i need to fine tune all this problems, hopefully an upgrade on the program won't cause this program. I am also trying the try-except method to prevent exception error.

          M 1 Reply Last reply
          0
          • G Gary R Wheeler

            According to the C++ language definition, the variable szMsg is allocated on entry to the Check() function, and deallocated on exit. Typically, and in the Windows environment, local memory used in this way is from the stack. Stack allocations and deallocations are inexpensive, since they usually amount to moving the stack pointer by the size of the value. In the embedded environment, however, things may be different. Depending on the CPU architecture, local memory may be managed via a heap or some other structure. The cost of local memory depends upon the CPU and how the compiler is implements it. PIC microcontrollers, for example, typically only store return addresses on the stack. Local memory is managed via a heap or statically through call graph analysis.


            Software Zen: delete this;

            S Offline
            S Offline
            SiddharthAtw
            wrote on last edited by
            #5

            In microcontollers (8051), the code segment and data segment is embedded in to the chip, while Stack and heap can be on RAM. Hope it helps.

            1 Reply Last reply
            0
            • I IceBerG71

              In Window CE devices, Will there be any difference in creating on the stack and heap? I have a current program that sometimes during usage by client, will cause the program to crash.Went through the codes and debugging, still cannot find the bug. It only happens randomly but particularly at a specific places where there is a lot of loading of GUI on the screen. Hence, i need to fine tune all this problems, hopefully an upgrade on the program won't cause this program. I am also trying the try-except method to prevent exception error.

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              Another factor to consider is the limited amount of stack available; by default, you have 64KB of stack, which has to encompass all of your thread's execution. If you run out of stack, Windows CE will raise a Stack Overflow exception. You shouldn't try to catch this - it indicates a serious problem. If uncaught, the thread will be terminated. You should really only use the stack for short-lived items and in leaf functions (functions that call no other functions). Using the stack for anything that will have a long lifetime (e.g. your main frame window) isn't a good idea.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups