Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. extern char *toAscii

extern char *toAscii

Scheduled Pinned Locked Moved C / C++ / MFC
comdebugginghelpquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manish K Agarwal
    wrote on last edited by
    #1

    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

    N L 2 Replies Last reply
    0
    • M Manish K Agarwal

      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

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      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 memory

      2. When toAscii is defined as "extern char toAscii[]"

        toAscii\[0\] = 1;
      

      00401DED mov byte ptr [toAscii (004167c0)],1

      I hope you understood.

      nave [OpenedFileFinder] [My Blog]

      M 1 Reply Last reply
      0
      • N Naveen

        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 memory

        2. When toAscii is defined as "extern char toAscii[]"

          toAscii\[0\] = 1;
        

        00401DED mov byte ptr [toAscii (004167c0)],1

        I hope you understood.

        nave [OpenedFileFinder] [My Blog]

        M Offline
        M Offline
        Manish K Agarwal
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • M Manish K Agarwal

          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

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          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/[^]

          nave [OpenedFileFinder] [My Blog]

          1 Reply Last reply
          0
          • M Manish K Agarwal

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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:


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups