简体   繁体   中英

How do memory addresses know how long an an array is?

I guess I can tag C++, Assembly and C as memory management works similarly in all languages.

I've just hit the stage in C where I'm learning pointers. I understand that if you store a string (or in C, better known as a char array) it's actually just an address in memory of the first element of that array.

How does this first element of the array know how much it has to count for the whole array, my own guess is it stops at the first occurrence of \\0 but am I right? I've ben told \\0 is a sign that the array has finished that you've hit the last element (or technically, 1 after the last element).

I guess I posted this for validation of my maybe-right-answer. Can anyone explain it in better detail?

It doesn't.

Use a too-large value as an index and you read from/write to memory outside of the array.

Do it in C++ and the optimizer may cache data and not refresh it and get insane program states (UB).

Now, copy an array in a struct via = ? Size is then known at compile time, similarly no runtime markers used.

In those low-level languages, you can access an array even past what is allocated. You have to manually keep track of how long the array is. However, there are different data types, like linked lists, that can be built to keep track of the beginning and end of the data. For example, in Python, there are "lists" instead of "arrays", and the size of the list can be found out using the "len" built-in function.

To answer your question, memory addresses do not "know" how long an array is. It all depends on your computer architecture and is discovered through CPU instructions. When you access the first element of the array, an instruction is decoded by your CPU and will proceed to grab the first byte of data in the given memory address. This first byte of data should contain an ascii value that pertains to a specific char element and will display your desired character. If you iterate through the string and implemented it correctly, there typically is a null character to which you can end your iteration. However, do note that the null character is not guaranteed to be there.

I understand that if you store a string (or in C, better known as a char array) it's actually just an address in memory of the first element of that array.

A [narrow] string literal has type char[C] where C == 1+strlen(s) (the 1 is for the '\\0' at the end, which the strlen function excludes from its return value).

If you store a string literal in a variable of type char* , then it's indeed as you say: just an address in memory of the first element of that array. That is how "array decay" works.

If, however, you store a string literal in a variable of type char[N] , then how things work depends on N and 1+strlen(S) (where S would be a string literal). If N < 1+strlen(S) , then only the first N chars are stored in the char array, and the array is not terminated by '\\0' . If N >= 1+strlen(S) , then all chars of S are stored in the char array, and any elements in excess of 1+strlen(S) are zero-initialized per usual for arrays.

How does this first element of the array know how much it has to count for the whole array, my own guess is it stops at the first occurrence of \\0 but am I right? I've ben told \\0 is a sign that the array has finished that you've hit the last element (or technically, 1 after the last element).

The first element doesn't store the count of the array unless you make it that way. In the case of storing a string literal or any other char array in an object of type char* , you're discarding the size info, unless you stored the size in the first element manually (strings in the Pascal programming language do this). Whether or not you stored the size, you're still responsible for tracking the size info. If you initialize an object of type char[N] with a string literal, you keep the size info of the array, and the compiler tracks all N chars for the lifetime of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM