Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:25:06+00:00 2026-05-11T01:25:06+00:00

This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining

  • 0

This post reference to the One Definition Rule.

Wikipedia is pretty bad on explaining how to implement it

Where can I find good ressources about guidelines to follow in C++ .NET?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:25:06+00:00Added an answer on May 11, 2026 at 1:25 am

    The one definition rule basically means that a variable/function can only be located at one place in address space of the compiled executable. One way to think of it is while you compile, there is an array of memory to be used in the compiled program (object code), and a lookup table to reference variable/function locations. This is done on a per process level. Suppose that the following is a simple program:

    file1.cpp

    int square(int x); // this is a declaration extern int someVariable; // this is a declration  void square(int x)  // this is a definition {     return x * someVariable; } 

    file2.cpp

    int square(int x); // this is a declaration  int someVariable; // this is a definition     void main() {     someVariable = 12;     someVariable = square(4); }        

    When the compiler starts compiling the object code, it reads in the declarations, and puts thing in its table. AT the end of compiling file1.cpp, it will end up with something like this:

    declarations:     square (XX): function that returns int, and takes a single int as parameter [4 bytes]     someVariable (YY): integer [4 bytes] data:     12 34 56 78 aa XX XX XX XX ab cd definition:     square: starts at address 0 

    This assumes that the function gets compiled to those particular assembly instructions. At linker time, XX XX XX XX will get replaced by the address of someVariable.

    File2 ends up something like:

    declarations:     square (XX): function that returns int, and takes a single int as parameter [4 bytes]     someVariable (YY): integer [4 bytes] data:     00 00 00 00 12 34 56 78 12 34 56 YY YY YY YY 23 21 definitions:     someVariable: starts at address 0     main: starts at address 4 

    And in this case, the YY will be replaced by the address of square.

    That’s where the linker comes into play. The linker’s job is to go through the list, and build up a table of where everything is in the address space of the program at compile time. However, there is a problem if two object files have the same definition of a variable when it tries to link. If there were two definitions of someVariable in the above example, then it wouldn’t know what to replace YY with. Likewise, if there’s no definition, then you get ugly linker errors.

    The ‘solution’ to the rule is to partition your file such that you have definitions only in .cpp files, and have declarations of things in your .h files, so the example above would become:

    file1.cpp

    #include 'file2.h'  void square(int x)  // this is a definition {     return x * someVariable; } 

    file1.h

    int square(int x); // this is a declaration 

    file2.cpp

    #include 'file1.h'  int someVariable; // this is a definition     void main() {     someVariable = 12;     someVariable = square(4); } 

    file2.h

    extern int someVariable; 

    Note that this is an incredibly simple example, and that it doesn’t really apply in .NET since there isn’t a concept of a distinction between a declaration and definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can also use the new operator just like with… May 11, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer Here's a hint: Use radio buttons. ;) I wouldn't recommend… May 11, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer Take all the code and make it into a DLL,… May 11, 2026 at 8:08 pm

Related Questions

I have a project that is stored in a Subversion repository. In this repository,
I've been having tons of problems getting the non-xml configuration for Castle Windsor set
With reference to this programming game I am currently building. Thanks to the answers
This is a follow up to this post on passing messages between two programs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.