Jump to content

Size of global and local variables

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
ayiesal

ayiesal

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi... Please help me.
How can we know the size of global variable and local variable (in bytes) in any given C program? I was trying to do a benchmark on my simulation, and the benchmark file should be the various number of variables size. But... how to compute the size of variables in large C program (many functions on a single file)? Thanks in advance :-)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can use sizeof(type) to get the size as a multiple of the sizeof(char).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ayiesal

ayiesal

    Newbie

  • Members
  • PipPip
  • 10 posts
Let say if I have a c file like this...


//some global var here

funct_1(){
//some local variable here
}

func_2(){
//some local variable here
}

main(){
//some local variable here
}

How to know the size of all local n global variable using sizeof(type)? Do I need to type sizeof(type) after every variable declaration? It will be tedious if the file is too long doesn't it? Is there a way... any compiler/tool/command/instruction/... to do this?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You would need to look at each variable type you use in your program.
sizeof(int) gives the size of an int.
sizeof(float) gives the size of an float.
sizeof(double) gives the size of an double.
etc.

Use that and multiply but the number of each variable type to get the total memory used.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
I think what your trying to ask is if you can dynamically find out how much memory your variables are taking up at any one time and then be able to print it to the screen (or do something else with the result) rather then having it precoded?

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#6
ayiesal

ayiesal

    Newbie

  • Members
  • PipPip
  • 10 posts
Actually, I need to have result look something like this... (for example)

File: Size of Global Var (bytes): Size of Local Var (bytes):
testing.c 50 100
anyfile.c 1000 5000
... ... ...

Do you have any idea how I can achieve this? :(