CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009

    Is it possible to tell if a string is a literal or not?

    I have to send an array of strings into a shared library, some of the strings are constants, some are generated. The array is put together in different parts of the program, so using c_str on a std::string is not possible as they go out of scope, so I have a method that is places the strings in the array, using strdup. What I want to know, is there a way to tell what is a string literal and what is a generated string? This library interface is a chokepoint, so I want to get through it as quickly as possible.

    Something like

    void addToArray(const char * str, const char ** array, size_t top){
       if (is_literal(str)){
          array[++top] = str;
       }  else {
          array[++top] = strdup(str);
    void purgeArray(const char ** array, size_t top){
       for(size_t i = 0; i < top; ++i){
          if (!is_literal(array[i])){
    Anything like that, or am I pipe dreaming?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Seattle, WA

    Re: Is it possible to tell if a string is a literal or not?

    I doubt it. One thing you might consider is adding a parameter to specify whether or not the addToArray function should copy or merely reference the passed string, and internally maintain a bool array indicating which is the case for each entry. This is what OpenCV does for matrix internals, for instance.

  3. #3
    Join Date
    Jun 2009

    Re: Is it possible to tell if a string is a literal or not?

    Quote Originally Posted by ninja9578 View Post
    Anything like that, or am I pipe dreaming?
    I'd say you're dreaming

    Plus, to be honest, what you are trying to do sounds very dangerous. Objects should either have ownership of all their objects, or none of them. Mixing both will guarantee you leaks/double delete. (IMO)

    Can't you just systematically create a copy taking ownership? If space is tight, and if your strings lengths are constant, you can always take the "One Huge Array" approach to store all your strings, with a secondary array storing the pointers to where they begin.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Oct 2008

    Re: Is it possible to tell if a string is a literal or not?

    ninja, take a look at this thread; if you read it carefully, you'll find some tentative solutions to your problem with the associated caveats; in particular, you can consider the template based ( template <std::size_t size >f(const char (&str)[size]) ) or the macro based (the ""-"" trick ) solutions.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.