Adding char to string produces crash in C++ - c++

I have this public function inside a class and what I am doing is appending a string to another string char by char (because I'm also making some checking on the characters). So, it basically looks like this:
void mystring::copy(wstring oldtext) {
wstring newtext;
for (unsigned int i=0; i<oldtext.length(); ++i) {
// Doing some checking ...
// I also used newtext.append(1, oldtext[i]);
newtext += oldtext[i];
// Just testing
wprintf_s(L"String: %s\n", newtext);
}
}
So, if I comment the wprintf_s line the program doesn't crash, but if I want to test and see what is added to newtext by using it, it crashes. I tried to add "\0\n" but it doesn't work either and it smells like some pointer problem. What could it be?

I guess you should use newtext.c_str() to convert it to a normal C string when using it with %s type specifier inside a printf.

Related

How to use a variable in the same struct it's defined in?

I am making a rogue-like ASCII game and made a struct called "Armor" and I want to use the name variable in the struct to have the path to whatever the name is.
struct Armor {
bool equipped;
std::string name;
int getBuff(int buff) {
std::fstream item;
std::string line;
std::string response;
std::string value;
item.open("../Data/Items/" + name + ".item", std::fstream::in);
if (item.fail())
errorQuit("ERROR: There was a problem loading armor type .ITEM file."); // Error and quit function
while (!item.eof()) {
getline(item, line);
response = split(line, '=', 0); // Splits string
if (response == "buff" + std::to_string(buff)) {
value = split(line, '=', 1);
break;
}
}
item.close();
return std::stoi(value);
}
};
Then I called it like this:
Armor sword;
sword.name = "Wooden Sword";
int buff = sword.getBuff(1);
But this throws an Unhandled exception error.
I changed it so that getBuff takes 2 parameters, int buff and std::string itemName. and replaced name in the path with itemName;
Then I tried calling it like this:
Armor sword;
sword.name = "Wooden Sword";
int buff = sword.getBuff(1, sword.name);
But this throws the same error.
I'm confused as to why I can't use the name variable as it has already be defined. Is there any other way I can use the name variable like that?
I see you've just edited your comment to say you've figured your problem out, but I just want to add something else that may be helpful:
Without seeing how errorQuit() is defined, there's a potential problem in your getBuff() function. If the expression if (item.fail()) evaluates to true, the function may continue on trying to process the data (unless errorQuit() somehow breaks out of the program or something, which probably isn't the best approach).
Basically, testing for fail() may or may not provide the behavior you require in all scenarios, depending on what bits are set in the stream state. Implementations vary, but... if the file fails to open, failbit and/or badbit will be set, but not eofbit. getline() will see the error state and so it will not try to read from the stream when you call it. But that also means the eofbit will never be set!
There's lots of different "techniques" to file reading. Some people prefer an RAII approach. Others like looping on getline(). Or you could even just use good() to check the error state if you don't care what happened and simply want to know if everything is fine or not.
In any case, you might be interested in the info on this page: std::ios_base::iostate.
Thanks for all your help but I figured it out on my own.
I just made a stupid error that I overlooked like an idiot.
It is searching for buff + int (e.x. buff1) in the file but there are multiple lines that contain that word so I guessed that messed it up. I just made an adjustment to the if statement and it is working as expected.
Sorry to bother you!
your getBuf() function fails on some io-operation and throws an exception.You dont handle exceptions and thus the application quits with the appropriate message. Try surrounding the call to getBuf with try/catch (add the includes to iostream and stdexcept)
try {
int buff = sword.getBuff(1);
}
catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}

not able to return QString from c++ function to PERL function by using SWIG

I have c++ code (Includes Qt also), i want to call those functions by using Perl.
Which we can do by using SWIG, so I have implemented interfaces and did all the stuff need to use them in Perl script.
I have a function in c++ which returns a QString value,
QString get_string()
{
return QString("mystring");
}
I have written one more class which will be used in perl script where i have a function which calls this get_string() function and returns const char*.
const char* get_const_string()
{
QString str = get_string();
**//here I print str and str .toLocal8Bit().constData()
//both are printing the text which i shoud get here**
return str.toLocal8Bit().constData();
//here I have tried diff combinations also, as
// return str.toStdString().c_str();
}
The problem is, in get_const_string() function, I could get the string I wanted, but when I call this function in my perl script, I am getting undefine value i.e null string
.
Any idea, what is the problem here ??
I am using perl5, Qt4.8.4
Thanks in advance.
if you cant use a QString return value, maybe you can use std::string.
if both fail and you do not have limitations, you could do some dirty trick:
QString get_string()
{
static QByteArray arr;
QString str = getString();
arr = str.toLocal8Bit();
return arr.constData();
}
note that the arr variable will not be free'd untill your app is running
edit: found a possible solution to just use std::string ... string arguments are not recognized by SWIG

C/C++ string memory allocation

I've just recently begun with C/C++ development on an embedded system (ARM - STM32F4 to be more specific) and I am now having almost classical problems of someone who isn't used to C or low level memory managment.
Basically I have a class MenuOption that inherits some field from some other class and basically looks like this:
...
char text[20];
...
void MenuOption::setText(const char* text1)
{
clearCurrent();
needsUpdate = true;
strncpy(text, text1, 20);
width = font->FontWidth*strlen(text);
}
The constructor for this class calls that setText method to store the text. This works fine if I use it like this inside a main function:
std::vector<MenuOption *> mainMenuOptions;
MenuOption* op1 = new MenuOption(13, 15, "Info", WHITE, BLACK);
op1->setSelected(true);
mainMenuOptions.push_back(op1);
But it fails when I want to use it like this:
std::vector<MenuOption *> options;
for (int i = 0; i < things.size(); i++)
{
Thing *th = things[i];
... do some stuff with th ...
MenuOption* op = new MenuOption(190, 38+25*i, "test", WHITE, BLACK);
options.push_back(op);
}
This fails (debugger sort of stalls) at the MenuOption* op ... line. Now I am guessing that this isn't something that I should be doing. But I can't seem to find a working solution.
EDIT:
To answer everyones questions. This does in fact compile with a C++ compiler. GCC using C++11 dialect.
There is a reason why I am using C strings instead of std::string. I am using a few C libraries that need C strings. And whenever I tried to convert that string into a C string inside a FreeRTOS task the thing would fail. The same problem as now actually.
No other breakpoint inside the task will trigger after it reaches that constructor line. I can't step in or skip a line or anything like that at that line. I have a feeling it gets caught by a hard_fault interrupt handler. Other tasks would continue to run. That's the problem. There are no errors or anything that would point me to the cause. The same problem was when I was using std::string when I tried to create a new MenuOption inside a FreeRTOS task. The thing works if I remove the string from the constructor. So I am guessing that it has something to do with strings.
As for string length. I know that strings used here will not be longer than 15 characters. I used those 5 characters for pure "backup"-
As for the ... do some stuff with th ... that was just this: th->flag = true;. I didn't do anything more with it because of this problem.
The tip posted in the comments by Étienne was actually what lead me to the answer. More specifically I found this: http://www.freertos.org/FreeRTOS_Support_Forum_Archive/October_2013/freertos_Using_C_std_vector_in_task_93928e86j.html
void *operator new(size_t size)
{
void *p;
if(uxTaskGetNumberOfTasks())
p=pvPortMalloc(size);
else
p=malloc(size);
return p;
}
void operator delete(void *p)
{
if(uxTaskGetNumberOfTasks())
vPortFree( p );
else
free( p );
p = NULL;
}

How to avoid deprecated conversion from string constant to 'char*' in C++

I would like to call the following code in C++, which I cannot change:
void getAge(char *name)
{
// do something
}
When I call it with getAge("hello");, it has the following warning:
warning: deprecated conversion from string constant to 'char*'
but there is no warning in C code. What is the difference, and how do I change the call to avoid the warning in C++?
the function […] can not be changed
Then write a wrapper around the function and copy the string – or, if you feel lucky (= you know that the string won’t be modified inside the original function), explicitly cast away const-ness:
void getAge(char const* name) {
the_namespace::getAge(const_cast<char*>(name));
}
If you’re unsure whether the function modifies its parameters, use something like the following – however, if that’s the case then calling the function with a string literal (getAge("hello")) would have been invalid anyway.
void getAge(char const* name) {
std::string buffer(name);
the_namespace::getAge(&buffer[0]);
}
Here we copy the string into a modifiable buffer and pass an address to its first character to the original function.
The safest way is to copy the string, then call the C function:
void getAgeSafe(const char* name)
{
std::vector<char> tmp = name?
std::vector<char>(name, name+1+strlen(name))
:std::vector<char>();
getAge( tmp.data() );
}
and call getAgeSafe from your C++ code.
A less safe way that relies on the C code never modifying the char* name would be to const_cast, again in a "wrapping" function:
void getAgeUnsafe(const char* name)
{
getAge( const_cast<char*>(name) );
}
but this time the name is more scary, as is the operation. If you call getAge with a compile time constant string like "bob", if getAge modifies its input, undefined behavior results (this is true in both C and C++ -- C++ at least warns you about it).
You can try getAge((char*)"hello").
In c++ you can write it like this,
void getAge(string name)
{
// do something
}
and also include the header file #include<string> because you are using string now

Core dump error while returning from a function

I have a function:
void function(const string param1, string *p2param, string *retparam)
when this is invoked from main, execution goes till last line of this function and then fails with
Bus Error(coredump)
The function performs some string manipulation using pointer to string and then the final value is passed to *retparam.
The code goes like this aa.c has
string *f1;//global
string f2= "abc";//global
function_2()
{
stringstream aa;
*f1 += aa<<"test";
//similar concatenation
}
function(param1, *p2param, *retparam)
{
/* assign back the values*/
f1 =&f2;
//call to a function from bb.c
// from bb.c function_2() is in called
retparam = f1
}
The only information I could get is:
pstack core
$ pstack core
core 'core' of 4517: aa_test -t 745
ffffffff7c67109c __1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___2T5B6M_v_ (ffffffff7fffce98, 1002805fc, 10010cc90, 0, ffffffff7c8c3bd8, ffffffff7fffce98) + 14
0000000100004498 main (10010b000, 100000, ffffffff7fffce98, ffffffff7fffcf00, ffffffff7fffd288, ffffffff7fffd0b8) + 818
0000000100003a7c _start (0, 0, 0, 0, 0, 0) + 17c
What is causing this error?
Thanks for your valueable inputs which finally has sorted out my issue.
The issue was with the typecast of string variable
code snippet
void function(const string param1, string *p2param, string *retparam) {
//function to call from bb.c has prototype
//fun2(const char **str,stubfunc)
const char *l_str = param1.c_str();
fun2((const char **) &l_str,coverage_hook);
}
//this was ealier called as
//fun2((const char**) &param1,coverage_hook); hence was causing the core dump
//why?? still dont know :)
If retparam is the address of an actual string when you pass it in, then what you really want to do before returning is
(*retparam) = f2;
Setting the value of retparam itself isn't doing you any good, since it's a local variable in the function, and changing its value won't change anything in the parent. But you can change the memory it points to, which is what happens here.
You are returning a pointer to f2 which is a local variable that gets destroyed when the function returns.
I'm taking a bit of a stab in the dark here, but I think your problem might be that you're defining the function like this:
void function(const string param1, string *p2param, string *retparam)
{
.
.
.
}
but you're forward-declaring it somewhere (in a header file?) like this:
string function(const string param1, string *p2param, string *retparam);
(promising that it will have return-type string rather than void). So when the function returns, the calling code tries to use its return-value, and pandemonium ensues when it turns out there isn't one.
(If that's not the case — and it may well not be — then I think it would help if you posted the full function definition, as well as the code that invokes the function.)