tags:

views:

179

answers:

5

I was trying to understand how webkit parses urls, and I'm having a hard time making heads or tails of this:

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). 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.

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>(...)?

Thanks in advance

+1  A: 

No it shouldn't, new may or may not be used to allocate a new object in C++, unlike, for example, C#.

Explanation:

Vector is not a standard class, like STL's vector.

The line creates object buffer, on stack, of type Vector. It then passes parameters to the constructor (what's in the brackets)

Pavel Radzivilovsky
+1  A: 

Check out more info about the Vector class that is used (it is the Vector from webkit (WTF namespace) - not from std).

Check the Vector.h header file. It is here.

Iulian Şerbănoiu
To complete: Initialization occurs in the lines below. it takes a reference to the internal buffer held in the Vector class and populates it (I'm referring to the part of the code you showed us).
Iulian Şerbănoiu
+2  A: 

I get that it's making a vector of type char, with each entry being an array of char 4096 bytes large

It's not. The WTF::Vector template takes two template parameters. The first is the element type (in this case char, which means that each element is a char, not an array of chars). The second is the inline capacity, i.e. 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.

The syntax to create a variable on the stack is 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).

sepp2k
+1  A: 

From Iulian's link, the source for aptly-named WTF::Vector says that the second template parameter is named 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.

Potatoswatter
+2  A: 

The buffer variable is constructed exactly on that line:

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. 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.

In the current Vector sources, there is no constructor that takes an 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.

Daniel Trebbien