1

Is there a way to get the line the code is being executed on?

For example:

int death() {
  cout << "An error has occurred on line " << line << endl;
  exit(128);
}

Then if I call death() function on line 64 it tells me that the error occurred on line 64.

EDIT: Most people seem to be telling me to use the __LINE__ macro. I know you can do that:

#define DEATH(message) death(__LINE__, message)

where

int death(int line, string message) {
  cout << "An error has occurred at line << line << endl;
}

But I'm thinking of using it in such a way:

string readFile(string file) {
  string str;
  ifstream stream(file);
  if (!stream) { death(); } //if this is line 23, it prints line 23
  stream >> str;
  stream.close();
  return str;
}

//instead of printing line 64 if readFile() is executed on line 64

And using the __LINE__ macro alone only returns the line at which the death() function was executed. In other words, if I execute readFile() at line 64, I want death() to print line 64, not the line at which it was defined inside the readFile() function.

5
  • So, are you expecting death() to terminate the program as well as log the error? If so, I think that you simply need to have a function call such as death(__LINE__) and call exit() within the death() function. If you simply want death() to end the current function, then just add a return statement directly after you call death(). Commented Feb 5, 2015 at 8:50
  • Looks like you want stack trace? Commented Feb 5, 2015 at 8:53
  • Yes, I believe that is the terminology Commented Feb 5, 2015 at 8:55
  • There is no portable way to get a stack trace. It is very much platform/compiler dependent. On Windows you have CaptureStackBackTrace (msdn.microsoft.com/en-us/library/windows/desktop/bb204633.aspx) for example. With gcc on other platforms you stackoverflow.com/questions/77005/… Commented Feb 5, 2015 at 9:14
  • If you only want a single line number (rather than a complete stack trace) then you can simply store the output of __LINE__ at an earlier point. I've updated my answer to give an example of this; it doesn't report the calling line, but I think that the basic idea could work - perhaps with a bit of pre-processor trickery to insert the CAN_DIE macro before each call to someFunction()? Commented Feb 5, 2015 at 9:25

4 Answers 4

2

Try using the __LINE__ macro:

The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.

__FILE__

This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in #include or as the input file name argument. For example, /usr/local/include/myheader.h is a possible expansion of this macro.

__LINE__

This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its "definition" changes with each new line of source code.

__FILE__ and __LINE__ are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was detected.

If you want to get a line number other than the current line, you'll need to store the line number from __LINE__ at an earlier point. For example, this code gets the first line number of the function that calls death():

#include <stdio.h>
#include <stdlib.h>

/* Set up some stuff to keep track of where you died. */
int DEATH_LINE = 0;
#define CAN_DIE DEATH_LINE = __LINE__;
void death() { printf("OH NO, YOU DIED AT LINE %d!\n", DEATH_LINE); }

/* This function can die, so get the first line number using CAN_DIE */
int someFunction() { CAN_DIE
  printf("Hello, World!\n");
  death();
  printf("Goodbye, Cruel World!\n");
  return 0;
}

int main() { return someFunction(); }

This produces the following output:

Hello, World!
OH NO, YOU DIED AT LINE 10!
Goodbye, Cruel World!

Process returned 0 (0x0)   execution time : 0.009 s
Press any key to continue.

Alternatively, you could get the line number that calls the function as follows:

#include <stdio.h>
#include <stdlib.h>

int DEATH_LINE = 0;
#define CAN_DIE(x) ((DEATH_LINE = __LINE__), (x));
void death() { printf("OH NO, YOU DIED AT LINE %d!\n", DEATH_LINE); }

int someFunction() {
  printf("Hello, World!\n");
  death();
  printf("Goodbye, Cruel World!\n");
  return 123;
}

int main() { return CAN_DIE(someFunction()); }

This produces the following output:

Hello, World!
OH NO, YOU DIED AT LINE 15!
Goodbye, Cruel World!

Process returned 123 (0x7B)   execution time : 0.008 s
Press any key to continue.

Note that the second example requires the calling function to wrap the other function (i.e. the one that actually calls death()) in a macro to set the current line number correctly.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to mix macro and functions:

int death_at(int line) {
   cerr << "An error has occurred on line " << line << endl;
}
#define DEATH() death_at(__LINE__)

You might even have

#define FAILURE(Out) do { cerr << __FILE__ << ":" __LINE__ \
                          << ": " << Out << endl; } while(0)

then you could code:

FAILURE("x is " << x)

And you might even use some throw near the end of your FAILURE macro definition.

Comments

1

You can use __LINE__.

The __LINE__ macro is often combined with the __FILE__ macro, which expands to the current file name.

#define ERR(msg) printf("%s : %d", (msg), __LINE__)

const char * msg ="An error has occurred on line ";
cout << ERR(msg) << endl;

Get code line with LINE

1 Comment

Yeah here it is again Get code line with LINE
0

Use __LINE__

void foo() {
  if (somethingWrong) { death(__LINE__); } // Pass this line number to death()
}

void death(int line) {
  cout << "An error has occurred on line " << line << endl;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.