English 中文(简体)
C++ 显示类型分割过失的点
原标题:C++ pointer to string type segmentation fault
  • 时间:2010-12-21 14:52:57
  •  标签:
  • c++

我先设一个点,以显示类型,并用小型功能分配一些记忆。 我的问题是,在试图利用这个要点的一点时,即出现分化错误。

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

事先得到任何帮助。

最佳回答

避免使用C++中的物体 Smalloc

相反,使用<条码>新:

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

阵列:

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

在本案中,它之所以失败,是因为mallocnew/code>不同,并不叫上班建筑商。 自std:string>自发分配记忆以来,你最终就获得一个无效物体。

因此,new/delete(或[new]/code>/delete[>>)是到这里去的道路。 如果你想要的话,你仍然可以使用<代码>小型,但只能用于“POD”类(原始类型、旧风格结构等)。 如果你用<条码>小型分配,则以<条码>免费印发。 Don trange up new and free or malloc and delete>> (该规则的例外:见Mehrdad Afshari的评论如下)。

问题回答
std::vector<std::string> strings(5);

这是你实际想要的。

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.

你们想制造一系列扼杀。 在C,你将使用阵列,但在C++中则使用 st子:探测器。

也许你不想这样做,但如果你使用小鼠来分配记忆,那么你需要重新使用位置。

实际上,这将给你带来新的安置。

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

要求保留()为你们分配记忆,然后转播()实际上是在已分配的记忆中制造物体,重新定位。

C++ FAQ的新安置情况在此有详细记录。 阅读。 然后可能简单地使用病媒。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?