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 :-)
Size of global and local variables
Started by ayiesal, Mar 21 2009 03:44 PM
5 replies to this topic
#1
Posted 21 March 2009 - 03:44 PM
|
|
|
#2
Posted 21 March 2009 - 05:09 PM
You can use sizeof(type) to get the size as a multiple of the sizeof(char).
#3
Posted 21 March 2009 - 05:42 PM
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?
//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
Posted 21 March 2009 - 05:50 PM
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.
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.
#5
Posted 22 March 2009 - 03:59 AM
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
Posted 22 March 2009 - 06:17 AM
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? :(
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? :(


Sign In
Create Account


Back to top









