0
Console application has triggered a breakpoint.

So I have this 2D char array of pointers which I'm also passing onto other functions but when I try to delete the array I get a breakpoint error. I'm guessing some functions are not properly saving the data behind the pointer.

void toevoegenL()
{
    int keuze;
    int index = 0;
    int indey = 2;
    char** text;
    text = new char *[20];
    for (int i = 0; i <20; i++)
        text[i] = new char[10];
    fillspacearray(text);
    leverancier leverancier1;
    leverancier1.levID = instellenL();


    try
    {
        invoerschermL();
        gotoxy(22, 5); std::cout << leverancier1.levID;
        texteditor(22, 6, 4,text);
        cout << text[2][3];
        chararray_to_leverancier(leverancier1, text);
        wegschrijvenL(leverancier1);

        leverancier1.levID++;
        invoerschermL();
        gotoxy(22, 5); std::cout << leverancier1.levID;
        updatenL(leverancier1.levID);
    }
    catch (const std::exception& e)
    {
        cout << "er is een fout gebeurt, u kunt opnieuw proberen"<<endl;
        system("Pause");
        invoerschermL();
        gotoxy(22, 5); std::cout << leverancier1.levID;

    }

    for (int i = 0; i <20; i++)
        delete[] text[i];
    delete[] text;
}

this is the piece of code where the breakpoint happens.

delete[] text[i];

this line in particular.

I'm also not sure if I'm passing the arrays properly to the other functions.

like this:

void print2DArray(char** A, int width, int height)

or like this:

void print2DArray(char**& A, int width, int height)
Semanresu
  • 49
  • 1
  • 8
  • 1
    I'm just guessing here, but it seems to me that you're writing out of bounds of he allocated memory. You never write strings more than **nine** characters into the sub-arrays (nine, so you leave space for the string terminator)? – Some programmer dude Apr 21 '15 at 09:22
  • I would use the second form for passing the array. If you are already using pointers there is no need to pass a reference, imho. – user2891462 Apr 21 '15 at 09:28
  • wait, so you're saying that I can't write more than 9 characters per sub array? shouldn't that be 20? since I declared it that way. – Semanresu Apr 21 '15 at 09:29
  • That's a *one*-dimensional array of pointers. – molbdnilo Apr 21 '15 at 09:34
  • @Semanresu `text[i] = new char[10];` says that you can store ten characters. Subtract one for the terminating zero and you have space for nine. – molbdnilo Apr 21 '15 at 09:36
  • Elaborating on @Joachim's comment: Easily conceivable that the chunks of chars are allocated sequentially so that writing past an allocated sub array overwrites the allocator's bookkeeping information of the one after that. A hint whether that's the case would be that the *first* free always works. – Peter - Reinstate Monica Apr 21 '15 at 09:40
  • Also this "22" as argument to gotoxy and texteditor make me somewhat suspicious after 20 strings have been allocated... but of course it may be completely unrelated. – Peter - Reinstate Monica Apr 21 '15 at 09:42
  • ...that was the culprit. I honestly feel ashamed that I overlooked such a simple mistake. thansk anyway molbdnilo – Semanresu Apr 21 '15 at 10:02
  • It would be simpler with `std::array, 20>`. – Jarod42 Apr 21 '15 at 10:02

1 Answers1

0

As you have already realized, the problem is having passed 22 instead of 20 to gotoxy. A way of preventing this in future code is to define those values as constants, whether it is by using #define or a static const variable (you can see arguments for each one of them in this other StackOverflow question: static const vs #define). This way, you could do:

#define NUMBER_OF_STRINGS 20
#define LENGTH_OF_STRING 10
char** text;
text = new char *[NUMBER_OF_STRINGS];
for (int i = 0; i <NUMBER_OF_STRINGS; i++)
    text[i] = new char[LENGTH_OF_STRING];
...
gotoxy(NUMBER_OF_STRINGS, 5);

EDIT: I misunderstood what you said the problem was in the comments to the question. I do not think the for loop with delete within it is the problem.

Community
  • 1
  • 1
user2891462
  • 3,033
  • 2
  • 32
  • 60
  • No, the problem was that I tried to delete more arrays than there existed, the for loop till 20 with delete within while I declared it's length as 10. also there's nothing wrong with gotoxy since it's only to "go-to" the cursor at the given (x,y) coordinates. – Semanresu Apr 21 '15 at 11:13
  • I do not see anything wrong with your last loop. You have allocated an array of 20 entries, and each of those entries is an array. You need to then perform 21 de-allocations: one per each sub-array (their length is 10, but that is irrelevant) and a final one for the array which contains the sub-arrays. What did you change it to? Have you run it through Valgrind to make sure the change is not making you leak memory? – user2891462 Apr 21 '15 at 11:34
  • If I'm not mistaken, that is your original code. What have you changed in it? – user2891462 Apr 21 '15 at 11:52
  • This is going to be confusing for everyone, since I'm quoting an edit you proposed. You say your fix is to allocate 20 chars for each entry of text, "since the for loop has a length of 20." I think you do not really understand how allocating a bidimensional array works in C++, so I've explained it here: http://pastebin.com/raw.php?i=jCS7qWuR The problem with your code was not that. If changing that fixed it is probably because you were writing more chars to each row than allowed. You may need to allocate more than 10 per row then, but the reason is no to match the number in for loop. – user2891462 Apr 21 '15 at 13:11
  • I only roughly understood how this worked so thanks for the explanation. But in the end the error itself was because I tried to delete more char arrays than there actually existed (for loop till 20 instead of 10 when deleting). So either declare 20 char arrays OR delete 10 char arrays was my point I was trying to get across. – Semanresu Apr 22 '15 at 07:37
  • The code which is in the question right now, which I think it's the one that was there originally, is fine when it comes to deletion. You allocate 21 arrays (one of 20 entries, called text, and 20 more, one per entry in text). At the end you delete 21 arrays (first the 20 of text and then text itself). The problem was not having more deletes than new, the length of an array is not related to the number of calls to delete you need (please, re-read my explanation). Allocating 20 chars per array may have fixed your problem, but not for the reasons you think. – user2891462 Apr 22 '15 at 10:53
  • In a C++ code, there must be a call to delete for each call to new, the number of calls to delete and new must be the same. Your original code does that (see in this online IDE: http://goo.gl/BEWr5o). I'm sorry to keep repeating myself, but your problem is not deleting more char arrays that existed, you deleted all of them. The problem was somewhere else, probably writing too much in one of those functions. – user2891462 Apr 22 '15 at 11:09