-7

I have a static array which I only need to update in certain areas.

char* MapIds[5000] = { "Northeast Asia","Hanyang","Pusan","Pyongyang","Shanghai","Beijing","Hong Kong" /* 4999 more stuff */ };

I have a mapName which is a JSON Object it can give a String which I convert to a char* using c_str();

I want to be able make use of the strings even when the JSON loading function is exited so thats why I try to copy the string into the array of string literals. Also not touch other string indexes that I didn't have to replace with new strings. But they all crash or give empty strings.

I try to stay away from std::string and instead use char*'s because std::string is slow with huge initialized list

            int size = strlen(mapName.ToString().c_str());
            char* str = new char[size + 1];
            str[size] = '\0';
            //std::copy(mapName.ToString().c_str(), mapName.ToString().c_str() + size, MapIds[std::stoi(mapId.ToString().c_str())]); //fails
            //strcpy(MapIds[std::stoi(mapId.ToString().c_str())], mapName.ToString().c_str()); //fails
            //MapIds[std::stoi(mapId.ToString().c_str())] = mapName.ToString().c_str(); //fails
            //sprintf(MapIds[std::stoi(mapId.ToString().c_str())], "%s", mapName.ToString().c_str()); //fails
            MapIds[0] = "gfggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"; //this good

            printf("map0 = %s, map1 = %s, map2 = %s\n", MapIds[0], MapIds[1], MapIds[2]);
SSpoke
  • 5,656
  • 10
  • 72
  • 124
  • 2
    `std::string` will also make your problem go away entirely. How is your `MapIds` meant to know which strings were statically allocated and which ones were dynamically allocated? Are you managing the memory? So when you say _But they all crash or give empty strings_ you're saying each entry of `MapIds` you try to access crashes? You should endeavour to include a [mcve] which clearly demonstrates your issue. Surely you can print `MapIds` just fine? Then your call to `MapIds[0] = "gfg...."` would be fine? – Tas Mar 03 '20 at 21:53
  • 5
    *I try to stay away from std::string and instead use char*'s because std::string is slow with huge initialized list* -- How many times are you going to initialize the list? If it's only once, then that is a one-time thing, not constantly being executed over and over again. – PaulMcKenzie Mar 03 '20 at 21:55
  • 1
    You trade "slow" one time initialization for manual management and copying of C strings? Not a wise decision. – Ted Lyngmo Mar 03 '20 at 21:57
  • ya one time initialization while compiling takes 40 seconds.. previously I used `char * array[];` which took a few seconds to compile – SSpoke Mar 03 '20 at 21:57
  • 1
    "compiling takes 40 seconds...few seconds to compile" I think you're mixing up terminology. Compile time shouldn't be an issue since you compile once, and then run it many times. You shouldn't be recompiling your code over and over unless it changes. – JohnFilleau Mar 03 '20 at 21:59
  • @SSpoke To clarify, your issue is that code using `std::string` takes too long to _compile_, as opposed to it being slow at runtime? – Brian61354270 Mar 03 '20 at 21:59
  • Yes only compile is slow the runtime is fine. – SSpoke Mar 03 '20 at 22:00
  • 1
    @SSpoke The time it takes the compiler to compile the code is irrevelent. What is important is the time the compiled code takes to execute at runtime. And a one-time initialization of a static array of `std::string`s may or may not take a lot of time to execute at runtime, depending on the number and content of the strings. But the tradeoff in being able to *manage* and *update* the array effectively at runtime should outweight any slowness in the array's initialization. – Remy Lebeau Mar 03 '20 at 22:01
  • 1
    If compile time really is your issue, then trading ease of use of automatically managed `std::string` for a one-time gain of 30 seconds is insanity! Turn back! Use `std::string`!!! – JohnFilleau Mar 03 '20 at 22:02
  • I just think whats the point of using the new technology when the old one works the same way and compiles faster as a bonus I dont mind messy code as long as it works best in all cases and `char* []` seems to be what I am looking for. – SSpoke Mar 03 '20 at 22:05
  • 1
    This is at least the third time you've asked this question today – Alan Birtles Mar 03 '20 at 22:08
  • 3
    _I just think whats the point of using the new technology when the old one works the same way_ no it doesn't - if you were using `std::string` you wouldn't even have this question. You've have 5 lines of code: 1 for your initialisation, 3 to change the values of 3 of them, and one to print the whole thing. – Tas Mar 03 '20 at 22:12
  • 3
    "whats the point of using [`std::string`] when [c-style strings] work the same way...?" Your code isn't working. I wouldn't call that working the same way. – JohnFilleau Mar 03 '20 at 22:12
  • 1
    Will anyone else have to work on this code ever? `std::string` is more maintainable. Will they know they need to jump through hoops doing manual memory management? Heck, will YOU remember to do the manual memory management one month, six months, one year from now when you need to go and change something? Maintainability is a HUGE issue with code. – JohnFilleau Mar 03 '20 at 22:15
  • @John Google Dilbert Holy Grail to see the counter-argument. I'm pleased to note that it doesn't work very often. – user4581301 Mar 03 '20 at 22:17
  • @user4581301 Ah yes, "Job Securability", the hidden "ility". lol – JohnFilleau Mar 03 '20 at 22:19
  • ah well 3rd time I asked a similar question and can't get a solid answer which tells me how to use the outdated technology people back in the 70s - 90s who used C / C++ probably did what I'm trying to do on a daily basis and either none of those people exist anymore or everyone just forgot how to do it lol.. O well I guess I give up here. – SSpoke Mar 03 '20 at 22:39
  • 1
    @SSpoke What can we say? What you are trying to do is not legal C++. Yes, I think you are (or were) allowed to do it in C, but C++ is a different language. I have _not_ forgotten how it was in the early 80's (when I started). It was a mess of pointers with tons of bugs because of it. A well tested `std::string` removes that and adds a lot of convenience functions too. And you did get an answer. It does not allow you to break C++ rules as you wish for, but it lets you use modern classes and not suffer too much compilation angst because of it. – Ted Lyngmo Mar 03 '20 at 22:46
  • 2
    @SSpoke - actually, no, people who used C in the early 70s-90s did not do what you describe either. You seem to have an illusion that some things were easy then that are difficult now. Yes, sometimes people - mostly beginners - would think they could do what you describe. They would be rewarded with a mess of code that was hard to get right, would be fragile to change (e.g. small changes would suddenly unearth bugs that had to then be hunted down). Professionals worth their salt learned to not do such things. – Peter Mar 03 '20 at 23:54
  • 1
    fine fine guys thanks for the advice I'll stick to `std::string` thanks later guys @TedLyngmo @Peter – SSpoke Mar 04 '20 at 06:46

1 Answers1

1

I try to copy the string into the array of string literals

What you are trying to do isn't legal C++. char* MapIds[5000] = ... should be const char* MapIds[5000] = ... and trying to overwrite anything in that array makes your program have undefined behavior.

I try to stay away from std::string and instead use char*'s because std::string is slow [to compile] with huge initialized list

Make a std::vector<std::string> (or std::array) that you put in one .cpp file by itself and make a header with an extern declaration. Only if you need to update that vector will you notice the extra time it takes to compile it.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108