I wrote the following function for my assignment at college. The whole thing is working flawlessly. This function requires me to create a box on the screen as follows:

int console_Box(int row, int col, int l, int h, char edge, char top_btm, char ls_rs)
This function displays a box that will be used to frame a
text field on the screen.
The box must be drawn starting at coordinates 'row' and 'col',
be 'l' and 'h' units in length and height respectively,
and be comprised of the characters 'edge' (for all 4 corners),
'top_btm' (for the top and bottom lines), and 'ls_rs' for
the left and right sides.
For example, the function call
console_Box(2, 3, 10, 3, '+', '-', '|'),
would draw the following box (excluding contents) starting
at row 2 and column 3:

0123456789012
0
1
2 +--------+
3 |abcdefgh|
4 +--------+
The function returns a true value if there is sufficient space
to draw the box on the screen, and a false value otherwise.
Note how a box which is 10 units in length, can only surround
an 8 character text field!

Can somebody explain why in the code below, when i use free(), it crashes. I have marked it at the very end.

int console_Box(int row, int col, int l, int h, char edge, char top_bm, char ls_rs){
    int i = 0, k = 0, x1, x2, y1, y2, rtnVal = 0;
    int borderDone = 0, s1 = 0;
    int checkCol = console_GetCols(), checkRow = console_GetRows();
    char_p s;
    s = (char *) malloc(sizeof(char) * (l - 1));
    

    /*Redefine the length and height of the box in local variables
    here, x1 is the starting row and x2 is where the height ends.
    The start of the box line is y1 and y2 is the end.*/

    x1 = row;               
    x2 = (row + h) - 1;     
    y1 = col;               
    y2 = (col + l) - 1;     

    /*Verify that the box is possible to be built as per the current screen size*/
    if((col + l) < checkCol && (row + h) < checkRow){

        /*Following Lines construct the horizontal borders
        using '+' characters for the corners and '-'
        for the lines. Also, it adds an extra line for
        the scroll bar.
        */
        s[0] = edge;
        for(i = 1; i < y2-3; i++){
            s[i] = '-';
        }
        s[i] = edge;
        s[i+1] = '\0';

        /*The following line put the border on the screen
        at footer and header positions.*/
        console_Move(x1, y1);
        console_PutS(s);
        console_Move(x2, y1);
        console_PutS(s);

        /*Since the corners are already been constructed
        the following lines only display the vertical
        border lines.*/
        for(k = y1; k <= y2;)
        {
            for(i = (x1 + 1); i < x2; i++)
            {
                console_Move(i, k);
                console_PutC(ls_rs);
            }
            k += l - 1;
        }

        /*Return TRUE when box built successfully*/
        rtnVal = 1;
    } 

    else /*Else Return false when the box is not possible to be built*/

    {
        rtnVal = 0;
    }
    
    /*free(s);*/  /*<-- THIS CRASHES THE WHOLE THING IF NOT COMMENTED */
    return rtnVal;

}

Recommended Answers

All 2 Replies

>Can somebody explain why in the code below, when i use free(), it crashes.
That's easy. You corrupted the memory and the dynamic memory manager is choking because the corruption removed something it expects to see. A common situation that causes this problem is failing to allocate enough memory for the entire string and the terminating null character, but you'll need to do some index counting to find out exactly where the corruption is (you're writing beyond the boundaries of the memory you allocated).

lol! ya i just saw it, i was going 2 over!! Thanks to visual studio step by step debugging!!

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.