Different signature problem with extern variables
Related Blog Items
- How to inlcude C header files in C++
- Name Mangling
- How can I create a C++ function that is callable by my C code?
- Program Linkage
- Swapping variables without additional space
| File1.c | File2.c | |
1 int arr[80]; |
1 extern int *arr;2 int main()3 {4 arr[1] = 100;5 return 0;6 }
|
(1) int arr[80];
(2) int *arr;
are different things. (1) will "create space" for 80 integers in the data segment. "arr" is just shorthand for that memory location (in the data segment). (2) will "create space" for one pointer in the data segment.
So when you say:
extern int* arr;
You are actually telling the compiler to assume there’s exactly one pointer in some other translation unit. When you do arr[1]=100, the code thats going to be generated in x86 assembly would be something like.
mov esi, arr
mov [esi+4], 100
key point here is that we are moving the "contents of arr" into esi.
in the case of an array, the code would look exactly the same, with one major semantic difference
mov esi, $arr,
mov [esi+4], 100
here "arr" is a constant (referring to the location where the array begins).
Here in this case we encounter a linking error, if we make the declarations to same thing ex. to array, then it works fine.
It is well explained in Andrew Koeing’s "c traps and pitfalls"
[tags] extern variables, different extern variables in files[/tags]
Popularity: 5%
You need to log on to convert this article into PDF
Related Blog Items - How to inlcude C header files in C++
- Name Mangling
- How can I create a C++ function that is callable by my C code?
- Program Linkage
- Swapping variables without additional space
Related Blog Items
- How to inlcude C header files in C++
- Name Mangling
- How can I create a C++ function that is callable by my C code?
- Program Linkage
- Swapping variables without additional space
No Comments
No comments yet.