user1121487
user1121487

Reputation: 2680

C: static int gets strange value

(changed name of variable from original question to fit the actual code) I'm new to C and I'm implementing a queue. The error is with the static int head=0 variable. It's incremented by 1 each time dequeue() is called. The error seem to occur when the queue is dequeued and function get_person() is called. The head-variable is then as it seems getting a high random number, like 23423449. I have no idea where this comes from. However if I get rid of the "static" keyword so variable is declared as int head=0, it works fine. How come?

Upvotes: 0

Views: 710

Answers (4)

David Sykes
David Sykes

Reputation: 49832

I think you are overrunning your person array

One of the strcpy functions is going beyond the bounds of the buffers in the person object, and overwriting the head variable. I would guess the tail and nbr_elem are going too.

You should check that the number of characters you are copying does not exceed the buffer lengths, or use strncpy.

Upvotes: 1

phenompbg
phenompbg

Reputation: 401

You are calling strcpy without checking that the values you are trying to write will actually fit inside the allocated space inside the person struct.

What is most likely happening is you are writing beyond the allocated memory, and your strcpy is actually overwriting the value of head. strcpy will keep writing until it hits a null terminator ('\0').

If you were to run this in valgrind (a useful tool for finding this type of problem in large programs) it would probably tell you you have invalid writes occurring.

C assumes you know what you are doing, as long as you have access to the memory you can do with it as you please :)

Upvotes: 0

H_squared
H_squared

Reputation: 1271

If you declare a global static variable in file A.c, it means this variable is only available within the scope of this A.c file. See : http://en.wikipedia.org/wiki/Static_variable

Since you haven't posted any code, and you are metioning using the same variable in a different file (e.g. B.c), it seems like it is invoking an undefined behavior, which explains the random number your program is printing.

If you wish to use the variable in a different .c file, you should not make it static.

Upvotes: 0

datenwolf
datenwolf

Reputation: 162164

using a "global" variable in top of a included file: static int variable1=0

This clearly indicates, that you don't understand what the static keyword means on the global scope. In the global scope, outside of a function, static means, that the variable is visible to only the code within the compilation unit the variable has been defined in.

Now if you define a static variable in a header, each compilation unit that includes that header will have its own variable of that name. So your program is littered with many identically named variables each specific to the compilation unit it's in.

I think what you actually want is an non-static, extern declaration in the header, and exactly one compilation unit actually defining the variable.

Upvotes: 4

Related Questions