3 Replies - 3023 Views - Last Post: 01 August 2009 - 02:24 AM Rate Topic: -----

#1 shahidpucit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-July 09

Required Help on double pointer memory allocation

Post icon  Posted 31 July 2009 - 11:42 PM

salam,
i have write a code to dynamically allocate memory to a double pointer.i passed to function addresse to a triple pointer but memory is not allocated. the following is a code

int width=3,height= 3;
int main()
{
	int **mat=0;
	memoryallocate(&mat);
}
void memoryallocate( int ***mt)
{
   int i,j;
	mt=(int***)malloc( sizeof(int) );
	for(i=0;i<width;i++)
	{
		mt[i]=(int** )malloc(n* sizeof(int) );
	for(j=0;j<hieght;j++)
	{
		mat[i][j]=(int* ) malloc(m*sizeof( int));
	}
	}
}


the memeory is not allocated to mat double pointer int the main function.plz help me where i was wrong plz reply as soons as posssible

Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Required Help on double pointer memory allocation

#2 Guest_Neumann*


Reputation:

Re: Required Help on double pointer memory allocation

Posted 31 July 2009 - 11:52 PM

What you want to accomplish requires a solid understanding of pointers. Make sure you study this topic in a good amount of detail, because it is crucial.

I think it's better to put "width" and "height" as arguments to the function. This is what I came up with:

void alloc(int ***ptr, int width, int height) {
  int i;
  *ptr = (int**)malloc(sizeof(int*)*height);
  for (i = 0; i < width; i++) {
	(*ptr)[i] = (int*)calloc(width, sizeof(int));
  }
}



Edit: By the way, in the row initialization I have used calloc() instead of malloc() in order to ensure that everything is initialized to zero.

This post has been edited by Neumann: 01 August 2009 - 12:11 AM

Was This Post Helpful? 1

#3 shahidpucit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-July 09

Re: Required Help on double pointer memory allocation

Posted 01 August 2009 - 01:00 AM

thanks i will check it
Was This Post Helpful? 0
  • +
  • -

#4 shahidpucit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 31-July 09

Re: Required Help on double pointer memory allocation

Posted 01 August 2009 - 02:24 AM

Thanks for your help i have s=uccesfully allocated memory.but when i copy the values into a function and then print these values using function then the NULL pointer assignment Errors Appreas the code is given below.

void fun(int***,int,int);
void assignvalues(int***m);
void printValues(int***);

int main()
{
	int **m=0;


	clrscr();
	fun(&m,3,3);
	assignvalues(&m);

	printValues(&m);


	getch();

	return 0;

}

void printValues(int***mt)
{
   int i,j;
   for(i=0;i<3;i++)
   {
		for(j=0;j<3;j++)
		{
			printf("%d\t",mt[0][i][j]);
		}
		printf("\n");
   }
}

void assignvalues(int***mt)
{
	int i,j;
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			mt[0][i][j]=i;
		}
	}
}

void fun(int ***mt,int w,int h)
{
	int i=0;
	*mt=(int**)malloc(sizeof(int)*h);
	for(i=0;i<w;i++)
	{
		*mt[i]=(int*)malloc(sizeof(int)*w);
	}

}




View PostNeumann, on 31 Jul, 2009 - 10:52 PM, said:

What you want to accomplish requires a solid understanding of pointers. Make sure you study this topic in a good amount of detail, because it is crucial.

I think it's better to put "width" and "height" as arguments to the function. This is what I came up with:

void alloc(int ***ptr, int width, int height) {
  int i;
  *ptr = (int**)malloc(sizeof(int*)*height);
  for (i = 0; i < width; i++) {
	(*ptr)[i] = (int*)calloc(width, sizeof(int));
  }
}



Edit: By the way, in the row initialization I have used calloc() instead of malloc() in order to ensure that everything is initialized to zero.


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 01 August 2009 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1