C Dynamic Memory Problem

Joined
Jan 10, 2007
Messages
43,898
Location
In The Machine
I need to pass a pointer to a function that points to a dynamic memory allocation while freeing the memory allocated to it and changing the value of the pointer outside the function so that it points to a new dynamic memory allocation, so I pass a pointer to the pointer to the function like this:

void example_function (int **ptr_to_ptr, ...)
{


...

free (*ptr_to_ptr);

}

The free call produces an error in Visual Studio 2005 Standard, so this does not work, but do the nature of my program, I need to do this and I have tried a few variations on it.

I have two functions that do this, but I managed to hack a workaround by doing for one of them:

void example_function (int **ptr_to_ptr, ...)
{

int * temp = *ptr_to_ptr;


...

free (temp);

}

The two functions are virtually identical, so the fact that I get an error doing this in one and no error doing this in the other is bewildering. The one that produces an error with this workaround is a form of the one that does not, optimized for a special case of it, so I can substitute the one for this this workaround works for the one for which this work around does not work. If I do that, I get the same error.

Here is the code to my program:

#include
#include

void matMul ( unsigned int **a, unsigned int **b );
void matSqr ( unsigned int **a );
unsigned int * matPow ( unsigned int * a, unsigned int n );

#define matCopy(a, b) *a = *b, *(a + 1) = *(b + 1),*** *(a + 2) = *(b + 2), *(a + 3) = *(b + 3)

int main ( void )
{

*** unsigned int a[] = { 1, 1, 1, 0 }, *b, n = 1;

*** while ( n > 0 )
*** {

*** *** printf("Compute Fibonacci: ");

*** *** scanf("%u", &n);

*** *** if ( n < 2 )
*** *** {

*** *** *** printf("\n\nFibonacci %u is %u\n\n", n, n);

*** *** *** continue;

*** *** }

*** *** b = matPow(a, n - 2);

*** *** printf("\nFibonacci %u is %u\n\n", n, *b);

*** *** free(b);

*** }

*** return 0;

}

void matMul ( unsigned int **a, unsigned int **b )
{

*** unsigned int *c = (unsigned int*)malloc (sizeof(unsigned int) 0; --n )
{

temp = b;
b = b + a;
a = b;

}

//give the user b here

}

I wrote the above program because I was curious as to which approach has the lower computational time. The reason I am wrote the series of matrix multiplications in the manner I did is because I was inspired by a function I wrote for another program that does fast exponentiation:

long long int pow (long long int base, long long int exp)
{

*** long long int result = (exp & 1) ? base : 1;

*** if (base == 2)
*** {

*** *** return 1 >= 1 ; exp > 0 ; exp >>= 1 )
*** {

*** *** base *= base;

*** *** if (exp & 1)
*** *** {

*** *** *** result *= base;

*** *** }

*** }

*** return result;

}

Also, does anyone know if there are any instructions available on an Intel Core 2 Duo Conroe processor that can be used to do matrix multiplication or at least accelerate it? Provided that they exist, I do not plan to use them right now, as I am not familiar with assembly yet, but since I found a use for matrix multiplication in a program, I would like to know if there are instructions that can be used to accelerate matrix multiplications for future reference.


More...

View All Our Microsft Related Feeds
 
Back
Top