Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7719069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:19:18+00:00 2026-06-01T03:19:18+00:00

I have a 2D pointer setup representing a grid, the grid consists of columns

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:19:20+00:00Added an answer on June 1, 2026 at 3:19 am

    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.

        newg = malloc (*height * sizeof(char *));
    
        // Initialise each column
        for (x = 0; x < *height; x++) {
            newg[x] = malloc (*width);
            for (y = 0; y < *width; y++)
                newg[x][y] = 0;
        }
    

    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:

    #include <stdio.h>
    #include <stdlib.h>
    
    char** alloc_rectangle(int *width, int *height);
    void free_rectangle(char **orig, int *width);
    char** spin (char **orig, int *width, int *height);
    
    int main (int argc, const char * argv[]) {
        int width = 20;
        int height = 30;
    
        char** orig = alloc_rectangle(&width, &height);
        char** newg = spin(orig, &width, &height);
    
    
        return 0;
    }
    
    char** alloc_rectangle(int *width, int *height) 
    {
        char **newg = calloc (*width, sizeof(char *));
    
        // Initialise each column
        for (int x = 0; x < *width; x++) {
            newg[x] = calloc (*height, sizeof(char));
        }
        return newg;
    }
    
    void free_rectangle(char **orig, int *width)
    {
        // free memory for old grid
        for (int x = 0; x < *width; x++) {
            if (orig[x] != NULL) {
                free (orig[x]);
            }
        }
    
        free (orig);
    }
    
    char** spin (char **orig, int *width, int *height) 
    {
        int x;
        int y;
    
        char **newg = alloc_rectangle(height, width);
    
        // Rotate
        for (x = 0; x < *width; x++) {
            for (y = 0; y < *height; y++)
                if (orig[x] != NULL) 
                    newg[*height - 1 - y][x] = orig[x][y];
        }
    
        return newg;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pointer to integer array of 10. What should dereferencing this pointer
I have a pointer to a map that I am trying to delete (this
I have a pointer which points to a function. I would like to: if
I have a setup similar to code below, where there can be any number
Say I have this basic setup: #include <list> struct Linker { Linker* to; //some
i have JavaScript components, that has following architecture: var MyComponent = function(params) { setup(params);
I have a C struct that contains a function pointer. Now, I have used
Help, I'm deperate..i can't continue my app because of this: I have setup one
The Setup I have a PDF API which has a native function that is
I have a pointer to a UIView . How do I access its UIViewController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.