Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ string manipulation

My lack of C++ experience, or rather my early learning in garbage collected languages is really stinging me at the moment and I have a problem working with strings in C++.

To make it very clear, using std::string or equlivents is not an option - this is char* 's all the way.

So: what I need to do is very simple and basically boils down to concatenating strings. At runtime I have 2 classes.

One class contains "type" information in the form of a base filename.

in the header:

char* mBaseName;

and later, in the .cpp it is loaded with info passed in from elsewhere.

mBaseName = attributes->BaseName;

The 2nd class provides version information in the form of a suffix to the base file name, it's a static class and implemented like this at present:

static const char* const suffixes[] = {"Version1", "Version", "Version3"}; //etc.

static char* GetSuffix()
{
    int i = 0;
    //perform checks on some data structures
    i = somevalue;
   return suffixes[i];
}

Then, at runtime the base class creates the filename it needs:

void LoadStuff()
{
    char* suffix = GetSuffix();
    char* nameToUse = new char[50];
    sprintf(nameToUse, "%s%s",mBaseName,suffix);

    LoadAndSetupData(nameToUse);
}

And you can see the problem immediately. nameToUse never gets deleted, memory leak.

The suffixes are a fixed list, but the basefilenames are arbitrary. The name that is created needs to persist beyond the end of "LoadStuff()" as it's not clear when if and how it is used subsequently.

I am probably worrying too much, or being very stupid, but similar code to LoadStuff() happens in other places too, so it needs solving. It's frustrating as I don't quite know enough about the way things work to see a safe and "un-hacky" solution. In C# I'd just write:

LoadAndSetupData(mBaseName + GetSuffix());

and wouldn't need to worry.

Any comments, suggestions, or advice much appreciated.

Update

The issue with the code I am calling LoadAndSetupData() is that, at some point it probably does copy the filename and keep it locally, but the actual instantiation is asynchranous, LoadAndSetupData actually puts things into a queue, and at that point at least, it expects that the string passed in still exists.

I do not control this code so I can't update it's function.

like image 844
xan Avatar asked Nov 10 '08 16:11

xan


People also ask

Is string modifiable in C?

Difference between character Array and String literal A string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz". String literals are not modifiable (and are placed in read-only memory).

What are string manipulations?

String Manipulation is a class of problems where a user is asked to process a given string and use/change its data. An example question would be a great way to understand the problems that are usually classified under this category.

How are strings handled in C?

C programming language provides a set of pre-defined functions called string handling functions to work with string values. The string handling functions are defined in a header file called string. h. Whenever we want to use any string handling function we must include the header file called string.


1 Answers

Seeing now that the issue is how to clean up the string that you created and passed to LoadAndSetUpData()

I am assuming that:

  1. LoadAndSetUpData() does not make its own copy
  2. You can't change LoadAndSetUpData() to do that
  3. You need the string to still exist for some time after LoadAndSetupData() returns

Here are suggestions:

  1. Can you make your own queue objects to be called? Are they guaranteed to be called after the ones that use your string. If so, create cleanup queue events with the same string that call delete[] on them

  2. Is there a maximum number you can count on. If you created a large array of strings, could you use them in a cycle and be assured that when you got back to the beginning, it would be ok to reuse that string

  3. Is there an amount of time you can count on? If so, register them for deletion somewhere and check that after some time.

The best thing would be for functions that take char* to take ownership or copy. Shared ownership is the hardest thing to do without reference counting or garbage collection.

like image 58
Lou Franco Avatar answered Sep 22 '22 01:09

Lou Franco