1

I have a extern const int variable in main.cpp which I am using in mathFunctions.cpp. If I try to modify the variable in mathFunctions.cpp, the program is failing. I am using latest verion of CodeBlocks with C++ 11.

Program works fine if I do not try to modify the variable.

//main.cpp
#include <iostream>
using namespace std;
extern const int valueTen = 10;
int addValue(int);
int main()
{
    cout << addValue(2) << endl;
    return 0;
}
//mathFunctions.cpp
extern int valueTen;
int addValue(int x){
    valueTen++; //program stops because of this line
    return x + valueTen;
}
walnut
  • 21,629
  • 4
  • 23
  • 59
Biswa8998
  • 13
  • 3
  • 3
    Why are you trying to modify a `const` variable? Do you know what `const` means/does? – NathanOliver Nov 01 '19 at 21:23
  • Yes, I do. As I am new to CPP, I am trying to understand the errors I will get in such cases. – Biswa8998 Nov 01 '19 at 21:24
  • _I am trying to understand the errors I will get in such cases._ You will get "the program is failing". – Eljay Nov 01 '19 at 21:27
  • Very true. But in the IDE I am getting Process Terminated, but no proper error indicating why it is terminating. – Biswa8998 Nov 01 '19 at 21:27
  • 1
    It's because you lied to the compiler. When you lie, you get in trouble (at least some of the times). – NathanOliver Nov 01 '19 at 21:28
  • often you dont get errors when you do something wrong, in the best case something wrong happens, in the worst case it appears to be fine, until you try to show the program to your teacher/prof/customer – 463035818_is_not_an_ai Nov 01 '19 at 21:47
  • Unrelated: `extern` says "This is a declaration of a variable. The actual variable will be defined elsewhere." If you tack on an initialization, as `extern const int valueTen = 10;` does, you get a definition instead of the declaration. The `extern` goes away, and sometimes it goes away silently. This can lead to problems later, so don't use `extern` with initialization. – user4581301 Nov 01 '19 at 21:57
  • @ user4581301: https://www.learncpp.com is my mentor and didn't find the point you mentioned yet.But I will keep this in mind. Thank you. – Biswa8998 Nov 01 '19 at 22:03
  • The C and C++ are still close enough in behaviour for this to be good reading: [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files). Here is some good [documentation on `extern`](https://en.cppreference.com/w/cpp/language/storage_duration) and also [on initialization in general](https://en.cppreference.com/w/cpp/language/initialization). The latter two links are pretty low level and will probably take a read or two to translate. – user4581301 Nov 01 '19 at 22:28

1 Answers1

3

When you compile your main.cpp file, the statement extern const int valueTen = 10; tells the compiler that the valueTen variable is a constant, and will not be changed during run-time. Typically, this will 'pass on' a signal to the linker that it can allocate space for that variable in an area of memory with the READONLY attribute.

When you compile your mathFunctions.cpp file, the statement extern int valueTen; tells the compiler that valueTen is an integer variable, whose definition is provided by another module. In this file, there is nothing telling the compiler that valueTen is const, so you don't get a compiler error when you include code that modifies its value.

However, when you have built the program (linked all the individual modules), then the code produced by the valueTen++; statement will try to modify something that is located in READONLY memory - and this will cause the program to crash, with a message along the lines of "Access violation."

Feel free to ask for further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83