GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Apr-2005, 13:26
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road

Pointer Problem


Hi all,

I've got a function :
CPP / C++ / C Code:
void padMatrix (int **A, int n) // overwrites A with a padded matrix.
{
	int i, j;
	int **R;
	
	R = malloc((n+1)*sizeof(int *));

	for (i = 0; i < n+1; i++)
		R[i] = malloc((n+1)*sizeof(int *));
	
	for (i = 0; i < n+1; i++)
		for (j = 0; j < n+1; j++)
		{
			if (i == n)
				R[i][j] = 0;
			else if (j == n)
				R[i][j] = 0;
			else
				R[i][j] = A[i][j];
			
		}
	free(A); 
	A=R; // overwrites A
}

But when i call it in main:
CPP / C++ / C Code:
padMatrix(A, n);
padMatrix(B, n);

Both A and B get assigned to the same address. Why is this so, don;t the malloc in padMatrix assign to diff address each time I call it? And, how can I overwrite the 2-D array in padMatrix propoerly using passing of ref and not copying value to value?

Many thanks.
  #2  
Old 14-Apr-2005, 15:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nusstu
Hi all,


If you want to change the value of A in the function padMatrix and you want that value to be transferred back to the main function, there are a couple of ways. Since A is a pointer-to-pointer to int, you can do either of these:

1. Make padMatrix return a pointer-to-pointer to int, and return whatever you want to be the new value of A.

2. Make the function argument be a pointer to A, and work with it in the function. (So the argument is a pointer-to-pointer-to-pointer to int).


An additional note:
Be careful of how you free A in the function. Didn't you allocate storage for it in the way that you allocated storage for R in the function?

That is, shouldn't you do something like this to free storage for the old matrix:

Code:
free A[i] for i = 0..n free A

Otherwise you will have a memory leak.

Regards,

Dave
  #3  
Old 14-Apr-2005, 15:45
rdrast rdrast is offline
New Member
 
Join Date: Apr 2005
Posts: 22
rdrast is on a distinguished road
In addition to Dave's post above, something else to watch out for...

In your padMatix function, it seems you can expand the boundries, but if you do, you are going to have a problem here:

Code:
if (i == n) R[i][j] = 0; else if (j == n) R[i][j] = 0; else R[i][j] = A[i][j];

If you originally had a 3x3 matrix, and expanded to a 7x7 one, you are going to have problems with A[5][5] in your copy...

And my personal preference, regarding Dave's solution, is return an int** from padMatrix. Too much indirection is painful!
  #4  
Old 15-Apr-2005, 05:39
nusstu nusstu is offline
New Member
 
Join Date: Mar 2004
Posts: 27
nusstu is on a distinguished road
Thanks guys for ur replies.

Well I did what rdrast said, but the thing is these matrices are huge of order of
thousands of integers that's why I wanna overwrite . Just wondering if I use "triple" pointers do i pass the address of A in as in:
CPP / C++ / C Code:
padMatrix(&A, n);
  #5  
Old 15-Apr-2005, 11:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by nusstu
Thanks guys for ur replies.

Well I did what rdrast said, but the thing is these matrices are huge of order of
thousands of integers that's why I wanna overwrite . Just wondering if I use "triple" pointers do i pass the address of A in as in:
CPP / C++ / C Code:
padMatrix(&A, n);

Yes. The concept of pointer-to-pointer-to-pointer is a little daunting for some of us, but if you really want learn how to do it this way, you could try something like this:

CPP / C++ / C Code:

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

int main()
{
  void padMatrix (int ***A, int n);

  int **x;
  
  /* do stuff with nxn matrix x */

 
  padMatrix(&x, n);
  n = n+1; 

  /* do stuff with padded nxn matrix x --- n is one greater than the original */


  for (i = 0; i < n; i++) { /* n is the dimension of the padded matrix */
    free(x[i]);
  }
  free(x);
  
  return 0;
}

/* 
 * This assumes the input matrix was nxn, and the new matrix
 * will be (n+1)x(n+1)
 */
void padMatrix (int ***A, int n)
  int i, j;
  int **R;
  
  R = malloc((n+1)*sizeof(int *)); /* allocate n+1 rows */

  /* you should probably check return value from malloc(), to make
   * sure that you don't run out of memory.
   */

  for (i = 0; i < n+1; i++) {
    R[i] = malloc((n+1)*sizeof(int)); /* for each row, allocate n+1 ints */
  }

  
  for (i = 0; i < n+1; i++) {
    for (j = 0; j < n+1; j++) {
      if ((i == n) || (j == n)) {
        R[i][j] = 0;
      }
      else {
        R[i][j] = (*A)[i][j];
      }
      
    }
  }

  for (i = 0; i < n; i++) {
    free((*A)[i]);
  }
  free(*A); 
  *A = R;
}


You could simplify the notation by using a typedef or by using a different variable in padMatrix(), but the above code will work as written. Be careful with the notation: (*A) is used to dereference A. Also, make sure that the matrix you represent with the first call padMatrix(&xxx, n) is an nxn matrix, dynamically allocated (with malloc) from a pointer-to-pointer to int.

Regards,

Dave
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D arrays:dynamic allocation and freeing bravetanveer C Programming Language 48 27-Nov-2007 15:55
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
Please help! Newbie pointer problem! robsmith C Programming Language 5 12-Mar-2005 04:48
C Pointer problem fwongmc C Programming Language 8 04-Dec-2004 12:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.