0

I'll preface this question by saying I am knowledgeable on C, and I'm learning C++.

Suppose I've got an std::string object and I want to go thru all of its characters to verify it (for example, only 4 letters allowed).

I have seen for (type var : array) being used in some code around a week ago, but I couldn't remember how it was used. I'm wondering.. exactly how does this work, and why does it work? Logically it shouldn't.

AFAIK, arrays don't really "know" what their length is. So suppose I have this code:

int arr[] = { 1, 2, 3 };
for (int item : arr) {
    std::cout << item << std::endl;
}

How the hell does the for loop know when to stop? Arrays are simply put allocated memory (stack, in this case) that start from a specific address and go on. There's no "stored length" (AFAIK) for arrays. This is why it is a programmer's responsibility to store the length of the array, and do for (int i = 0; i < [length]; i++) but this somehow works?

Would love some explanation. Specific use case for my code:

std::string test = "Hello World!";
for (char letter : test) {
    if (letter == 'l') {
        std::cout << "'l' exists!" << std::endl;
    } // result should be 3 prints.
}
yeho
  • 132
  • 2
  • 13
  • 3
    Of course an arrays has a known length, how else would the compiler know how much space to allocate for it? Are you confusing arrays with arrays decayed to pointers? – UnholySheep Nov 06 '20 at 19:44
  • arrays in C and C++ are very different, the big one being that C++ arrays know their size. – cigien Nov 06 '20 at 19:45
  • @cigien: unless you're talking about the `std::array` *class,* C++ knows as much about its arrays as C does. – paxdiablo Nov 06 '20 at 19:46
  • @paxdiablo But in `int arr[] = { 1, 2, 3 };` the size of `arr` is part of its type. That's not the case in C, right? – cigien Nov 06 '20 at 19:48
  • @cigien I'm used to C arrays, so I'm very confused about how C++ handles this type of loop. Also, another thing that confuses me is, why is it only supported in C++11 and up? – yeho Nov 06 '20 at 19:48
  • Because that's when it was added to the language, that's all :) Spend some time reading the dupe target, it explains how the loop works. – cigien Nov 06 '20 at 19:50
  • @cigien `int arr[] = { 1, 2, 3 };` is the exact same thing in C and C++, it deduces `arr` to be of type `int[3]` – UnholySheep Nov 06 '20 at 19:51
  • The linked answer (dupe) only explains how the variable is initialized (the variable x). It does not explain how it works behind the scenes in terms of memory. This is what interests me. I want to know where this supposed length is stored. – yeho Nov 06 '20 at 19:51
  • Oh, I see you're asking specifically about `range-for` with arrays. I've added a specific duplicate target for that as well. – cigien Nov 06 '20 at 19:52
  • @UnholySheep Oh, really? I haven't written C in over a decade, but I remember needing to do `sizeof` tricks to figure out the size of a C array. – cigien Nov 06 '20 at 19:54
  • @cigien You mean `sizeof(arr)/sizeof(*arr)`? Wouldn't you do the exact same thing in C++ for a C-style array? And isn't that also what `std::end()` and `std::size()` do for arrays? – UnholySheep Nov 06 '20 at 19:55
  • @UnholySheep For a C-style array, sure. But that's just `int*`, and there's no C-style array in the OP's code. And you can't do `begin` `end` or use `range-for` loops with C-style arrays. – cigien Nov 06 '20 at 19:57
  • @cigien Actually, great that you've pointed this out. Will the `for-range` loop work with `int* arr = new arr[5];`? – yeho Nov 06 '20 at 19:58
  • @yeho Why don't you try compiling the code yourself? Best way to find out :) – cigien Nov 06 '20 at 19:59
  • `this range-based 'for' statement requires a suitable "begin" function and none was found` – yeho Nov 06 '20 at 20:01
  • That's correct :) In C++, we call `int*` C-style arrays (and as far as I recall, those are the only kind of arrays in C), and you can't compute `begin` and `end` for them, so they can't be used in `range-for` loops. – cigien Nov 06 '20 at 20:02
  • Mhm. In C, there's `int arr[]` and `int* arr`, but functionally they're identical. If it's different in C++, according to you, how is it handled in the background? Is a C++ array an object? Moreover, is `begin` [0] and `end` [-1] (python reference)? – yeho Nov 06 '20 at 20:04
  • I'm pretty sure that this is all covered in whatever resources you're using to learn C++. I'm happy to answer questions, but now the topic is deviating away from the question in the post, and so I have to stop the discussion. SO is not really a tutorial site, or a discussion forum, I'm afraid. Hope you understand :) – cigien Nov 06 '20 at 20:07

0 Answers0