Thread: deprecated char *

  1. #1
    Registered User
    Join Date
    Aug 2023
    Posts
    45

    Lightbulb deprecated char *

    Code:
    static char *abortMsg[] = {
        NULL,
        "Invalid command line arguments",
        "Failed to open source file",
        "Failed to open intermediate form file",
        "Failed to open assembly file",
        "Too many syntax errors",
        "Stack overflow",
        "Code segment overflow",
        "Nesting too deep",
        "Runtime error",
        "Unimplemented feature",
    };
    In another thread I was given the advice to get rid of char * and use string type. However, in the above code from my old project I get warnings about this conversion. The recommendation was to change everything to string. I don't see how to do this, but another recommendation that worked and got rid of the warnings was to change to static const char* [etc]. I don't see how to use string or string_view to fix these warnings.
    Any advice appreciated.
    maxcy/wt1v

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like this?
    Code:
    #include <string>
    
    static std::string abortMsg[] = {
        NULL,
        "Invalid command line arguments",
        "Failed to open source file",
        "Failed to open intermediate form file",
        "Failed to open assembly file",
        "Too many syntax errors",
        "Stack overflow",
        "Code segment overflow",
        "Nesting too deep",
        "Runtime error",
        "Unimplemented feature",
    };
    Changing to const char * is also fine, since there is never any intention to try and change the strings.

    > The recommendation was to change everything to string.
    That's more appropriate when you're trying to do your own memory management in some form or other.

    Examples.
    Code:
    char buff[SIZE];
    strcpy(buff,"something");
    
    char *buff = (char*)malloc(SIZE);  // not a good thing to use in C++
    strcpy(buff,"something");
    free(buff);
    
    char *buff = new[SIZE];
    strcpy(buff,"something");
    delete [] buff;
    If you're ever in the position of having to calculate (or hope/guess) the value for SIZE, or in any way forgetful about freeing memory after you've allocated it, then std::string is your friend.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with deprecated functions
    By ferrad in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2017, 11:46 AM
  2. deprecated conversion from string constant to char*
    By baxy in forum C++ Programming
    Replies: 7
    Last Post: 08-07-2013, 05:52 AM
  3. Replies: 8
    Last Post: 07-24-2011, 11:34 PM
  4. warning: deprecated conversion from string constant to char*
    By Albinoswordfish in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2008, 10:24 AM
  5. Replies: 7
    Last Post: 09-24-2008, 03:48 AM

Tags for this Thread