views:

96

answers:

4

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...

+1  A: 

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,

anon
+1  A: 

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;
Alex
The problem of this, is that soon I'll not link to the standard library anymore. The classes are actually in my own static library. Maybe I could use std::string as buffer for now. Thanks anyway.
Time Machine
@Koning you are rewriting standard lib?
Kugel
@Kugel, not really. I'm writing a program which links to my static-library to test it. That static-library indeed doesn't link to anything.
Time Machine
+3  A: 

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.

Mark Wilkins
Awesome! Thanks!
Time Machine
...until someone enters 1005 characters.
Alex
Time Machine
A: 

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;
JRL
I'm using GCC. Switching to another one is a real pain with Xcode...
Time Machine