Struggling with mobile compatibility for string manipulation in C for MVP
I've been struggling with this for a few days now and could really use some help. I tried several approaches but none seem to work. Building an application that requires dynamic string manipulation and needs to function seamlessly on mobile devices. I've implemented a simple string concatenation function that works well on desktop platforms but runs into performance issues on mobile. Hereโs the code I have: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* concat_strings(const char* str1, const char* str2) { size_t len1 = strlen(str1); size_t len2 = strlen(str2); char* result = malloc(len1 + len2 + 1); if (result) { strcpy(result, str1); strcat(result, str2); } return result; } ``` The function is straightforward, but performance profiling during development showed significant overhead when concatenating strings, particularly with larger inputs. I tried optimizing by reducing the number of calls to `malloc`, but this led to fragmented memory and eventual crashes on lower-end mobile devices. To mitigate this, I experimented with using a buffer to pre-allocate memory, but it introduced complexity in managing string lengths and ensured null termination. Hereโs the modified approach I attempted: ```c #define BUFFER_SIZE 1024 char buffer[BUFFER_SIZE]; void concat_to_buffer(const char* str1, const char* str2) { snprintf(buffer, BUFFER_SIZE, "%s%s", str1, str2); } ``` Despite this, the strings sometimes overflowed the buffer in specific cases. Iโve researched using `strncat` for safer concatenations, but I worry it might also introduce performance bottlenecks. Given that my focus is on creating a mobile MVP, any recommendations on best practices for efficient string manipulation in C on constrained devices would be invaluable. Are there alternative libraries or design patterns that could better suit mobile environments? What are the trade-offs I might face with different approaches? Appreciate any insights! I'm working on a application that needs to handle this. Has anyone else encountered this? This is happening in both development and production on Windows 10.