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 3692842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:18:31+00:00 2026-05-19T04:18:31+00:00

I’m trying to achieve a dynamic two dimensional array in C. Whenever the program

  • 0

I’m trying to achieve a dynamic two dimensional array in C.

Whenever the program is trying to access a value in the array I’m checking whether or not the array is large enough. If it isn’t more storage should be allocated. The code below checks whether or not the array has enough columns and if it doesn’t it reallocates new memory.

unsigned long cnt; // y is the element we are trying to access, playfieldsize stores the size of the array
if (y >= playfieldsize.y) { // if the array isn't large enough more data should be allocated
    char **tmp;
    unsigned long cnt;
    cnt = playfieldsize.y; // stores the old size of the array
    playfieldsize.y += (y - playfieldsize.y) + INCREASE; // the new array size
    if (tmp = realloc(playfield, playfieldsize.y * sizeof(char *))) { // if storage can be allocated for the new size
        playfield = tmp;
        for (; cnt<playfieldsize.y; cnt++) { // for every new column a row is added
            char *tmp;
            printf("cnt=%lisize=%li\n", cnt, playfieldsize.y); // added for debugging purpose
            if (tmp = realloc(playfield[cnt], sizeof(char) * playfieldsize.x)) // segfault happens here
                playfield[cnt] = tmp;
            else
                die("Not enough initial memory");
        }
    } else // if storage could not be reallocated
        die("Not enough initial memory");
}

I’m however getting segmentation fault when the array is being accessed with a y value that is constantly being increased by one. This is what the program prints out:

...
cnt=327size=330
cnt=328size=330
cnt=329size=330
cnt=330size=360
cnt=331size=360
Segmentation fault

And I get this segfault when I in the beginning is accessing the array with a few <10 y values and then one of 301:

...
cnt=27size=30
cnt=28size=30
cnt=29size=30
cnt=30size=330
cnt=31size=330
Segmentation fault

So in the first example it initialized rows up until 331 before the error happens, and in the second it fails at 31. I can’t figure what is going on, it seems pretty random to me.

This is the entire program if needed: http://pastebin.com/13mRDh8A

  • 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-05-19T04:18:32+00:00Added an answer on May 19, 2026 at 4:18 am

    Your second realloc (for allocating new rows) should be malloc instead. At that point, playfield[cnt] contains uninitialized data, so trying to realloc it can cause a segfault.

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

Sidebar

Related Questions

No related questions found

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.