94

I am working with the memory of some lambdas in C++, but I am a bit puzzled by their size.

Here is my test code:

#include <iostream>
#include <string>

int main()
{
  auto f = [](){ return 17; };
  std::cout << f() << std::endl;
  std::cout << &f << std::endl;
  std::cout << sizeof(f) << std::endl;
}

The ouptut is:

17
0x7d90ba8f626f
1

This suggests that the size of my lambda is 1.

  • How is this possible?

  • Shouldn't the lambda be, at minimum, a pointer to its implementation?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
  • 18
    its implemented as a function object (a `struct` with an `operator()`) – george_ptr May 27 '16 at 11:02
  • 15
    And an empty struct can not be size 0 hence the 1 result. Try capturing something and see what happens to the size. – Mohamad Elghawi May 27 '16 at 11:03
  • 3
    Why should a lambda be a pointer??? It's an object that has a call operator. – Kerrek SB May 27 '16 at 11:37
  • 7
    Lambdas in C++ exist at compile-time, and invocations are linked (or even inlined) at compile or link time. There's therefore no need for a *runtime* pointer in the object itself. @KerrekSB It's not an unnatural guess to expect that a lambda would contain a function pointer, since most languages that implement lambdas are more dynamic than C++. – Kyle Strand May 27 '16 at 18:44
  • @KyleStrand: That's a fairly vague statement. A lambda is an expression, and the expression does indeed exist at compile time, but what matters is the *value* you obtain from evaluating that expression, which is the closure object, and that exists at runtime. Expression evaluation happens (as-if) at runtime (except for constant expressions). – Kerrek SB May 27 '16 at 19:23
  • 2
    @KerrekSB "what matters" -- in what sense? The *reason* a closure object can be empty (rather than containing a function pointer) is *because* the function to be called is known at compile/link time. This is what the OP seems to have misunderstood. I don't see how your comments clarify things. – Kyle Strand May 27 '16 at 20:50
  • If you ever need a type's true size rather than sizeof's lie, you can use `return std::is_empty_v ? 0 : sizeof(T);`. I used this in certain templated classes to determine whether a lambda has any state or not. `#define true_sizeof(T) TrueSizeof >()` – Dwayne Robinson Dec 20 '19 at 08:02

5 Answers5

116

The lambda in question actually has no state.

Examine:

struct lambda {
  auto operator()() const { return 17; }
};

And if we had lambda f;, it is an empty class. Not only is the above lambda functionally similar to your lambda, it is (basically) how your lambda is implemented! (It also needs an implicit cast to function pointer operator, and the name lambda is going to be replaced with some compiler-generated pseudo-guid)

In C++, objects are not pointers. They are actual things. They only use up the space required to store the data in them. A pointer to an object can be larger than an object.

While you might think of that lambda as a pointer to a function, it isn't. You cannot reassign the auto f = [](){ return 17; }; to a different function or lambda!

 auto f = [](){ return 17; };
 f = [](){ return -42; };

the above is illegal. There is no room in f to store which function is going to be called -- that information is stored in the type of f, not in the value of f!

If you did this:

int(*f)() = [](){ return 17; };

or this:

std::function<int()> f = [](){ return 17; };

you are no longer storing the lambda directly. In both of these cases, f = [](){ return -42; } is legal -- so in these cases, we are storing which function we are invoking in the value of f. And sizeof(f) is no longer 1, but rather sizeof(int(*)()) or larger (basically, be pointer sized or larger, as you expect. std::function has a min size implied by the standard (they have to be able to store "inside themselves" callables up to a certain size) which is at least as large as a function pointer in practice).

In the int(*f)() case, you are storing a function pointer to a function that behaves as-if you called that lambda. This only works for stateless lambdas (ones with an empty [] capture list).

In the std::function<int()> f case, you are creating a type-erasure class std::function<int()> instance that (in this case) uses placement new to store a copy of the size-1 lambda in an internal buffer (and, if a larger lambda was passed in (with more state), would use heap allocation).

As a guess, something like these is probably what you think is going on. That a lambda is an object whose type is described by its signature. In C++, it was decided to make lambdas zero cost abstractions over the manual function object implementation. This lets you pass a lambda into a std algorithm (or similar) and have its contents be fully visible to the compiler when it instantiates the algorithm template. If a lambda had a type like std::function<void(int)>, its contents would not be fully visible, and a hand-crafted function object might be faster.

The goal of C++ standardization is high level programming with zero overhead over hand-crafted C code.

Now that you understand that your f is in fact stateless, there should be another question in your head: the lambda has no state. Why does it not size have 0?


There is the short answer.

All objects in C++ must have a minimium size of 1 under the standard, and two objects of the same type cannot have the same address. These are connected, because an array of type T will have the elements placed sizeof(T) apart.

Now, as it has no state, sometimes it can take up no space. This cannot happen when it is "alone", but in some contexts it can happen. std::tuple and similar library code exploits this fact. Here is how it works:

As a lambda is equivalent to a class with operator() overloaded, stateless lambdas (with a [] capture list) are all empty classes. They have sizeof of 1. In fact, if you inherit from them (which is allowed!), they will take up no space so long as it doesn't cause a same-type address collision. (This is known as the empty base optimization).

template<class T>
struct toy:T {
  toy(toy const&)=default;
  toy(toy &&)=default;
  toy(T const&t):T(t) {}
  toy(T &&t):T(std::move(t)) {}
  int state = 0;
};

template<class Lambda>
toy<Lambda> make_toy( Lambda const& l ) { return {l}; }

the sizeof(make_toy( []{std::cout << "hello world!\n"; } )) is sizeof(int) (well, the above is illegal because you cannot create a lambda in a non-evaluated context: you have to create a named auto toy = make_toy(blah); then do sizeof(blah), but that is just noise). sizeof([]{std::cout << "hello world!\n"; }) is still 1 (similar qualifications).

If we create another toy type:

template<class T>
struct toy2:T {
  toy2(toy2 const&)=default;
  toy2(T const&t):T(t), t2(t) {}
  T t2;
};
template<class Lambda>
toy2<Lambda> make_toy2( Lambda const& l ) { return {l}; }

this has two copies of the lambda. As they cannot share the same address, sizeof(toy2(some_lambda)) is 2!

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 7
    Nit: A function pointer can be smaller than a void*. Two historical examples: Firstly word addressed machines where sizeof(void*)==sizeof(char*) > sizeof(struct*) == sizeof(int*). (void* and char* needs some extra bits to hold the offset within a word).Secondly the 8086 memory model where void*/int* was segment+offset and could cover all of memory, but functions fitted within a single 64K segment (so a function pointer was only 16 bits). – Martin Bonner supports Monica May 28 '16 at 06:22
  • 1
    @martin true. Extra `()` added. – Yakk - Adam Nevraumont May 28 '16 at 09:21
54

A lambda is not a function pointer.

A lambda is an instance of a class. Your code is approximately equivalent to:

class f_lambda {
public:

  auto operator() { return 17; }
};

f_lambda f;
std::cout << f() << std::endl;
std::cout << &f << std::endl;
std::cout << sizeof(f) << std::endl;

The internal class that represents a lambda has no class members, hence its sizeof() is 1 (it cannot be 0, for reasons adequately stated elsewhere).

If your lambda were to capture some variables, they'll be equivalent to class members, and your sizeof() will indicate accordingly.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
27

Your compiler more or less translates the lambda to the following struct type:

struct _SomeInternalName {
    int operator()() { return 17; }
};

int main()
{
     _SomeInternalName f;
     std::cout << f() << std::endl;
}

Since that struct has no non-static members, it has the same size as an empty struct, which is 1.

That changes as soon as you add a non-empty capture list to your lambda:

int i = 42;
auto f = [i]() { return i; };

Which will translate to

struct _SomeInternalName {
    int i;
    _SomeInternalName(int outer_i) : i(outer_i) {}
    int operator()() { return i; }
};


int main()
{
     int i = 42;
     _SomeInternalName f(i);
     std::cout << f() << std::endl;
}

Since the generated struct now needs to store a non-static int member for the capture, its size will grow to sizeof(int). The size will keep growing as you capture more stuff.

(Please take the struct analogy with a grain of salt. While it's a nice way to reason about how lambdas work internally, this is not a literal translation of what the compiler will do)

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
13

Shouldn't the lambda be, at mimumum, a pointer to its implementation?

Not necessarily. According to the standard, the size of the unique, unnamed class is implementation-defined. Excerpt from [expr.prim.lambda], C++14 (emphasis mine):

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below.

[ ... ]

An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:

— the size and/or alignment of the closure type,

— whether the closure type is trivially copyable (Clause 9),

— whether the closure type is a standard-layout class (Clause 9), or

— whether the closure type is a POD class (Clause 9)

In your case -- for the compiler you use -- you get a size of 1, which doesn't mean it's fixed. It can vary between different compiler implementations.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Are you sure this bit applies? A lambda without a capture-group isn't really a "closure." (Does the standard refer to empty-capture-group lambdas as "closures" anyway?) – Kyle Strand May 27 '16 at 18:47
  • 1
    Yes, it does. This is what the standard says "_The evaluation of a lambda-expression results in a prvalue temporary. This temporary is called the closure object._", capturing or not, it's a closure object, just that one will be void of upvalues. – legends2k May 28 '16 at 10:24
  • I didn't downvote, but possibly the downvoter doesn't think this answer is valuable because it doesn't explain *why* it's possible (from a theoretical perspective, not a standards perspective) to implement lambdas without including a run-time pointer to the call-operator function. (See my discussion with KerrekSB under the question.) – Kyle Strand Jun 03 '16 at 18:07
8

From http://en.cppreference.com/w/cpp/language/lambda:

The lambda expression constructs an unnamed prvalue temporary object of unique unnamed non-union non-aggregate class type, known as closure type, which is declared (for the purposes of ADL) in the smallest block scope, class scope, or namespace scope that contains the lambda expression.

If the lambda-expression captures anything by copy (either implicitly with capture clause [=] or explicitly with a capture that does not include the character &, e.g. [a, b, c]), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured.

For the entities that are captured by reference (with the default capture [&] or when using the character &, e.g. [&a, &b, &c]), it is unspecified if additional data members are declared in the closure type

From http://en.cppreference.com/w/cpp/language/sizeof

When applied to an empty class type, always returns 1.

Community
  • 1
  • 1
george_ptr
  • 558
  • 6
  • 17