LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-26-2011, 11:51 PM   #1
Zaizeku
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
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);
}

Last edited by Zaizeku; 05-26-2011 at 11:54 PM.
 
Old 05-27-2011, 03:01 AM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
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
 
Old 05-27-2011, 04:19 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 555

Rep: Reputation: 75
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);
}
 
Old 05-27-2011, 04:55 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
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
 
Old 05-27-2011, 05:49 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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

Last edited by dwhitney67; 05-27-2011 at 05:53 AM.
 
1 members found this post helpful.
Old 05-27-2011, 11:28 AM   #6
Zaizeku
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Original Poster
Rep: Reputation: 0
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.

Last edited by Zaizeku; 05-27-2011 at 11:40 AM.
 
Old 05-27-2011, 11:34 AM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Mark the thread as solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double pointers and two dimensional arrays in C kponenation Programming 23 03-16-2011 11:43 AM
Warning in GCC while using pointers njanani Programming 5 06-29-2007 02:59 AM
Assigning a short to a double. Differences between gcc-2.95 and gcc-3.4.x _john_i_ Programming 2 01-12-2007 01:11 PM
Matrix of function pointers kvtournh Programming 9 03-20-2006 04:38 AM
double matrix array in c alaios Programming 3 09-15-2005 11:34 AM


All times are GMT -5. The time now is 09:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration