paper.plane
paper.plane

Reputation: 1197

How is this happening with static?

Need help to understand the following behaviour.

static int a;
int b[a];

for(int i=0; i<10; i++)
    b[i] = i+1;

for(int i=0; i<10; i++)
    cout << " " << b[i];

The output is $> 1 2 3 4 5 6 7 8 8 10

However if I declare variable 'a' as auto [like int a] then it crashes at run-time, which is obvious. But why is it working with static. Also if I run the loop for more number of time(above it is 10) it crashes. I made it 12 and it crashed. Moreover in the output 8 is coming instead of 9 in between 8 & 10 which is abnormal. Please help.....

Upvotes: 1

Views: 158

Answers (8)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

I guess you were simply lucky that it doesn't crash. a is initialised to 0, and that means you are writing to memory past the array and that can mean anything.

I assume you were expecting a crash. Fact is that even if you access memory beyond an array limit, that doesn't automatically mean you get a crash. It can also overwrite other variables or memory contents of your code. This can go noticed (e.g. with a crash), or it can be silent, producing weird results.

It is not called "undefined" behaviour for nothing.

Upvotes: 0

Bertrand Marron
Bertrand Marron

Reputation: 22210

Static variables without an explicit initializer are initialized to zero.

static int a;
int b[a];

b is initialized as an array of 0 int.

C++ doesn't do any boundary check, it lets you access the array outside its bounds, but it invokes undefined behavior.


int b[a]; declares a variable length array and this is just an extension provided by your compiler, this is not part of the standard and therefore not portable.

As @Als suggests, you could compile with the -pedantic compiler option which would throw something like

ISO C++ forbids variable-size array


What you seem to want is just

static int b[10];

Upvotes: 5

Cory Nelson
Cory Nelson

Reputation: 29981

There are a few problems here.

First and foremost: this is invalid C++. It looks like you're using C99's variable-length arrays, which never made it into any C++ standard. I'm surprised your compiler isn't at least warning you. In C++, array lengths must be a compile-time constant.

You never initialize a. When a is made static it gets initialized by default to 0, so you are creating a zero-length array. When it's not static, no such implicit initialization is done -- it has an undefined value. Thus the array is created with an undefined length, and it's quite possible it's too large to fit in your stack space and is causing the crash.

Additionally, in the static variant you're reading/writing 10 elements in a 0-length array, which also results in undefined behavior. This means the standard doesn't define what happens in this weird case, and so anything is allowed to happen. It is only working for you purely by chance!

Upvotes: 0

nirmus
nirmus

Reputation: 5103

It crashes because you don't initialize variables a (default initialized for static int is 0). Write example:

 static int a = 50;

and it will be working fine

edit

It working sometimes because you have luckily and in this piece of memory are some trash. Array in c++ doesn't throw exception when you get out of range. But when your program write on protect memory, it will crash

Upvotes: 0

Ajay
Ajay

Reputation: 18421

You are accessing array out-of-bounds, which would simlpy cause buffer-overrun. The program may or may not run, and hence the term "undefined behavoir"

Upvotes: 0

Ani
Ani

Reputation: 1526

The size of array b[] is not known, and I don't see that you have assigned any value to a , what you are seeing is undefined behavior.

Assuming a is not global, When made static, it will not be on the stack, instead, it may be placed in the data section

Upvotes: 1

A. K.
A. K.

Reputation: 38156

static int a;

means a is initialized to zero so the following line:

int b[a];

makes no sense.

You getting output is completely kind of strange because i'm getting compilation error;

error: array bound is not an integer constant

In C++(at least) the variable a has to be const.

Upvotes: 2

zvrba
zvrba

Reputation: 24546

In the absence of an initializer, static variables are guaranteed to be initialized to zero.

Upvotes: 0

Related Questions