CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Posts
    7

    Is it bad coding practice to use #defines for string literals ?

    During a recent code review it was pointed out to me that it is bad to use #define for string literals because :

    while making your debug build your compiler makes a separate copy of the string for each use, in your exe, while in your release build it ptimizes this to make just one string

    Now if you are passing your string to a function as an argument and that function modifies it, you
    are safe in your debug build becoz each usage is a new string however, in your release build you
    are dead cause the subsequent uses of that string points to modified/corrupt data

    I have therefore been advised to use a global array of const char*s.

    Any opinions/alternative solutions will be welcome

    Thanx

    sanjay



  2. #2
    Join Date
    Apr 1999
    Posts
    90

    Re: Is it bad coding practice to use #defines for string literals ?

    Is this a VC++ project?

    If so, put string literals in the string table. This provides for a centralized location to modify strings in the future and is easier for possible localization efforts.

    If not, then 'const char *' provides a better solution for type checking anyway.



  3. #3
    Join Date
    Apr 1999
    Posts
    20

    Re: Is it bad coding practice to use #defines for string literals ?

    Why in the world would you be modifying a string define???

    In terms of internationalization, you would want to put strings into the string table so that you can provide localized resources (putting the strings into CStrings in your code via LoadString() ). So, for me that would be the biggest reason NOT to put them in defines.

    For stuff like Registry key information, it isn't a problem since you don't really want that stuff translated anyways.

    Global strings just sound icky to me in general


  4. #4
    Join Date
    May 1999
    Posts
    19

    Re: Is it bad coding practice to use #defines for string literals ?

    For advice on this question on many like it, see Effective C++, by Scott Meyers. See page 10 for this. He recommends const and inlines instead of #defines.


  5. #5
    Join Date
    Apr 1999
    Posts
    383

    Re: Is it bad coding practice to use #defines for string literals ?

    > while making your debug build your compiler makes a separate copy of the
    > string for each use, in your exe, while in your release build it ptimizes
    > this to make just one string

    Whether this happens or not is compiler implementation dependent. There is no requirement for compilers to do this. Some compilers make a configurable option.

    > Now if you are passing your string to a function as an argument and that
    > function modifies it, you are safe in your debug build becoz each usage is a
    > new string however, in your release build you are dead cause the subsequent
    > uses of that string points to modified/corrupt data

    In C++ it is totally unsafe either way. The result of attempting to modify a string literal is undefined (meaning it is not a valid operation). Doing this is *wrong*, and IMO the compiler should give an error.

    > I have therefore been advised to use a global array of const char*s.

    In C++ the use of global variables is considered very poor practice. Why use char*s when you can use string objects, why use either for literal strings if a string table resource is available?

    Dave


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured