What if it does ? If you go through with your plan to expand the array of Strings, which sound like a bad idea, then at some time you will use up the memory anyway so why not allocate it to start with ?
By the way, expect replies to this topic telling you not to use Strings, but I will leave that to others. Which board are you using ?
Expanding the array will increase the memory usage by six bytes per element (on an UNO), may be larger on an ESP32, you can check by printing sizeof(String). The actual memory for the text stored in the String is not part of the array, but is stored on the heap, and will not be allocated until you create the String.
The search should not take all that much time, but if you are concerned about that then keep track of the current number of users and only search for that many.
So you can use a std::vector and take advantage of dynamic allocation, so why bother with a fixed-size C array?
#include <vector>
std::vector<String> users;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
for (int i=0; i<10; i++) {
String user = "user-";
user += i + 1;
users.push_back(user);
}
// You can use a range based for loop to iterate the vector
for (String &user : users) {
Serial.println(user);
}
}
Or std::list which provides better performance for element insertion and deletion at the expense of poorer memory efficiency and lack of random access. The choice depends on your use case.
since insert(append) and update via index number is not so often, and ill use "access using indexing " and linear search over and over again, i think std::vector will be better choice
What type of application is the list of users being used in ? It takes an ESP32 about 2 microseconds to iterate through an array of 500 Strings looking for a match. Is your application that time critical ?
In the end, with the maximum of 500 users, the memory usage will likely be nearly the same.
The fact is, if you want to allow for up to 500 users, there absolutely has to be sufficient memory for that many, and if you really want to be safe, 500 users who use the maximum length that you are allowing for the data.
IT is always a balance between resource usage (memory, CPU, data transfer, etc.) and global or specific functions efficiency (together with costs, but that's not this case).
Almost any solution reducing the resources can't be always quick, but everything depends on the requirements (e.g. how much is important to get a result in 200ms instead of 50?). So if you want to obtain faster execution you should know it could require more resources/memory (e.g. a fully allocated array), if you want to save resources it can't be as quick as the first one. In any project the costs/benefits balance should always be taken in consideration when designing a system.
Just as an example, if you store the names as a file over SD card, it is the solution with less memory usage, but at the cost of search speed.
Besides, one of my favourite statement when dealing with customer requests is: "Any IT project can have three properties: good coding ; made in a short time ; cheap You can choose only two."
I don't know if anyone else did a similar thing, but I kinda fine tuned it during my years of IT work expericence, making the statement as compact as possible to be understandable by the "mean customer" (and here in Italy the customers are used to pay even a lot for hardware but always underestimate software because they "understand" the hardware and don't know a thing about how hard is a good coding...).
But IMHO it's a good and realistic rule of thumb to let customers understand that:
if they want it good and within a short time, can't be cheap
if they want it good and cheap, can't deliver it in a short time
if they want it cheap and in short time, it can't be good
As already noted, each object of the String class is only a few bytes long, the text data itself is stored in dynamically allocated memory as required. Thus Strings will be more memory efficient than c-strings (null-terminated char arrays), especially if there is a large distribution of string lengths.
A std::vector of String objects will be more memory efficient than a static array of String objects if there are significantly fewer allocated Strings than the maximum specified in the array.
Addition and removal (especially) of entries will be easier with std::vector as you'd be using the capabilities of a bulletproof, thoroughly debugged library rather than trying to roll your own implementation.
The time required to traverse the data structure will be indistinguishable between an array and a std::vector (assuming you keep track of the number of active entries in the array implementation).
If it were my project, I'd go with a std::vector of String (or std::string) objects.