internal/external linkage

This is a discussion on internal/external linkage within the C++ Programming forums, part of the General Programming Boards category; I was wandering why this works: file1.cpp Code: #include <iostream> struct Point { int a, b; }; void f(Point *); ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    internal/external linkage

    I was wandering why this works:

    file1.cpp
    Code:
    #include <iostream>
    
    struct Point {
           int a, b;
    };
    
    void f(Point *);
    
    int main(int argc, char *argv[])
    {
          Point a;
          f(&a);
          return 0;
    }
    file2.cpp
    Code:
    struct Point {
           int a, b, c;
           float z;
    };
    
    void f(Point *p)
    {
         p->a = 5;
         p->b = 5;
         p->c = 7;
         p->z = 25.5;
    }
    What will happen here?

    My compiler doesn't really detects anything.

    Edit: the program crashes when i try changing value of z there.

    Does this means the programmer has to take care of inconsistent type declarations throughout several files?
    Last edited by Tool; 08-03-2010 at 01:58 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Point a (from file1) is in stack storage. function f() will write beyond the proper limit of file1's Point a. In this case, probably no big deal (although a gross error), since there is nothing else (obvious) in the stack that is used by main.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    For pretty much the same reason that

    Code:
    char c;
    strcpy( &c, "oops" );
    Beyond being syntactically valid, there is only a finite amount of checking that the compiler can do from inspecting a single source file. Code checking tools like lint, which check ALL the code would most likely spot the anomaly. But even with these, it's not that hard to create something which passes every analysis tool, but the result simply doesn't work.


    > since there is nothing else (obvious) in the stack that is used by main.
    You mean like the return address back to the calling environment?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    So, a good thing to do would be to include type definitions into a .h file and then just include the .h file in the files which depend on those types?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Salem View Post
    > since there is nothing else (obvious) in the stack that is used by main.
    You mean like the return address back to the calling environment?
    ha ha ha. ho ho ho. I figured there were other things out there, but I have never studied stack structure and layout (thus my clever use of "obvious")
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Tool View Post
    So, a good thing to do would be to include type definitions into a .h file and then just include the .h file in the files which depend on those types?
    Yes. Excellent idea.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by Salem View Post
    Beyond being syntactically valid, there is only a finite amount of checking that the compiler can do from inspecting a single source file. Code checking tools like lint, which check ALL the code would most likely spot the anomaly. But even with these, it's not that hard to create something which passes every analysis tool, but the result simply doesn't work.
    It would take quite a bit of analysis to prove that this code is wrong (and it might not even be wrong). Simply having two types with the same name is not necessarily an error -- in fact, it definitely does happen and is why namespaces were invented. The lint tool would have to be smart enough to see that an instance of one type is being treated as an instance of another by analyzing data flows and function calls. And even if it could see that, it still might be intentional (though probably a bad idea).

    This type of thing could be caught at compile time if aggregate type names had signatures applied to them the way that function names do. But that's probably a pipe dream.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. internal/external linkage
    By MarkZWEERS in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2008, 03:34 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21