pdCURSES 和 addstr 与字符串的兼容性问题
嘿,所以我试图让 pdCurses 中的 addstr() 与首选字符串类一起工作(Windows Curses),所以我将该函数设为以下 string_to_80char() 函数,该函数应该接受一个字符串并返回一个 80 个字符长的 char大批 (字符数适合控制台中的一行),因为这是 addstr 似乎接受的唯一参数...
但是,当运行以下代码时,我确实得到了打印的“只是一个字符串”,但带有一个随机字符,例如 ' @' 或 '4' 就像后面有 50 个空格......
有什么问题吗?感谢您的帮助! =)
#include <curses.h> /* ncurses.h includes stdio.h */
#include <string>
#include <vector>
#include <Windows.h>
#include <iostream>
using namespace std;
char* string_to_80char (const string& aString)
{
int stringSize = aString.size();
char charArray[90];
if(stringSize <= 80)
{
for(int I = 0; I< stringSize; I++)
charArray[I] = aString[I];
for(int I = stringSize; I < sizeof(charArray); I++)
charArray [I] = ' ';
return charArray;
}
else
{
char error[] = {"STRING TOO LONG"};
return error;
}
};
int main()
{
// A bunch of Curses API set up:
WINDOW *wnd;
wnd = initscr(); // curses call to initialize window and curses mode
cbreak(); // curses call to set no waiting for Enter key
noecho(); // curses call to set no echoing
std::string mesg[]= {"Just a string"}; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
clear(); // curses call to clear screen, send cursor to position (0,0)
string test = string_to_80char(mesg[0]);
char* test2 = string_to_80char(mesg[0]);
int test3 = test.size();
int test4 = test.length();
int test5 = sizeof(test2);
int test6 = sizeof(test);
addstr(string_to_80char(mesg[0]));
refresh();
getch();
cout << endl << "Try resizing your window(if possible) and then run this program again";
system("PAUSE");
refresh();
system("PAUSE");
endwin();
return 0;
}
Hey so i'm trying to get addstr() in pdCurses to work (windows curses) with the preferred string class so I made the function the following string_to_80char() function, which is supposed to take a string and return an 80 character long char array
(the number of characters fit on one line in the console) since this is the only parameter addstr seems to accept...
However when running the following code i do get the "Just a string" printed but with a random character like an '@' or '4' like 50 spaces after it.....
WHATS THE PROBLEM?? Thanks for the help! =)
#include <curses.h> /* ncurses.h includes stdio.h */
#include <string>
#include <vector>
#include <Windows.h>
#include <iostream>
using namespace std;
char* string_to_80char (const string& aString)
{
int stringSize = aString.size();
char charArray[90];
if(stringSize <= 80)
{
for(int I = 0; I< stringSize; I++)
charArray[I] = aString[I];
for(int I = stringSize; I < sizeof(charArray); I++)
charArray [I] = ' ';
return charArray;
}
else
{
char error[] = {"STRING TOO LONG"};
return error;
}
};
int main()
{
// A bunch of Curses API set up:
WINDOW *wnd;
wnd = initscr(); // curses call to initialize window and curses mode
cbreak(); // curses call to set no waiting for Enter key
noecho(); // curses call to set no echoing
std::string mesg[]= {"Just a string"}; /* message to be appeared on the screen */
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
clear(); // curses call to clear screen, send cursor to position (0,0)
string test = string_to_80char(mesg[0]);
char* test2 = string_to_80char(mesg[0]);
int test3 = test.size();
int test4 = test.length();
int test5 = sizeof(test2);
int test6 = sizeof(test);
addstr(string_to_80char(mesg[0]));
refresh();
getch();
cout << endl << "Try resizing your window(if possible) and then run this program again";
system("PAUSE");
refresh();
system("PAUSE");
endwin();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 string_to_80char() 返回一个指向局部变量的指针,并且当函数返回时该变量的生命周期就结束了,因此指针指向垃圾。另外,您没有在返回的字符串末尾放置
'\0'
字符(但除此之外,您返回的内容并不正式存在)。让调用者提供缓冲区以将 80
char
字符串放入其中(未经测试的示例):或者,在堆上分配返回的缓冲区并返回该缓冲区(在这种情况下,调用者必须在调用时释放该缓冲区)重新完成它)。
如果您使用的是 Windows 并且没有
strdup()
,请执行以下操作:Your
string_to_80char()
is returning a pointer to a local variable and the lifetime of that variable is over when the function returns so the pointer is pointing to garbage. Also, you're not putting a'\0'
character at the end of your returned string (but that's besides the point that what you're returning doesn't officially exist anyway).Have the caller provide the buffer to put the 80
char
string into (untested example):Alternatively, allocate the returned buffer on the heap and return that (in which case the caller must free the buffer when they're done with it).
If you're on Windows and don't have
strdup()
, here you go:一个问题是您正在返回一个指向 string_to_80char() 中存储在堆栈上的变量的指针。该变量存储在堆栈中:
当您从该函数返回时,该变量使用的存储不再有效,并且可能会被重用。 addstr() 的堆栈变量很可能会覆盖相同的存储,因此您的字符串被损坏。
一个简单的修复方法是将 charArray 设为静态,这样它就不会在堆栈上分配:
One problem is that you are returning a pointer to a variable stored on the stack in string_to_80char(). This variable is stored on the stack:
When you return from that function, the storage used by this variable is no longer valid, and its likely to be reused. It's likely that addstr()'s stack variables are overwriting this same storage so your string is being corrupted.
A simple fix is to make charArray static so it's not allocated on the stack:
应该就是你所需要的。 PDCurses 是一个 C 库,因此它需要 C 字符串。它们不必是 80 列或任何其他特殊的东西。
或者,创建一个简单的 C++ 包装函数:
should be all you need. PDCurses is a C library, so it takes C strings. They don't have to be 80 columns or anything else special.
Alternatively, make a simple C++ wrapper function: