We help IT Professionals succeed at work.

assigning string

learningunix asked
Under heavy load system, can this every fail or assign empty.

std::string inStr;
printf ("Str = %s", inStr.c_str())

void func (std::string& outString)
     std::string localString ("TEST");
     outString = localString; // can this every fail under heavy load.

     // I mean is it possible that outString will be empty under any case?
Watch Question

Owner, Lead Technology Programmer
No - unless the assignment operator was written to intentionally not make the assignment under some circumstance, which I doubt is the case.

If it fails then it would have to be for another reason.  Memory overwrite?  Multi-threading issue?
Most Valuable Expert 2011
Top Expert 2015

Perhaps you have another naughty program that is not checking its bounds and overwriting memory in this program.



I am not sure what you mean by memory overwrite in this code?
Most Valuable Expert 2011
Top Expert 2015

No. In another program.
Bill NolanOwner, Lead Technology Programmer
A memory overwrite occurs when you've written to memory that has been reserved for something else.  E.g., if you allocate memory for a discrete block of data, such as:

char* pStuff = malloc(1024)

...the memory manager sets that aside for your block of chararacters.  Other critical data may be stored immediately following this reserved block.  If you then proceed to write 2,000 characters at that location (pStuff), you will overwrite anything stored just beyond its declared borders.  This is the most insidious of errors, and can cause all kinds of unpredictable behaviour, and in particular is a strong candidate for the kind of error you mention here.

Note: The memory overwrite needn't be in another program; in fact, if that is your problem, it is much more likely that the overwrite IS in the same program.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.