I have a function which outputs a really long string in a box of defined size. The problem is, after a few lines, I get an error saying that HEAP CORRUPTION has occured and I wrote to a memory beyond the allocated limit. Now, I'm only writing to console and not to memory so this has me puzzled to my wits end. Can some one please take a look and tell me whats wrong?

int TextField::edit(){
    int x = console_Box(frow - 1, fcol - 1, len + 2, txtHeight + 2, txtEdge, txtTop_btm, txtLs_rs); //Function to make the box
    int index = 0, i = 0, exitCode = 0, lenSent = 0;
    char * line = new char[fslen + 10];
    for(i = 0; i < txtHeight; i++){
        //the original string of data is 'fstr'
        //getline gets a single line, of length 'len'. the start position is 'i' 
        //and 'lenSent' is the length of the string already sent.
        strcpy(line, getLine(fstr, len, i, lenSent)); //I get an error here, 
        //which makes no sense as getline will always return a string
        // 10 characters shorter than line.
        console_Move(frow + i, fcol);
        console_PutS(line);
        lenSent += strlen(line);
        }    

    console_Move(frow, fcol);

    return exitCode;
    }

which makes no sense as getline will always return a string 10 characters shorter than line.

How do you know this?

Actually, this is hard to figure out as you're not telling us a lot of other stuff. You have a function call to getLine( ) - what's this do?

Well depite the fact that this looks awful, you almost definately have a
memory leak. You initialize line to fslen+10, whatever fslen is. Then you do not delete it.

However, as vmanes says, you are not giving us much to go on. I would, at the minimum post the class and related functions

ok. The problem was in the getLine() function.

Sorry its doesn't look so neat, thats why I have done everything the again :-)

Thanks all for the replies!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.