A question on pointers

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1059 Views - Last Post: 19 May 2010 - 07:21 AM Rate Topic: -----

#1 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

A question on pointers

Posted 18 May 2010 - 12:24 PM

Here's a small problem I'm having in my project. Say I have a function f1 which is something like this

char f1(double arg1, int n, struct mytype *arg2)
{
   //some code here
   arg2 = (struct mytype*)malloc((size_t)(n*sizeof(struct mytype));
   //some code here
   return 0;
}



and my main function is like this
int main()
{
   struct mytype *var1;
   char c;
   double d = 20;
   int n = 10;
   //some code here
   c = f1(d, n, var1);
   //some code here;
   return 0;
}



Now running this code (in Visual Studio 2010) caused a run time error because I passed the variable var1 into the function without initializing it. However, I want f1 to allocate memory to it. This version of the program works:

char f1(double arg1, int n, struct mytype *arg2)
{
   //some code here
   return 0;
}



int main()
{
   struct mytype *var1;
   char c;
   double d = 20;
   int n = 10;
   //some code here
   arg2 = (struct mytype*)malloc((size_t)(n*sizeof(struct mytype));
   c = f1(d, n, var1);
   //some code here;
   return 0;
}



But is it possible to allocate memory to a pointer the way I want to like in the first case?
Thanks in advance :)

This post has been edited by Louisda16th: 18 May 2010 - 12:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: A question on pointers

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: A question on pointers

Posted 18 May 2010 - 12:53 PM

Since you want to modify the pointer itself, you need to pass a pointer reference, example:

#include <iostream>
#include <cstdlib>
using namespace std;

struct test{
	int data;
};

void function(struct test*& var){
	var = (struct test*)malloc((size_t)(sizeof(struct test)));
	var->data = 5;
	return;
}

int main(){
	struct test* one;
	function(one);
	cout << one->data << endl;
	free(one);
	return 0;
}




When you pass a pointer, the thing it points at can be changed, but the pointer itself is passed by value, i.e. this results in an access violation:

#include <iostream>
#include <cstdlib>
using namespace std;

struct test{
	int data;
};

void function(struct test* var){
	var = (struct test*)malloc((size_t)(sizeof(struct test)));
	var->data = 5;
	return;
}

int main(){
	struct test* one;
	function(one);
	cout << one->data << endl; //the compiler will scream at you here
	free(one);
	return 0;
}


Was This Post Helpful? 2
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: A question on pointers

Posted 18 May 2010 - 12:56 PM

I forgot to add that I'm using C and not C++. How do I do this in C? Will using a pointer to a pointer work?
Was This Post Helpful? 0
  • +
  • -

#4 Delta62  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 19-February 09

Re: A question on pointers

Posted 18 May 2010 - 12:56 PM

First off, when you have n elements you want to allocate, you can use the calloc() function.

The problem that you're having is that you are allocating the space, then returning zero. If you want to use that allocated space in an outer function, return the pointer that malloc gives back to you, like so:

struct mytype *f1(double arg1, int n)
{
   struct mytype *arg2;
   //some code here
   arg2 = (struct mytype*)malloc((size_t)(n*sizeof(struct mytype));
   //some code here
   return arg2;
}



And yes, this will work perfectly fine in C.
Was This Post Helpful? 2
  • +
  • -

#5 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: A question on pointers

Posted 18 May 2010 - 01:00 PM

The purpose of the function isn't just allocating memory to the pointer. I'm passing the pointer as an argument because I need to do more with it. The struct is actually a matrix plus a few more variables to store pixel information of an image. I need to create an array of these structs and then fill it with the pixel data of say 'n' images. I thought of creating one function that does both - allocating memory and getting the pixel information.
Was This Post Helpful? 1
  • +
  • -

#6 Delta62  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 19-February 09

Re: A question on pointers

Posted 18 May 2010 - 01:08 PM

Then you should just do the operations on the struct after you do the allocation.

struct mytype *f1(double arg1, int n)
{
   struct mytype *arg2;

   arg2 = (struct mytype*)malloc((size_t)(n*sizeof(struct mytype));
   //change members of arg2 here
   //[Example] arg2->foo = arg1;
   return arg2;
}

Was This Post Helpful? 1
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: A question on pointers

Posted 18 May 2010 - 01:08 PM

Quote

The purpose of the function isn't just allocating memory to the pointer.
But if it's the very first thing it does, passing in a pointer may not be meaningful. Why not just return a valid pointer on success, and NULL on failure?

Quote

I thought of creating one function that does both - allocating memory and getting the pixel information.
Be consistent, and pick a certain mode of approach. Either memory allocation is handled by the library, or it's handled by the caller, and your functions operate on the allocated memory. From an engineering standpoint, I prefer the latter approach.
Was This Post Helpful? 2
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: A question on pointers

Posted 18 May 2010 - 01:10 PM

View PostLouisda16th, on 18 May 2010 - 02:00 PM, said:

The purpose of the function isn't just allocating memory to the pointer. I'm passing the pointer as an argument because I need to do more with it. The struct is actually a matrix plus a few more variables to store pixel information of an image. I need to create an array of these structs and then fill it with the pixel data of say 'n' images. I thought of creating one function that does both - allocating memory and getting the pixel information.


Edit: see above ... byOler1s

This post has been edited by David W: 18 May 2010 - 01:15 PM

Was This Post Helpful? 1
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: A question on pointers

Posted 18 May 2010 - 01:11 PM

That's why I didn't suggest returning the handle from the malloc call [since it was apparent there was other stuff that you were going to do in the function.]


C version:

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

struct test{
	int data;
};

void function(struct test** var){
	*var = (struct test*)malloc((size_t)(sizeof(struct test)));
	(*var)->data = 5;
	return;
}

int main(){
	struct test* one;
	function(&one);
	printf("%d\n", one->data);
	free(one);
	return 0;
}




As Oler1s pointed out, responsibility shifts from the function doing the allocation to main() (or another function depending on how you have your code set up), so as long as the delegation of authority for allocated memory is consistent (particularly the clean up), you can do it however you like.
Was This Post Helpful? 2
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: A question on pointers

Posted 18 May 2010 - 01:23 PM

I'm still having problems. Not sure what I'm doing wrong. :( So here's my code:
char get_input(const char *imglist[], const char *pointslist[], int n, struct imgsamples **trainingp)
{
	char c;
	int i;
	*trainingp = (struct imgsamples*)malloc((size_t)(sizeof(struct imgsamples)*n));
	for(i = 0; i<n; i++)
	{
		c = get_bmp_matrix(imglist[i], &trainingp[i]->img );
		c = get_landmark_coordinates(pointslist[i], trainingp[i]);
	}

	if(c!=0)
		return -1;
	else
		return 0;
}



int main()
{
	struct imgsamples *training;
	struct imgsamples testimg;
	struct imgsamples **trainingp;
	
	int i, j;
	FILE *fp;
	int n;
	char c;
	struct model facemodel;
	char *imglist[] =	{"1.bmp", "2.bmp", "3.bmp", "4.bmp", "5.bmp", "6.bmp", "7.bmp", "8.bmp", "9.bmp", "10.bmp"};
	char *pointslist[] = {"1.pts", "2.pts", "3.pts", "4.pts", "5.pts", "6.pts", "7.pts", "8.pts", "9.pts", "10.pts",};
	char *testfile = "21.bmp";
	n = 10;
	/*Get the training samples and save them in 'training'*/

	trainingp = &training;
	c = get_input(imglist, pointslist, n, trainingp);

	/*rest of the code*/
	
	system("PAUSE");
		

	return 0;
}



Here are the definitions of get_bmp_matrix and get_landmark_coordinates
char get_bmp_matrix(const char *filename, struct bmpmatrix *face);
char get_landmark_coordinates(const char *filename, struct imgsamples *img);



and the struct is defined like this

struct imgsamples
{
	double *x,*y;
	int n;
	struct bmpmatrix img;
};


where bmpmatrix is:
struct bmpmatrix
{
    int width, height;
    double **pixel_matrix;
};



The char return types are basically for signaling any errors (will work on it later). So what am I missing?

PS- In the structs, those pointers and pointer to pointers have been used like in Numerical Recipes in C. That book shows a nice way to represent matrices and vectors :)

This post has been edited by Louisda16th: 18 May 2010 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#11 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: A question on pointers

Posted 18 May 2010 - 01:27 PM

Quote

That's why I didn't suggest returning the handle from the malloc call [since it was apparent there was other stuff that you were going to do in the function.]


? ... It does not really make any difference how you return the address to the new memory ... just pick a way that works for what you need.

Recalling the discussion of realloc awhile ago here at DIC ... where a pointer was passed in ... (not a pointer to a pointer) .... and the new address (or old, if unchanged) was returned ...

The need there ... was for both addresses to be available AFTER the new realloc failed ... and THEN ... returned a NULL ... thus ... just a pointer was passed in (not a ref. pointer)
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: A question on pointers

Posted 18 May 2010 - 02:05 PM

WTH do you have all this casting going on if you're writing in C???
Was This Post Helpful? 0
  • +
  • -

#13 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: A question on pointers

Posted 18 May 2010 - 02:08 PM

I didn't get you.
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: A question on pointers

Posted 18 May 2010 - 02:10 PM

All this casting:
*trainingp = (struct imgsamples*)malloc((size_t)(sizeof(struct imgsamples)*n));

is UNNECESSARY in C unless you've forgotten to include stdlib.h or you're compiling as C++.
Was This Post Helpful? 1
  • +
  • -

#15 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: A question on pointers

Posted 18 May 2010 - 02:15 PM

Okay. So I changed it to
*trainingp = (struct imgsamples*)malloc((sizeof(struct imgsamples)*n));



However it still doesn't solve the problem. In the for loop of the get_input function, at the second iteration, (i = 1), neither of the two functions - get_bmp_matrix and get_landmark_coordinates is able to work with the variable trainingp. I get this message "Unhandled exception at 0x00fb323b in Active Shape Models.exe: 0xC0000005: Access violation writing location 0xccccccd8." Not sure what I'm missing here.

EDIT: typo

This post has been edited by Louisda16th: 18 May 2010 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2