0

I initialize a pointer to string type and allocate some memory with malloc function. My problem is when i try to use one of the string pointed by this pointer, i get segmentation fault.

string anyString = "anyWords";
string *pointerToString;
pointerToString = (string *) malloc(sizeof(string) * 5);
pointerToString[i] = anyString; // this line causes segmentation fault

Thanks in advance for any help.

3
  • What's the value of 'i'? Commented Dec 21, 2010 at 14:55
  • 2
    You should throw away whatever source taught you to use malloc here. Commented Dec 21, 2010 at 15:05
  • Or drop the ever quirky C++ for a real language ;). Just kidding. Commented Dec 21, 2010 at 15:09

4 Answers 4

5
std::vector<std::string> strings(5);

This is what you actually want.

Sign up to request clarification or add additional context in comments.

Comments

4

Avoid using malloc with objects in C++.

Instead, use new:

std::string * str = new std::string("Hello");
// ...
delete str;

For an array:

std::string * tab = new std::string[5];
// ...
delete[] tab; // if you allocated with new[], release with delete[]

The reason it's failing in this case is that malloc, unlike new, does not call the class' constructor. Since std::string allocates memory on its own, then you get an invalid object in the end.

So, new/delete (or new[]/delete[]) is the way to go here. You can still use malloc if you want, but only for "POD" type (primitive types, old style structs, etc.). And if you allocate with malloc, release with free. Don't mix up new and free or malloc and delete (there's an exception to this rule: see Mehrdad Afshari's comment below).

4 Comments

You could technically also use malloc and call the constructor directly. But this would be bad, bad, bad.
@John Yes, but the only good reason I can think off is to allocate a large memory buffer to initialise later, and we got placement new for that.
Avoid allocating dynamic arrays in C++; use vector.
Regarding the last paragraph, there's one subtle exception: you can allocate memory with malloc and use placement new to initialize a class in that memory location. Of course, this technique should be reserved only for special circumstances. For instance, adding new(pointerToString+i) string; before the last line will call the constructor for the string class on that memory location. Note that if you do this, you should call destructor manually: pointerToString[i].~string(); and then use free (if you allocated with malloc).
0

As Etienne said, you should have used new as malloc only allocates memory but does not call constructor which initializes that memory. std::string is a class and when you want to create instance of it on the heap, you must use new, just like for any other class type. You tried to write to a wrong address and therefore your segmentation fault.

You want to create array of strings. In C, you will use array, but in C++ use std::vector.

Comments

0

You probably do not want to be doing it that way but if you are, i.e. using malloc to allocate the memory, then you need to use placement-new.

Actually, this will do placement-new for you.

std::vector< std::string > vec;
vec.reserve( 5 );
vec.resize( 5 );

The call to reserve() allocates the memory for you and then resize() actually creates the objects there in the memory already allocated, with placement-new.

Placement new in the C++ FAQ is documented well here. Read it to learn. Then probably simply use vector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.