开发者

std::cin >> *aa results in a bus error

开发者 https://www.devze.com 2022-12-27 21:52 出处:网络
I have this a class called PPString: PPString.h #ifndef __CPP_PPString #define __CPP_PPString #include "PPObject.h"

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.pointerTo开发者_JS百科CharString() << 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...


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.


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,


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;


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号