Tech2 Forums - Powered by vBulletin

Memory Allocation for variables in C

This is a topic on Memory Allocation for variables in C within the Programmers Corner forums, part of the Technology category; Hi, Can anyone tell when is memory allocated to variables like Automatic, Global, Static etc. ?? I think it is ...

Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By SyedAtif
  1. #1
    Tech2 Members
    Join Date
    Feb 2012
    Posts
    12
    Liked
    2 times
    Reputation points
    10

    Memory Allocation for variables in C

    Hi,

    Can anyone tell when is memory allocated to variables like Automatic, Global, Static etc. ?? I think it is during compile time but exactly how I don't know.

  2. #2
    Uber Newbie
    Join Date
    Jun 2012
    Posts
    1
    Liked
    1 times
    Reputation points
    20

    Re: Memory Allocation for variables in C

    No,no memory gets allocated during compile time,a simple compilation only validates your program for syntactical and semantical errors,parses the code and makes it ready for the linker;not to complicate matters further but i think you might be confused with .bss and .data which are part of the data segment in the heap and are generated during compile time.
    Memory is only allocated once your run the program,every program has its own stack and heap trace,all static,global variables are stored in the program stack while block scope variables are allocated memory in the heap.
    As for auto,im not really sure about the way c handles it but in c++ all block level variables are defined as auto by default meaning they are deallocated once their scope runs out.As for register variables,they are stored in the cpu register although its only a direction and not a compulsion ie they may or may not.
    hatZim likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •