CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Posts
    57

    Crazy pointer error

    Hello,

    I got a really strange problem here. I define some global variables in one of my cpp project files. I want to use that variables in another cpp file (same exe) as well so I define them there as extern. Here is the code:

    in header.h which is included in file1.cpp:

    char CygwinDir[MAX_PATH]="C:\\cygwin\\";
    char SybilDir[MAX_PATH]="C:\\cygwin\\home\\sybil\\";

    in file2.cpp:

    extern char* CygwinDir;
    extern char* SybilDir;

    Compiler and linker are both happy with that. If I start the exe like that and check the pointers they are both ok, pointing to the defined strings.

    BUT here comes the crazy thing: If I now reference any of the two in some further code in file2.cpp (where they are extern), like this

    strcpy_s(tempPath, sizeof(tempPath), CygwinDir);
    StringCbCat(tempPath, sizeof(tempPath), "bin;");
    StringCbCat(tempPath, sizeof(tempPath), SybilDir);
    StringCbCat(tempPath, sizeof(tempPath), ";");

    then both pointers will get screwed. They will both point to some memory not accesable (0x635c3a43). How is that possible??? They are never changed by the code...! And furthermore: they are already broke from the startup, not only after the execution of that code!
    Let me say it again: it DOES work if I exclude any code "using" the pointers. But as soon as I reference them anywhere, in file2.cpp, they get screwed (from the startup!). I can use them without problems in file1.cpp though (where they are not extern).

    Ideas?

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Crazy pointer error

    Probably, because your extern declaration doesn't match the actual globals' declaration. Here:
    Code:
    //in header.h which is included in file1.cpp:
    
    char CygwinDir[MAX_PATH]="C:\\cygwin\\"; 
    char SybilDir[MAX_PATH]="C:\\cygwin\\home\\sybil\\";
    
    //in file2.cpp:
    
    extern char* CygwinDir;
    extern char* SybilDir;
    Change the extern declarations to:
    Code:
    extern char CygwinDir[MAX_PATH]; 
    extern char SybilDir[MAX_PATH];
    Because the types didn't match earlier. You declare two global char arrays but then the extern declaration was provided for two pointers. See if the suggestion above fixes the problem.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Crazy pointer error

    Quote Originally Posted by deck42
    I define some global variables in one of my cpp project files. I want to use that variables in another cpp file (same exe) as well so I define them there as extern. Here is the code:
    Exterminator gave you the solution as to how to fix it, so I will try to explain your problem further:

    You defined two arrays in one file, and then two pointers in another file. Those are not the same variables, regardless if they share the same name.
    If you declare a global array of T in one file, the extern that matches it must also be an array of T, not a pointer, double, int, or any other type.

    The same rule applies to any global variable -- if you define it as type T, the extern is a type T. If you want proof of this, define a global variable as a double in one file, and do an extern char in another. You will see that the modules compile and probably link (if the linker is really smart, it will give you a warning). So what do you think happens if the global variable is really a char, and you try to store a double? You have a memory overwrite.

    The issue is very simple -- since the linker sees one name that declare different types, the linker can do one of many things:

    1) Issue a warning that you have a muliply-defined name accessing differing types.

    2) The linker gives you an error that the two types are different.

    3) Whichever definition the linker sees first, that's the amount of space it will use when allocating memory for that definition.

    The linker can do any or some of those options above. If the linker sees the pointer definition first, it will allocate sizeof(char *) bytes (which is usually 4 bytes). So as soon as you try to access anything past the fourth byte, you are overwriting memory.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 19th, 2008 at 09:04 AM.

  4. #4
    Join Date
    Aug 2005
    Posts
    57

    Re: Crazy pointer error

    Thanks a lot guys, that did the trick!
    I guess I understand what was the problem. My linker didn't say anything sadly.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Crazy pointer error

    Congratulations to you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured