std::cin>> *aa 导致总线错误

发布于 2024-08-29 19:52:34 字数 1278 浏览 18 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽梦紫曦~ 2024-09-05 19:52:34

带有 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 to char*. It has not been allocated. If you change the declaration of aa to char 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.

网白 2024-09-05 19:52:34

当你说:

char *aa[1000];
std::cin >> *aa;

*aa 没有分配内存时。同样的问题在这里:

char *stringValue[];

并且名称 __CPP_PPString 在 C++ 中是保留的,所有包含双下划线或以下划线和大写字母开头的名称也是如此。您不能在自己的代码中创建它们,

When you say:

char *aa[1000];
std::cin >> *aa;

*aa has no memory allocated to it. Same sort of problem here:

char *stringValue[];

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,

眉黛浅 2024-09-05 19:52:34

char *aa[1000]; 并不是你想象的那样。它是一个由 1000 个 char * 组成的数组。

使用 std::string 代替。这样,您就不必担心有人输入超过 1000 个字符并利用您的程序。

例如

std::string input;
std::cin >> input;

char *aa[1000]; is not what you think it is. It's an array of 1000 char *'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.

std::string input;
std::cin >> input;
巾帼英雄 2024-09-05 19:52:34

你使用什么编译器?尝试使用不同的编译器或启用您正在使用的编译器的所有警告,编译器应该告诉您此代码的错误,您不需要在运行时找出。

例如,科莫在线会告诉你:

"ComeauTest.c", line 4: error: incomplete type is not allowed
      char *stringValue[];
            ^

"ComeauTest.c", line 23: warning: variable "aa" is used before its value is set
  std::cin >> *aa;

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:

"ComeauTest.c", line 4: error: incomplete type is not allowed
      char *stringValue[];
            ^

"ComeauTest.c", line 23: warning: variable "aa" is used before its value is set
  std::cin >> *aa;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文