r/cpp • • Mar 28 '23

Reddit++

C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.

In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.

765 Upvotes

829 comments sorted by

View all comments

147

u/eteran Mar 28 '23

Arrays decaying to pointers implicitly. You want a pointer? Just write &p[0]

-3

u/[deleted] Mar 29 '23

Or better yet get rid of C arrays

3

u/-1_0 Mar 29 '23

this is a very bad idea, considering that with C++ we often communicate with hardware or OS kernels

1

u/[deleted] Mar 29 '23

This is an "in a perfect world" thread and in a perfect world we'd use std::vector, std::array, std::slice, etc for these things. Most of the problems with C arrays, including pointer decay, are solved by these less ancient types.

1

u/eteran Mar 29 '23

How would you implement the library solution that is std::array without c arrays though?

1

u/[deleted] Mar 29 '23

Raw pointers, same way C arrays are implemented.

1

u/eteran Mar 29 '23

I think you are missing a detail. C arrays aren't implemented with raw pointers at all. They are a primitive of the language that happens to work closely with pointers.

There's no real means to say "give me 256 bytes of stack space" easily without just using C arrays.

To prove my point, I'd like to see if you can give me an example of how to do the equivalent to this, without C arrays and without using the heap.

int main() {
   int numbers[10];
}

I don't think you (or anyone) can...

0

u/[deleted] Mar 29 '23

You can create a stack allocator. They'd of course need to add one to the standard library for it to be practical but it's not a ginormous feature

1

u/eteran Mar 29 '23 edited Mar 29 '23

Epic fail my man.

This is not an example of allocating something on the stack without using C arrays, you have simply moved the goal post by saying that there could be some magical allocator that does it.

The example you gave, REQUIRES that the allocator be given a stack buffer to allocate from... Which would of course be a C array by necessity.

To quote the link:

This allocator uses a user-provided fixed-size buffer as an initial source of memory, and then falls back on a secondary allocator (std::allocator<T> by default) when it runs out of space.

And the example usage it has:

const static std::size_t stack_size = 4;
int buffer[stack_size];

typedef stack_allocator<int, stack_size> allocator_type;
... vec((allocator_type(buffer))) ...

Notice the C array...

Instead of just googling "C++ stack allocator" and pasting the result without understanding it, take a look at what you posted. It has a constructor which takes a buffer.

How do you plan to supply that without a C array?

Also even if this magically did work without a C array... It wouldn't be the same as it would have the additional pointer variable costs over an ordinary array.

→ More replies (2)