LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with double pointers/matrix GCC (https://www.linuxquestions.org/questions/programming-9/problem-with-double-pointers-matrix-gcc-882959/)

Zaizeku 05-26-2011 11:51 PM

Problem with double pointers/matrix GCC
 
Hi guys I'm having a problem that I do not know nor have I found a way to solve. Let's say I have a 10x2 matrix, I create said matrix and fill it one column with zeros and the other with ones, now when I print the matrix I get this:

column 1:
0,0,0,0,0,0,0,0,1,1
column 2:
1,1,1,1,1,1,1,1,1,1

The first two rows of the second column are being replaced into the last two rows of the first column, now I even checked in visual studio and it works fine there. A friend tried my code and he gets it even worse:
column 1:
0,0,0,0,1,1,1,1,1,1
column 2:
1,1,1,1,1,1,1,1,1,1

As far as I've seen it must be a problem with GCC, unfortunately I need to have this up and running in GCC no matter what. Here I leave the test code. So if you guys have a solution to this I'm all ears.Thanks in Advance.

Code:

#include<stdio.h>
#include<stdlib.h>

int **NxN_Matrix(long rows, long columns);
int main()
{
        int **test;
        int i,j;

        test = NxN_Matrix(10,2);

        for(i = 0; i < 2; i++)
        {
                for(j = 0; j < 10; j++)
                {
                        test[i][j] = i;
                }
        }

        for(i = 0; i < 2; i++)
        {
                for(j = 0; j < 10; j++)
                {
                        printf("[%d]\n",test[i][j]);
                }

                printf("\n");
        }
       
        return 0;
}

int **NxN_Matrix(long rows, long columns)
{
    long i;
    int **matrix;

    matrix = (int**)calloc(rows,sizeof(int*));

    for(i = 0; i < rows; i++)
    {
        matrix[i] = (int*)calloc(columns, sizeof(int));
    }

    return (matrix);
}


markush 05-27-2011 03:01 AM

Hello Zaizeku,

on my system (Slackware64-13.37) I get with your code
Code:

markus@samsung:~/Programmierung/c$ gcc -o matrix matrix.c
markus@samsung:~/Programmierung/c$ ./matrix
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[0]
[1]
[1]

[1]
[1]
[1]
[1]
[1]
[1]
[1]
[1]
[1]
[1]

markus@samsung:~/Programmierung/c$

which seems to be the same as your output from Visual Studio.

Markus

Ramurd 05-27-2011 04:19 AM

To debug your code, I changed it a little bit:
This shows that the address assigned to test[0][8] is the same address as test[1][0], so it gets overwritten;

I need more coffee to see where the calculation goes wrong.
Code:

#include<stdio.h>
#include<stdlib.h>

int **NxN_Matrix(long rows, long columns);
int main()
{
        int **test;
        int i,j;

        test = NxN_Matrix(10,2);

        for(i = 0; i < 2; i++)
        {
                for(j = 0; j < 10; j++)
                {
                        test[i][j] = i;
                        printf("Assigned: test[%d][%d](%p)=%d; Value=%d\n",i,j,&test[i][j],i,test[i][j]);
                }
                printf("\n");
        }

        for(i = 0; i < 2; i++)
        {
                for(j = 0; j < 10; j++)
                {
                        printf("%d x %d = [%d]\n",i,j,test[i][j]);
                }

                printf("\n");
        }

        return 0;
}

int **NxN_Matrix(long rows, long columns)
{
    long i;
    int **matrix;

    matrix = (int**)calloc(rows,sizeof(int*));
    printf("matrix=%p\n",matrix);

    for(i = 0; i < rows; i++)
    {
        matrix[i] = (int*)calloc(columns, sizeof(int));
        printf("%p[%d]=%p\n",matrix,i,matrix[i]);
    }

    return (matrix);
}


markush 05-27-2011 04:55 AM

Hi,

I've changed your code to something more general.
Code:

#include<stdio.h>
#include<stdlib.h>

int **NxN_Matrix(int rows, int columns);
int main()
{
    int **test;
    int i,j;
  int r=12, c=3;

    test = NxN_Matrix(r,c);

    for(i = 0; i < c; i++)
    {
        for(j = 0; j < r; j++)
        {
            test[i][j] = i;
        }
    }

    for(i = 0; i < c; i++)
    {
        for(j = 0; j < r; j++)
        {
            printf("[%d]\n",test[i][j]);
        }

        printf("\n");
    }

    return 0;
}

int **NxN_Matrix(int rows, int columns)
{
    int i;
    int **matrix;

    matrix = (int**)calloc(columns, sizeof(int));

    for(i = 0; i < columns; i++)
    {
        matrix[i] = (int*)calloc(rows, sizeof(int));
    }

    return (matrix);
}

I've swaped rows and columns in the allocation part, now it works for me. It turns out, that your code works with rows up to a size of 8, but with longer rows you'll get wrong numbers on any position higher than 7.

Markus

dwhitney67 05-27-2011 05:49 AM

Based on the code in the OP, the matrix should be accessed using row, then column. After all, that is how it was setup. For example:
Code:

test[row][column]
Perhaps a diagram may enlighten the situation:
Code:

          Row      Columns
test --->  [0] ---> [0][1]
          [1] ---> [0][1]
          [2] ---> [0][1]
            .
            .
            .
          [9] ---> [0][1]

Thus code-wise, the 'test' matrix is setup as such:
Code:

for (int r = 0; r < rows; ++r)
{
  for (int c = 0; c < columns; ++c)
  {
      test[r][c] = c;
  }
}

Here's the complete code (using the C99 std):
Code:

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

int** NxN_Matrix(const int rows, const int columns)
{
  int** matrix = calloc(rows, sizeof(int*));

  assert(matrix != NULL);

  for (int r = 0; r < rows; ++r)
  {
      matrix[r] = calloc(columns, sizeof(int));

      assert(matrix[r] != NULL);
  }

  return matrix;
}

void NxN_Matrix_destroy(int** matrix, const int rows)
{
  for (int r = 0; r < rows; ++r)
  {
      free(matrix[r]);
  }

  free(matrix);
}

int main()
{
  const int rows = 10;
  const int cols = 2;

  int** test = NxN_Matrix(rows, cols);

  for (int r = 0; r < rows; ++r)
  {
      for (int c = 0; c < cols; ++c)
      {
        test[r][c] = c;

        printf("[%d]\t", test[r][c]);
      }

      printf("\n");
  }

  NxN_Matrix_destroy(test, rows);

  return 0;
}

To build:
Code:

gcc -Wall -pedantic -std=c99 matrix.c -o matrix

Zaizeku 05-27-2011 11:28 AM

Thanks everyone I guess I was too sleepy to notice that I swapped the columns with the rows by mistake, thank you all who responded now it works.

MTK358 05-27-2011 11:34 AM

Mark the thread as solved.


All times are GMT -5. The time now is 09:58 AM.