char a[5000];
main()
{}


and in another file
main()
{
    char a[5000];
}

after compiling both file.
exe of both file size have large difference.
why

Recommended Answers

All 3 Replies

In the first, the global array is likely to be stored in the BSS (uninitialized data) segment of the executable file itself while the local array is more likely to be drawn from the stack at runtime. Because stack space is already allocated to the executable in one way or another, local variables don't contribute extra bytes to the size of the file.

In the first, variable declared is an extern; unless it is assigned a value the memory will not be allocated. In the second,variable is a local to the block and in 'c' execution starts from main() soon after variable is declared; memory space is occupied for 8000 addresses.

In the first, variable declared is an extern; unless it is assigned a value the memory will not be allocated.

That's incorrect. Even for variables declared as extern, an object will be linked to it when creating the executable[1], and the object has a size that will contribute to the executable if it's stored in either the data or BSS segment.

In this case linkage is pretty much irrelevant to the question.

[1] Unless there's no actual object, in which case the linker will puke all over your code if you try to use the variable anywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.