0

hi recently I was learning C++ in Visual studio when I stopped on a problem when I want to use my method to log an int i need to convert it to const char* but when I do it things just crash is my code:

#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;

double pi = 3.14159265358979323846;

class Log {

int level = 1;

public:

    void set_Level(const char* lvl)
    {
        if (lvl == "LOW")
        {
            level = 1;
        }
        else if (lvl == "NORMAL")
        {
            level = 2;
        }
        else if (lvl == "HIGH")
        {
            level = 3;
        }
        else
        {
            level = 1;
        }
    }

    void Error(const char* message)
    {   
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x04);
        cout << "[ERROR]: " << message << endl;
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
    }

    void Warning(const char* message)
    {
        if (level != 3)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x06);
            cout << "[WARNING]: " << message << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
        }
    }

    void Info (const char* message)
    {
        if (level == 1)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x03);
            cout << "[INFO]: " << message << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
        }
    }
};

int main()
{
Log log;
log.set_Level("LOW");

int x = 10;

log.Info( (const char*) x);
}

it errors out and a page pops up saying: enter image description here

and its not my class because if i run log.Info("hi"); it works just fine!

Farouk
  • 261
  • 1
  • 10

3 Answers3

3

"hi" is a null terminated string literal. int x is not.

When you cast (incorrectly) int x to a const char * you are telling the compiler, here is a pointer to a contiguous set of bytes that has a null termination char at the end of the sequence. Of course, int is not a pointer - so its value 10 is probably a invalid address to start with (and that alone will most likely cause seg fault). And even if it was valid it would be "pointing" to some "arbitrary" address. C++ lets you do this conversion when you cast like you are - but you should not cast like this.

Use static_cast<const char *>(x) and then you should get a compiler warning telling off for this :)

And here is the error you get from the compiler:

<source>: In function 'int main()':
<source>:13:10: error: invalid 'static_cast' from type 'int' to type 'const char*'
   13 |     xxxx(static_cast<const char *>(x));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
code_fodder
  • 15,263
  • 17
  • 90
  • 167
2

casting an int to a const char * is not safe.

i would do this

std::string s = std::to_string(x);
log.info(s.c_str());

or

std::ostringstream oss;
oss << x;
log.Info(oss.str().c_str());
Mathias B.
  • 343
  • 2
  • 12
1

char* is a pointer type and stores addresses as its values. When you convert 10 to const char* . This represents an address value of 10, which might be inaccessible and lead to crashes and segmentation faults.

What you need to do is convert 10 to C-style string using itoa function.

lastbreath
  • 383
  • 1
  • 11