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

  • Home
  • SEARCH
  • 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 9271231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:34:11+00:00 2026-06-18T15:34:11+00:00

i have a struct cell defined as typedef struct{ int id; terrainType terrain; }

  • 0

i have a struct “cell” defined as

typedef struct{
int id;
terrainType terrain;
} cell;

i then make a 2d array of cells with

cell** makeCellGrid(int sizeX, int sizeY)
{  
    cell** theArray;  
    int i;

    theArray = (cell**) malloc(sizeX*sizeof(cell*));  

    for ( i = 0; i < sizeX; i++)
    {
        theArray[i] = (cell*) malloc(sizeY*sizeof(cell));  
    }

   return theArray;  
}  

at first i thought this was working fine but a few seg faults later i discovered that with some values (e.g. makeCellGrid(32, 87) ) it breaks.
im fairly fresh with C pointers and memory junk and was hoping some one could point me in the right direction here.

with lower number bounds i had no issue accessing it with

map[i][j].id = x;

and so on

EDIT: forgot to add, from testing, the seg fault originate from

theArray[i] = (cell*) malloc(sizeY*sizeof(cell)); 
  • 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-18T15:34:12+00:00Added an answer on June 18, 2026 at 3:34 pm

    The code lacks error checking for the malloc() system call.

    So if the first call to malloc() failed the second one (in the loop) tries to assign memory to NULL which indeed leads to the segmentation violation your are witnessing.

    You might consider modifing you code like so:

    #include <stdlib.h>
    
    typedef struct {
      int id;
      TerrainType terrain;
    } CellType;
    
    void freeCellGrid(CellType ** ppCells, size_t sizeX)
    {
      size_t i = 0;
      for (; i < sizeX; ++i)
      {
        free(ppCells[i]);
      }
    
      free(ppCells);
    }
    
    CellType ** makeCellGrid(size_t sizeX, size_t sizeY)
    {  
        CellType ** ppCells = malloc(sizeX * sizeof(*ppCells));  
    
        if (ppCells)
        {
          size_t i = 0;
    
          for (; i < sizeX; ++i)
          {
              ppCells[i] = malloc(sizeY * sizeof(**ppCells));  
              if (NULL == ppCells[i])
              {
                freeCellGrid(ppCells, i);
                ppCells = NULL;
    
                break;
              }
          }
       }
    
       return ppCells;  
    } 
    

    Notes on my modifications:

    • Always check system calls for errors (in the case of malloc() on error NULL is returned)
    • Better use an unsigned type to access memory/array indicies; size_t is meant for this
    • In C there is no need to cast the value returned by a void * function like malloc()
    • Always try to initialise variables as soon as possible; un-initilaised variables very easily lead to “irrational” behaviour of the application
    • If working with pointers, it might be helpfull to ‘code’ the level of indirection into their names (I did this here by using the prefix pp to indicated that it’s a 2-level indirection)
    • types are different from variables: One way to distinguish this is by starting type names using capitals (CellType) and variables using small letters (ppCells).
    • If allocating memory to a pointer and it matters that the size of the allocated memory some suits the pointer’s type it’s always more secure to use the (dereferenced) pointer itself as argument to the sizeof operator then some type. As the declaration of the pointer the memory is allocated to might be changed during develpment and the adjustment of the argument to malloc() will be forgotten. To cut it short: doing as I did is less error prone.
    • If encapsulating the dynamical creation of structures (including arrays) it is a could idea to also implement a method which de-allocates it (here: freeCellGrid()). Even better start of with coding this deallocator first, as then you have it by hand when coding the allocator’s error handling (as shown for the second call to malloc()).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct which corresponds to a register in hardware. typedef unsigned int
I have a class as follows: typedef struct grid_cell_type { int x; int y;
I have the following struct: struct cell { int nmbr; struct cell *p; };
Ok so I have struct like this typedef struct { float x; float y;
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct Primitive, which has the following definition: typedef struct Primitive {
I have this array declared inside my main() function: int VISITED[9][9]={{0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0},
I have these 2 structs: #define M 100 #define N 100 typedef struct xml_Element
I have this structure : typedef struct xmlelem{ Char *Element_name; Char *Element_Text; pAttr_Element attr_arr[M];
I have created a cell array of structure-files, like this for example: >> res2

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.