I have a 2D pointer setup representing a grid, the grid consists of columns containing 1/0 or null columns (i.e. don’t contain 1 in any cell). This function spins the grid by 90deg clockwise, and works except…
I think my malloc could be wrong as it works but I get many over picket-fence errors in dmalloc.
Am I allocating incorrect amounts of memory?
Also I wanted to swap the values of *width and *height to represent the new width and height of the grid but when I try this the program just segfaults on the second spin.
So *width is the dimension of orig’s first dimension, so it should be the size of newg’s second dimension.
Similarly *height should be the size of newg’s first, and hence the two sets of malloc sizes have been flipped the wrong way around.
I think it would be clearer to name the values orig_max_x and orig_max_y, then it should be clear if the function uses the values the wrong way around.
Further, it should not free any of newg’s storage if you want to return values from
spin()Edit: I still had some of those pesky *width and *height mixed. Sorry.
I strongly suggest the names should relate to the thing they talk about, orig_width,
orig_height would be have helped me read the code.
This is probably how I’d do it:
WARNING Untested code – some fun for all 🙂
I don’t think it is spin’s job to free orig. I’d prefer it to just make space to hold the result of spinning. So to make things tidier, I pulled freeing a rectangle into its own function. Similarly, I’d always want the rectangles to be allocated consistently, so that would be its own function.