English 中文(简体)
正在发生什么? 四种法系中同一处的三种不同类型?
原标题:What is happening? Three different types for the same stuff in four lines of code?
  • 时间:2012-04-16 21:03:41
  •  标签:
  • c++
  • std

我从另一个零碎问题中借了这个问题,但我不赞成这个问题。 我更担心我根本不理解这一职能。 类型和数据寿命:

同一数据由以下几个方面构成:探测器(带有内部元数据的动态射线型)、该数据所载示意图器(回归参数)和申报的回归类型,后者是一个 st脚石。

QUESTIONS: How does the data get safely out of the function when the std::vector is going to be destroyed? Is it implicitly copied? Is the dynamic array of char in the vector detached from the vector and returned as a std::string type so that no bulk copy is required? Sometimes, I think that C++ and the std library is trying to get me...

I ve been using C++ for some while but stuff like this does my ed in.

std::string TestFragmentation()
{
    std::vector<char> buffer(500);
    SomeCApiFunction( &buffer[0], buffer.size() ); // Sets buffer to null-terminated string data
    return &buffer[0];
}
最佳回答

The data stored by a std::vector is guaranteed to be contiguous, so &buffer[0] gets you a raw pointer to the beginning of that data.1

<代码>std:string has a Constructionor which take a const char *, which texts the data. 收益报表中含蓄地提到这一点(允许汇编者在大多数暗中转换作业中打电话,以避免汇编时间错误)。

In both cases (the vector and the string), the memory for the corresponding backing buffer is managed by the container class, so there is no possibility of a memory leak or similar (so long as your raw C function creates a valid null-terminated C-style string and doesn t trample beyond the buffer bounds).


页: 1 然而,注意到没有保证它会停留在一个地方。 当你生长或缩小病媒时,它有可能在其他地方复制,以备记忆,使在病媒中发现的所有原始点失效。 如果是<条码>, 本身被拆解,当然,背后的数据不再有效。

问题回答

暂无回答




相关问题
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?

热门标签