Board index » cppbuilder » Re: Question about const and pointers

Re: Question about const and pointers


2005-07-08 05:29:16 AM
cppbuilder61
"Randall Parker" < XXXX@XXXXX.COM >wrote in
message news:42cd8200$ XXXX@XXXXX.COM ...
Quote
But doesn't that cause a copy of the whole object to be placed on the
stack?
Do you mean a copy of the string? If you do, then AFAIK it does not.
AnsiString stores the string on the heap.
Jonathan
 
 

Re:Re: Question about const and pointers

Randall Parker < XXXX@XXXXX.COM >writes:
Quote
>And const AnsiString myclass::GetString(); ?

But doesn't that cause a copy of the whole object to be placed on
the stack?
Yes, but it's a lightweight operation since AnsiString is reference
counted. Copying an AnsiString copies a single 4-byte pointer and
then adjusts the object's refcount. It does not perform any
allocations nor deep copying.
--
Chris (TeamB);
 

Re:Re: Question about const and pointers

Chris Uzdavinis (TeamB) wrote:
Quote

>But I'm not sure about it. On the other hand I noted that the
>string class (basic_string) began to behave strangely, so that I had
>to make up my own functions for assigning and appending to get this
>done properly.

Do you have any examples? What do you mean by "began to behave
strangely"?
E.g. I want to produce an error message after catching an exception
including information about, where and why the exception occurred:
catch( ... )
{
string messtring;
messtring = "An undefined Error occured in the Function
StoichRatio, while processing/n";
messtring += string("Phase ")
+ owner->Name // is of type string
+ string(" Component ")
+ Name; // is also of type string
throw cs_error( messtring ); // go up to the next exeption
// handling level for display
}
The first assignment of messtring is executed, but the following calls
of the operators "+=" and "+" are partially ignored, that is, only the
first one will process correctly, all subsequent calls won't add
anything to messtring.
The same happens when changing the code to
catch( ... )
{
string messtring;
messtring = "An undefined Error occured in the Function
StoichRatio, while processing/n";
messtring += string("Phase ");
messtring += owner->Name;
messtring += string(" Component ");
messtring += Name;
throw cs_error( messtring );
}
or to
catch( ... )
{
string messtring;
messtring = "An undefined Error occured in the Function
StoichRatio, while processing/n";
messtring.append("Phase ");
messtring.append(owner->Name);
messtring.append(" Component ");
messtring.append( Name );
throw cs_error( messtring );
}
Again, only the first call to 'append' will perform correctly, all
subsequent calls fail.
I wrote a function 'AppendString' to perform appendage by "manually"
concatenating the strings together:
// For appending a printf-formatted string:
string &AppendString( string &str1, const char *fmt, ... )
{
va_list ap;
char buf[500];
char *tmp;
// int ret;
va_start(ap, fmt);
vsprintf(buf, fmt, ap );
va_end(ap);
tmp = new char[ str1.size() + strlen(buf) + 1 ];
strcpy( tmp, str1.c_str() );
strcat( tmp, buf );
str1 = tmp;
delete[] tmp;
return str1;
};
// For appending a second basic_string:
string &AppendString( string &str1, string str2 )
{
char *tmp;
tmp = new char[ str1.size() + str2.size() + 1 ];
strcpy( tmp, str1.c_str() );
strcat( tmp, str2.c_str() );
str1 = tmp;
delete[] tmp;
return str1;
};
Then this catch works without problems:
catch( ... )
{
string messtring;
messtring = "An undefined Error occured in the Function
StoichRatio, while processing/n";
AppendString(messtring,"Phase ");
AppendString(messtring,owner->Name);
AppendString(messtring," Component ");
AppendString(messtring, Name );
throw cs_error( messtring );
}
There are other examples like this, not only in catch blocks. Strange as
it is, sometimes the normal way works, and sometimes it doesn't. When it
doesn't, it might produce some error, or in other cases you can only see
it in the output data or in the log-file, because something is missing.
Anyway, when using my auxiliary functions, I have no trouble at all.
Quote

>I tracked the misbehaviour down to the basic 'replace' routine, but
>this doesn't exist as source code, so I'm not able to analyse what
>really happens and what I've done wrong to produce this
>misbehaviour. So some better understanding of string types might
>help to solve that riddle.


I'm curious what you're seeing.
Have a look into string.stl: The operators "+" and "+=" of basic_string
simply call basic_string::append, which in turn, as assign, insert and
erase, calls versions of replace, which in turn calls a version of
replace which is prototyped in the private section of the class, but is
not source coded in string.stl. So there stops any try to check
variables for their content and changes of it.
I have the feeling that there is some kind of memory corruption at work,
but I still cannot put my finger on it. As I said, I must be doing
something at some place in the program which causes the string functions
not to execute correctly but to face some error which let's them exit
prematurely without result.
Best regards
Matthias
 

{smallsort}