Reputation: 13
I have a function1
where I allocate an array with a specific number of bytes each time.
When I use printf
I can see that my data is loaded on this array.
function1 (other arguments, CvPoint2D32f* array1)
Then there is a function2
in which I call function1
and assign the array1 data to another variable (array2).
function2(other stuff, CvPoint2D32f* array2)
When I use printf
I can see that I cannot access my data. The program prints 0.000..
function2
is called by my main program in which I assign the data to a third variable...
I search and I search but cannot find what is wrong.
Passing the pointer is not enough already?
Help, if I was not specific enough, ask me:
function1(IplImage* grey, IplImage* image, CvPoint2D32f *array1){
int array_counter = cvFindContours(grey, storage_color, &colorcontoursizeof(CvContour),
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
array1 = (CvPoint2D32f*)malloc(array_counter*sizeof(CvPoint2D32f))
if (array_counter == 0){
printf("Error allocating memory or Just a colour missing!!!\n\n\n");
free(Man);
}
cvReleaseMemStorage(&storage_color); // Releasing memory.
free(Man);
return array_counter;
}
int function2(IplImage* image, CvPoint2D32f *array2, other stuff){
...other stuff...
// Find the contours for the different colors.
r_counter = function1(image1, image2, array2);
return 0;
}
EDIT =======================================
I tried to do it before posting here but here is a problem I get.
I get an error when compiling at the line where I load data in the array1 about non-class type of CvPoint2D32f.
That is what I do at these lines where I get the error:
array1[cnt].x = (tpt1.x + tpt2.x)/2;
array1[cnt].y = (tpt1.y + tpt2.y)/2;
2nd EDIT =========================
both answers helped and were correct. Thanks to everybody! The problem is solved. I used double pointers to both the functions to export data from function1 to function2 and from function2 to main program. I used some temporary variables which helped a lot. It was less confusing.
Upvotes: 1
Views: 1000
Reputation: 43688
(Trying to explain C pointer passing semantics, bear with me a little).
C only has pass-by-value, which means that you cannot alter an argument to a function. So in:
function1(IplImage* grey, IplImage* image, CvPoint2D32f *array1){
....
array1 = (CvPoint2D32f*)malloc(array_counter*sizeof(CvPoint2D32f))
....
}
r_counter = function1(image1, image2, array2);
The change you made to array1
in function1
is not visible outside of that function (array1
is passed by value, not by reference).
To get around this, pass to the function a variable that contains a pointer to the variable you want to pass by reference:
function1(IplImage* grey, IplImage* image, CvPoint2D32f **array1){
....
*array1 = (CvPoint2D32f*)malloc(array_counter*sizeof(CvPoint2D32f))
....
}
r_counter = function1(image1, image2, &array2);
i.e. just add another level of indirection. See RFC 1925 truth 6a (It is always possible to add another level of indirection.).
Upvotes: 2
Reputation: 121619
Two scenarios:
// SCENARIO 1: Array allocated outside a function and passed into it
myfunc (char * buff)
{
buff[0] = 'A';
...
main ()
...
char *mybuffer = NULL;
if ((mybuffer = malloc (100))
myfunc (mybuffer);
...
.
// SCENARIO 2: Array allocated inside of a function and passed out from it
myfunc (char **buff)
{
*buff = malloc (100);
...
main ()
char *mybuffer = NULL;
myfunc (&mybuffer);
...
I think the problem is you probably need Scenario 2. Try "**pointer-to-pointer" syntax in your "function1()"
Upvotes: 2