extern char *toAscii
-
I have a global variable defined as "char toAscii[255]" in some MyFile.c I declared another variable as "extern char *toAscii" in some other file YourFile.c of same console application. In YourFile.c, I accessed toAscii as "toAscii[0] = 1;" No linking error. Linker generate mangaled name same for both as "int * toAscii" (?toAscii@@3PAHA) but at runtime "toAscii[0] = 1" is causing crash because it point to a NULL while debugger showing a valid address. If I change "extern char *toAscii" to ""extern char *toAscii[]", every thing works fine. I just want to know why in first case it toAscii points to NULL at runtime while we have same mangaled name for both. I am using VC2005.
Manish Agarwal manish.k.agarwal @ gmail DOT com
-
I have a global variable defined as "char toAscii[255]" in some MyFile.c I declared another variable as "extern char *toAscii" in some other file YourFile.c of same console application. In YourFile.c, I accessed toAscii as "toAscii[0] = 1;" No linking error. Linker generate mangaled name same for both as "int * toAscii" (?toAscii@@3PAHA) but at runtime "toAscii[0] = 1" is causing crash because it point to a NULL while debugger showing a valid address. If I change "extern char *toAscii" to ""extern char *toAscii[]", every thing works fine. I just want to know why in first case it toAscii points to NULL at runtime while we have same mangaled name for both. I am using VC2005.
Manish Agarwal manish.k.agarwal @ gmail DOT com
There are difference in the code generated for "toAscii[0] = 1;" when toAscii is declared as "extern char *toAscii" and "extern char toAscii[]". 1. When toAscii is defined as "extern char *toAscii"
toAscii\[0\] = 1;
00401DED mov eax,[toAscii (004167c0)] // copy 4 bytes from the memory pointer by 004167c0
// which is actually the content of first 4 bytes of the array
//
00401DF2 mov byte ptr [eax],1 // now set 1 to the memory pointed by eax.
// which will be an invalid memory2. When toAscii is defined as "extern char toAscii[]"
toAscii\[0\] = 1;
00401DED mov byte ptr [toAscii (004167c0)],1
I hope you understood.
-
There are difference in the code generated for "toAscii[0] = 1;" when toAscii is declared as "extern char *toAscii" and "extern char toAscii[]". 1. When toAscii is defined as "extern char *toAscii"
toAscii\[0\] = 1;
00401DED mov eax,[toAscii (004167c0)] // copy 4 bytes from the memory pointer by 004167c0
// which is actually the content of first 4 bytes of the array
//
00401DF2 mov byte ptr [eax],1 // now set 1 to the memory pointed by eax.
// which will be an invalid memory2. When toAscii is defined as "extern char toAscii[]"
toAscii\[0\] = 1;
00401DED mov byte ptr [toAscii (004167c0)],1
I hope you understood.
yes, but you don't think that linker should be able to report such errors. Why it resolve "extern char *toAscii" to char "char toAscii[255]"
Manish Agarwal manish.k.agarwal @ gmail DOT com
-
yes, but you don't think that linker should be able to report such errors. Why it resolve "extern char *toAscii" to char "char toAscii[255]"
Manish Agarwal manish.k.agarwal @ gmail DOT com
Manish K. Agarwal wrote:
but you don't think that linker should be able to report such errors
Yes, as you mentioned if the mangle name where different, this wouldn't have happened. Try reporting it in http://connect.microsoft.com/[^]
-
I have a global variable defined as "char toAscii[255]" in some MyFile.c I declared another variable as "extern char *toAscii" in some other file YourFile.c of same console application. In YourFile.c, I accessed toAscii as "toAscii[0] = 1;" No linking error. Linker generate mangaled name same for both as "int * toAscii" (?toAscii@@3PAHA) but at runtime "toAscii[0] = 1" is causing crash because it point to a NULL while debugger showing a valid address. If I change "extern char *toAscii" to ""extern char *toAscii[]", every thing works fine. I just want to know why in first case it toAscii points to NULL at runtime while we have same mangaled name for both. I am using VC2005.
Manish Agarwal manish.k.agarwal @ gmail DOT com
Hi, IIRC the correct way to handle this is: - have a single .c file (say file1.c) that declares the variable (either char toAscii[256] or char* toAscii) - have all other interested .c files refer to it using
extern char* toAscii
the best way to do that is to include it in a .h file - best is to have file1.c also include that header, so the compiler checks both are compatible. Doing it that way the variable gets allocated only once, and the declarations are correct and available everywhere. :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused: