0

Just started working with lambdas. I wanted to create pointer to array. I think that I searched the whole internet and every sigle tutorial and I didn't find the answer.

auto c = []() -> int* { int* b = new int[10]; b[0] = 2; b[1] = 2; return b; };
std::cout << sizeof(c()) << std::endl;
delete c();

The sizeof(c) is always returning 4 even if arrar size is 1000. And my question is can you actually do it?

uselessguy
  • 15
  • 1
  • 4

1 Answers1

1

The sizeof(c) is always returning 4 even if arrar size is 1000.

If on your platform, the size of a pointer is 4, then the result is expected and correct.

[]() -> int* { int* b = new int[10]; b[0] = 2; b[1] = 2; return b; }

This lambda returns a int*.

So, the sizeof is actually returning the size of the int* type, not the size of the int[10] type.

Sprite
  • 3,222
  • 1
  • 12
  • 29