0

After some criticism regarding this post I have decided to try another solution to test my issue.

I purpose of the code below was to save user input into an array and then output it when user types in exit. I am relatively new to C++ which means that I am aware that this is a code issue.

The constraints for this code are to not use STL or strings.

I have attached the code below which does not give the desired output, instead it prints out "exit" the number of times equal to the number of items stored in the array.

#include <iostream>

char * textArr[1000];
int lineCount = 0;

void saveText(char * text) {
    textArr[lineCount] = new char(1000);
    strcpy(textArr[lineCount], text);
}


int main()
{
    char * line = new char(1000);

    while (lineCount < 1000) {
        std::cin.getline(line , 1000);

        if (strcmp(line, "exit") == 0) {
            break;
        }

        saveText(line);
        lineCount++;
    }

    for (int i = 0; i < lineCount; i++) {
        std::cout << textArr[i] << std::endl;
    }

    delete(textArr);
    delete(line);

    return 0;
}
  • 4
    Create a [mcve]. Also, why do you think that the map key was corrupted? I admit I haven't used VS debugger; does it really have a feature that tells which objects in memory are corrupted? – eerorika May 13 '17 at 22:35
  • Compile your program with debugging symbols and run it with valgrind. `valgrind ./yourprogram`. This should be able to find where you are getting memory corrupted. I think by the time you are reading `instr`, it's already corrupted. – arboreal84 May 13 '17 at 22:38
  • As a wild guess, (I'm not sure because I'm not familiar with `std::regex`) the second parameter to `std::regex_match` takes a `const&`, but it's possible that it expects the `std::regex` to not be a temporary. What if you defined the regex as a variable instead of inline? – Justin May 13 '17 at 22:39
  • 1
    @Justin It's perfectly fine to pass a temporary to a function that has a `const&` parameter. – zett42 May 13 '17 at 22:41
  • @zett42 I know, my point is that it's possible that `std::regex_match` expects it to not be a temporary (uses it after the temporary would be destroyed). Idk if this even matters, but that's a "wild guess" for you – Justin May 13 '17 at 22:42
  • @user2079303 I am submitting as much information as I can in hopes that this might be a known issue that I cannot find using Google. For the memory bit, please refer to the image I have posted a link to, the map key is turned from "b" to a weird set of characters. – Oskar Augustyn May 13 '17 at 22:43
  • @Justin It does not seem to work with the `std::regex` being declared as a separate variable. It just results in the key being changed twice. If I comment out this piece of code, everything works fine. – Oskar Augustyn May 13 '17 at 22:45
  • @Justin At least [cppreference](http://en.cppreference.com/w/cpp/regex/regex_match) doesn't state anything about that. – zett42 May 13 '17 at 22:49
  • @Justin functions can not use their arguments after the function has returned. A temporary argument of a function is not destroyed until the function has returned. – eerorika May 13 '17 at 22:49
  • @OskarAugustyn, valgrind use case is exactly this. Just install valgrind, run your program with it and let it find the problem for you. – arboreal84 May 13 '17 at 22:55
  • @partycode I have looked at valgrind but from what I can see it is Linux only (I am currently using Windows). I have looked at alternatives and found Dr. Memory (http://stackoverflow.com/a/6580332) however it experiences an internal crash when I drag my executable to it. – Oskar Augustyn May 13 '17 at 23:03
  • @user2079303 I have updated the question with a more concise, reproducible example. – Oskar Augustyn May 14 '17 at 18:56
  • 1
    You are using a single buffer for line, so you are storing that buffer's pointer in every location of your array (and overwriting the same buffer's contents in your read loop, so you end up with an array of elements all pointing to the same single buffer that contains your last line). You need to allocate memory for each line in the loop (and you should consider freeing that memory at the end of your program) – Joe May 14 '17 at 19:02
  • @Joe, I have modified the saveText function to use strcpy however the program now crashes during the `textArr[lineCount] = new char(1000)` line due to "A heap has been corrupted" – Oskar Augustyn May 14 '17 at 19:17
  • 2
    Did you maybe mean `new char[1000]`? – Retired Ninja May 15 '17 at 06:34
  • Don't delete textArr, since you didn't allocate it with new. – Klitos Kyriacou May 15 '17 at 07:06

0 Answers0