kiki
kiki

Reputation: 215

C++ cin , Segment fault 11

This gives me seg fault 11 when I input the string. Why am i getting the seg fault? It's so simple... where is the seg fault coming from?

int main(){
    char * str;
    printf ("please enter string : ");
    cin >> str;
    // reverse_str(str);   
}

Upvotes: 3

Views: 3454

Answers (7)

Alok Save
Alok Save

Reputation: 206646

You have not allocated any memory to str. So you are trying to essentially write to a pointer which cannot hold the string. Essentially this leads to an Undefined Behavior and a seg fault.

The solution is:
You should use std::string instead of pointer.

std::string str;
std::cout<<"please enter string :  ";
std::cin >>str;

Also, try not to mix C and C++.
Use streams in C++ not printf


Alternatively, there are 2 other approaches:

Not so good other Approach 1:
You can allocate memory to str by making it an fixed size array:

#define MAX_INPUT 256
char str[MAX_INPUT]={0};

Drawback:
This would require that you need to know the length of the maximum input that user can enter at compile time, Since Variable Length Arrays are not allowed in C++.

Not so good other Approach 2:
You could allocate memory dynamically to str using new [].str will be a pointer in this case.

#define MAX_INPUT 256
char *str = new char[MAX_INPUT];

Drawback:
Again this approach has the drawback of knowing how much memory to allocate at compile time in this case,since user inputs the string. Also, You need to remember to deallocate by calling delete[] or you leak memory. Also, try to avoid using new as much as possible in C++.

Conclusion:
Best solution here is to use std::string because it saves you from all above problems.

Upvotes: 5

Rob Kennedy
Rob Kennedy

Reputation: 163357

Segmentation faults occur when a program accesses memory that doesn't belong to it. The pointer str is not initialized. It doesn't point at any memory. The stream-extraction operator >> doesn't allocate new memory for the string it reads; it expects the pointer to already point to a buffer that it can fill. It doesn't know that str is garbage, though. It assumes it's a valid pointer and attempts to write into that memory. Since the memory at that garbage address doesn't belong to your program, the OS halts it with a segmentation fault.

Read into a std::string instead. Better yet, use std::getline so the user can enter a string with spaces; the >> operator will only read up to the first whitespace character.

std::string str;
std::getline(cin, str);

Upvotes: 0

Andrey Atapin
Andrey Atapin

Reputation: 7955

You need to allocate memory first for str. You declared only a pointer

char* str = (char*)malloc(MAX_SIZE_OF_BUFFER);

Upvotes: 0

Michael Kristofik
Michael Kristofik

Reputation: 35218

Your first line, char * str declares a raw pointer and doesn't initialize it or allocate any memory. That means it could point anywhere, and most likely not somewhere valid or useful. You most likely segfault when you try to use it on the 3rd line. Can you use std::string instead? It'll be much easier and safer than raw C-strings.

Upvotes: 1

Griwes
Griwes

Reputation: 9059

Use std::string.

Or, if you just want to use char * (discouraged):

char * str = new char[256];
cin >> str;

Upvotes: 0

Cat Plus Plus
Cat Plus Plus

Reputation: 130004

str doesn't point to anything sensible. You need to have a memory to write to.

std::string str;
std::cout << "please enter string: ";
std::cin >> str;

Upvotes: 3

slaphappy
slaphappy

Reputation: 6999

You should use std::string instead.

Your declaration:

char * str;

declares only a pointer, but does not allocate a memory region for the string to be stored. The solutions could have been

char str[256];

or

char * str = new char[256];

However, they are very bad style because you would then have to check and control the size of input each time you want to write to it.

C++ provides std::string to store strings. Use it.

Upvotes: 2

Related Questions