简体   繁体   English

此C ++代码是什么意思

[英]What does this C++ code mean

I was trying to understand how webkit parses urls, and I'm having a hard time making heads or tails of this: 我试图了解webkit如何解析url,而我却很难做到这一点:

Vector<char, 4096> buffer(fragmentEnd * 3 + 1);

This line is on line 1214 (you can see it here: http://trac.webkit.org/browser/trunk/WebCore/platform/KURL.cpp#L1214 ). 此行位于1214行(您可以在此处看到: http : //trac.webkit.org/browser/trunk/WebCore/platform/KURL.cpp#L1214 )。 I get that it's making a vector of type char, with each entry being an array of char 4096 bytes large, but I don't get the buffer(fragmentEnd * 3 + 1) part. 我知道它正在构成一个char类型的向量,每个条目都是一个4096字节大的char数组,但是我没有得到buffer(fragmentEnd * 3 + 1)部分。

I think that it confuses me most b/c I can't find where the buffer variable is instantiated anywhere (shouldn't it be something more like Vector<char, 4096> buffer = new Vector<char, 4096>(...) ? 我认为这使我最困惑,因为我找不到在任何地方实例化缓冲区变量的位置(应该不是Vector<char, 4096> buffer = new Vector<char, 4096>(...)

Thanks in advance 提前致谢

I get that it's making a vector of type char, with each entry being an array of char 4096 bytes large 我知道它正在构成char类型的向量,每个条目都是一个4096字节大的char数组

It's not. 不是。 The WTF::Vector template takes two template parameters. WTF :: Vector模板采用两个模板参数。 The first is the element type (in this case char, which means that each element is a char, not an array of chars). 第一个是元素类型(在本例中为char,这意味着每个元素都是一个char,而不是char的数组)。 The second is the inline capacity, ie the number of bytes the vector can store without allocating additional memory on the heap. 第二个是内联容量,即向量可以存储的字节数,而无需在堆上分配额外的内存。

but I don't get the buffer(fragmentEnd * 3 + 1) part. 但我没有得到buffer(fragmentEnd * 3 +1)部分。

The syntax to create a variable on the stack is type variablename(constructor,arguments); 在堆栈上创建变量的语法是type variablename(constructor,arguments); . So buffer is the name of the variable and fragmentEnd * 3 + 1 is the argument to Vector<char, 4096> 's constructor (which specifies the vector's initial size). 因此, buffer是变量的名称, fragmentEnd * 3 + 1Vector<char, 4096>的构造函数的参数(用于指定向量的初始大小)。

The buffer variable is constructed exactly on that line: buffer变量正是在该行上构造的:

Vector<char, 4096> buffer(fragmentEnd * 3 + 1);

Walking through the steps that the compiler takes, it first ensures that there are sizeof(Vector<char, 4096>) bytes of space on the stack into which it can construct the Vector<char, 4096> buffer object. 逐步执行编译器所采取的步骤,首先要确保堆栈上有sizeof(Vector<char, 4096>)个字节的空间,编译器可以在其中构造Vector<char, 4096> buffer对象。 It then calls a Vector<char, 4096> constructor that can take an int (because fragmentEnd * 3 + 1 is an int ) on the storage in the stack. 然后,它调用Vector<char, 4096>构造函数,该构造函数可以在堆栈中的存储区上使用int (因为fragmentEnd * 3 + 1int )。

In the current Vector sources, there is no constructor that takes an int . 在当前的Vector源中,没有采用int构造函数。 However, there is a constructor that takes a size_t (an unsigned int ), so the compiler will statically cast the result fragmentEnd * 3 + 1 to a size_t and invoke that constructor. 但是,有一个采用size_t (无符号int的构造函数 ,因此编译器会将结果fragmentEnd * 3 + 1静态转换为size_t并调用该构造函数。

No it shouldn't, new may or may not be used to allocate a new object in C++, unlike, for example, C#. 不,它不应该, new可以或可以不用于在C ++中分配新对象,这与C#不同。

Explanation: 说明:

Vector is not a standard class, like STL's vector. Vector不是STL的向量之类的标准类。

The line creates object buffer, on stack, of type Vector. 该行在堆栈上创建Vector类型的对象缓冲区。 It then passes parameters to the constructor (what's in the brackets) 然后将参数传递给构造函数(括号中的内容)

Check out more info about the Vector class that is used (it is the Vector from webkit (WTF namespace) - not from std). 查看有关所使用的Vector类的更多信息(它是来自Webkit的Vector(WTF名称空间)-不是来自std)。

Check the Vector.h header file. 检查Vector.h头文件。 It is here . 在这里

From Iulian's link, the source for aptly-named WTF::Vector says that the second template parameter is named inlineCapacity . 在Iulian的链接中,恰当命名为WTF::Vector的源表示第二个模板参数名为inlineCapacity

So, it has something to do with some kind of optimization which should have nothing to do with functionality. 因此,它与某种优化有关,而这种优化应该与功能无关。 Ignore it. 忽略它。 The size of the buffer is fragmentEnd * 3 + 1 , nothing more or less. 缓冲区的大小为fragmentEnd * 3 + 1 ,或多或少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM