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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:13:49+00:00 2026-05-25T03:13:49+00:00

While starting on a program for getting a matrix’s Reduced Row Echelon Form, I

  • 0

While starting on a program for getting a matrix’s Reduced Row Echelon Form, I transferred my code for printing the array into its own function. As soon as I did this, I got a segfault. What puzzled me though, was the fact that an unrelated variable declaration (commented below) solved the segfault.

    #include <stdio.h>


int COLS = 3;
int ROWS = 3;

void PrintArray(int array[][COLS]);

int main (int argc, char**argv) {

   int i, ii = 0;

   FILE *file;

   file = fopen(argv[1], "r");

   int array[ROWS][COLS];

   fscanf(file, "%d %d", &ROWS, &COLS);

   while (!feof(file))
   {
      fscanf(file, "%d", &array[i][ii]);
      ii++;
      if (fgetc(file) == '\n') {
         i++;
         ii = 0;
      }
   }
   int j = 0, k = 0; //This solved the segfault.
   PrintArray(array);
   printf("\n");

   fclose(file);
   return 0;
}

void PrintArray(int array[][COLS]) //The printing function
{
   int j, k;
   for (j = 0; j < ROWS; j++)
      {
         for (k = 0; k < COLS; k++)
         {
            printf("%d", array[j][k]);
         }
         printf("\n");
      }
}

After a couple hours of debugging, I eventually figured out that it may have had something to do with the scope of the variables within the for loop.

To illustrate:

int COLS = 3;
int ROWS = 3;
int a; //declared globally

//main

   for (a = 0; a < ROWS; a++) {
      printf("for loop");
   }

works, but as soon as I declare “a” in main:

int COLS = 3;
int ROWS = 3;


//main
   int a; //declared in main
   for (a = 0; a < ROWS; a++) {
      printf("for loop");
   }

it doesn’t work.

Also, if I replace the global variables with numbers, I still get a segfault, until I remove the line which originally fixed the segfault!

void PrintArray(int array[][3]) //COLS
{
   int j = 0, k = 0;
   for (j = 0; j < 3; j++) //ROWS
      {
         for (k = 0; k < 3; k++) //COLS
         {
            printf("%d", array[j][k]);
         }
         printf("\n");
      }
}

This appears to be as far as I can get to understanding the problem, so your help would be appreciated.

  • 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-25T03:13:50+00:00Added an answer on May 25, 2026 at 3:13 am

    You are getting out of array bounds. The two extra variables lay right after the array on the stack, so you begin corrupting them instead of something else, that’s why segfault is “solved” (it’s not solved, of course, the bug is still there).

    There are severe problems with that code:

    • Variable i is used without being initialized
    • The array size is always 3×3. Reading new values for ROWS and COLS does NOT resize the array. If e.g. you have read ROWS=4 and COLS=4 from file, you will corrupt the memory outside of that allocated for array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a timeout error while starting my Windows service. I am tring
I'm starting to get into Unit Testing, Dependancy Injection and all that jazz while
I have been learning python for some time now. While starting this learning python
I'm having trouble starting a transaction with Hibernate and MySQL while running in JUnit.
I'm starting with Ruby, and while making some test samples, I've stumbled against an
I am starting with google maps. just learning. while ago, it was working. now
I'm only starting with Android dev, and while the Milestone is a nice device
I've been working with python for a while now and am just starting to
I'm starting to look into the whole world of RJS and Prototype/jQuery in Rails
I have a program that simulates best-fit memory management. Basically, while there are available

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.