std::cin>> *aa 导致总线错误
我有一个名为 PPString:
PPString.h
#ifndef __CPP_PPString
#define __CPP_PPString
#include "PPObject.h"
class PPString : public PPObject {
char *stringValue[];
public:
char *pointerToCharString();
void setCharString(char *charString[]);
void setCharString(const char charString[]);
};
#endif
PPString.cpp
#include "PPString.h"
char *PPString::pointerToCharString() {
return *stringValue;
}
void PPString::setCharString(char *charString[]) {
*stringValue = *charString;
}
void PPString::setCharString(const char charString[]) {
*stringValue = (char *)charString;
}
设置 stringValue
我正在尝试使用 std::cin
: main.cpp
PPString myString;
myString.setCharString("LOLZ");
std::cout << myString.pointerToCharString() << std::endl;
char *aa[1000];
std::cin >> *aa;
myString.setCharString(aa);
std::cout << myString.pointerToCharString() << std::endl;
第一个,其中使用 const char
有效,但第二个使用 char
则无效,我得到以下输出:
从 STDOUT 复制并粘贴,
LOLZ
im entering a string now...
Bus error
其中第二行是我的内容输入,然后按return
键。
谁能帮我解决这个问题吗?谢谢...
I have this a class called PPString:
PPString.h
#ifndef __CPP_PPString
#define __CPP_PPString
#include "PPObject.h"
class PPString : public PPObject {
char *stringValue[];
public:
char *pointerToCharString();
void setCharString(char *charString[]);
void setCharString(const char charString[]);
};
#endif
PPString.cpp
#include "PPString.h"
char *PPString::pointerToCharString() {
return *stringValue;
}
void PPString::setCharString(char *charString[]) {
*stringValue = *charString;
}
void PPString::setCharString(const char charString[]) {
*stringValue = (char *)charString;
}
I'm trying to set the stringValue
using std::cin
:
main.cpp
PPString myString;
myString.setCharString("LOLZ");
std::cout << myString.pointerToCharString() << std::endl;
char *aa[1000];
std::cin >> *aa;
myString.setCharString(aa);
std::cout << myString.pointerToCharString() << std::endl;
The first one, which uses a const char
works, but the second one, with a char
doesn't, and I get this output:
copy and paste from STDOUT
LOLZ
im entering a string now...
Bus error
where the second line is what I entered, followed by pressing the return
key.
Can anyone help me fixing this? Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
带有
char *s[]
签名的 setCharString 取消引用指向char*
的指针数组的第一个元素。它尚未被分配。如果将aa
的声明更改为char aa[1000];
,它可能会运行。还有其他一些问题(其他人也指出了)。对变量
stringValue
的赋值也会取消引用似乎尚未分配的内存。很难说它的用法是什么,但它可能不应该有[]
声明。此外,分配存储了一个指向堆栈内存的指针,该指针在另一个函数调用后可能会无效。The setCharString with the
char *s[]
signature is dereferencing the first element of an array of pointers tochar*
. It has not been allocated. If you change the declaration ofaa
tochar aa[1000];
, it will probably run.There are some other issues too (also pointed out by others). The assignment to the variable
stringValue
is also dereferencing memory that does not appear to have been allocated. It's hard to say what the usage is, but it should maybe not have the[]
declaration. In addition, the assignment is storing a pointer to stack memory, which will likely not be valid after another function call.当你说:
*aa 没有分配内存时。同样的问题在这里:
并且名称
__CPP_PPString
在 C++ 中是保留的,所有包含双下划线或以下划线和大写字母开头的名称也是如此。您不能在自己的代码中创建它们,When you say:
*aa has no memory allocated to it. Same sort of problem here:
And the name
__CPP_PPString
is reserved in C++, as are all names that contain a double underscore or begin with an underscore and an uppercase letter. You are not allowed to create them in your own code,char *aa[1000];
并不是你想象的那样。它是一个由 1000 个 char * 组成的数组。使用
std::string
代替。这样,您就不必担心有人输入超过 1000 个字符并利用您的程序。例如
char *aa[1000];
is not what you think it is. It's an array of 1000char *
's.Use
std::string
instead. That way, you don't have to worry about someone entering more than 1000 characters and exploiting your program.E.g.
你使用什么编译器?尝试使用不同的编译器或启用您正在使用的编译器的所有警告,编译器应该告诉您此代码的错误,您不需要在运行时找出。
例如,科莫在线会告诉你:
What compiler are you using? Try using a different compiler or enabling all warnings for the one you're using, the compiler should be telling you the errors for this code, you don't need to find out at runtime.
For example, Comeau online will tell you: