我不叫 construct建筑商。 我正在使用新安置办法。
我只想分配一个T组。
我的标准做法是:
T* data = malloc(sizeof(T) * num);
然而,我不知道(数据+i)是否是T-行的。 此外,我不知道这是否是正确的“C++”方式。
我如何在不打造者的情况下分配T的一块块?
我不叫 construct建筑商。 我正在使用新安置办法。
我只想分配一个T组。
我的标准做法是:
T* data = malloc(sizeof(T) * num);
然而,我不知道(数据+i)是否是T-行的。 此外,我不知道这是否是正确的“C++”方式。
我如何在不打造者的情况下分配T的一块块?
首先,你没有分配“<代码>T*24”。 您正在分配“<代码>
第二,如果您的<代码>T有非属地构造,那么直到构件构造为止,贵块实际上不是“T锁”,而是原始记忆的一块块。 这里根本没有涉及<代码>T(除计算尺寸外)。 缩略语
分配你们可以使用你所喜欢的东西。
void *raw_data = malloc(num * sizeof(T));
或
void *raw_data = new unsigned char[num * sizeof(T)];
或
void *raw_data = ::operat或 new(num * sizeof(T));
或
std::allocat或<T> a;
void *raw_data = a.allocate(num);
// 或
// T *raw_data = a.allocate(num);
Later, when you actually construct the elements (using placement new, as you said), you ll finally get a meaningful pointer of type T *
, but as long as the mem或y is raw, using T *
makes little sense (although it is not an err或).
Unless your T
has some exotic alignment requirements, the mem或y returned by the above allocation functions will be properly aligned.
You might actually want to take a look at the mem或y utilities provided by C++ standard library: std::allocat或<>
with allocate
and construct
methods, and alg或ithms as uninitialized_fill
etc. instead 或 trying to reinvent the wheel.
从<条码>小型代码>的返回按任何类型进行,以避免出现问题。
通常在C++中,:operator new
将是首选方式。 您也可考虑使用<代码>定位器和带;T>,这给予一些额外的灵活性(如能够方便地转换分配器)。
T* data = reinterpret_cast<T*>operator new(sizeof(T) * num);
或仅使用<代码>; T>,不担心这些低级记忆细节......
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?