Solved

adding an integer to a pointer while creating it (pointer).

Posted on 2006-04-10
222 Views
Can someone please explain me what the folowing code is doing ? I have a problem at , why the pointer is being subtracted from the initial value while returning it. What does it achieve ? And the same (being added) while freeing it.

/*
* Allocates memory for an double vector from [beg...end]
*/
double  *Dec_Vector(int beg, int end)
{
double       *vec;

if( (end - beg +1) <= 0)
{printf("In Dec_Vector, Chk the parameters\n");exit(1); }

vec = (double *)malloc((end-beg+1)*sizeof(double));
if (vec == NULL) Abort("Error allocating memory in Dec_vector, Aborting ...\n");
return(vec-beg);

}

/*
* Free a dec vector
*/
void Free_Dec(double *vec, int beg, int end)
{
free(vec+beg);
}
For a 2D array ....

/*
* Allocate memory for a double matrix [begrow...endrow][begcol...endcol]
*/
double  **Dec_Matrix(int begrow, int endrow, int begcol, int endcol)
{
double       **mat;
int             i;

mat = (double **)malloc( (endrow-begrow+1)*sizeof(double*));
if (mat == NULL) Abort("Error allocating memory in Dec_Matrix, Aborting ...\n");
mat -= begrow;
for (i=begrow; i<=endrow; i++)
{
mat[i] = (double *)malloc((endcol-begcol+1)*sizeof(double));
if (mat[i] == NULL) Abort("Error allocating memory in Dec_Matrix, Aborting ...\n");
mat[i] -= begcol;
}
return(mat);

}

/*
* Free a Dec matrix
*/
void Free_Decmatrix(double **mat, int begrow, int endrow, int begcol, int endcol)
{
int             i;

for(i=begrow; i<=endrow; i++)
free(mat[i]+begcol);
free(mat+begrow);

}

And the array is referenced in the main program as below with their starting int values....
*aa = Dec_Matrix(1,nclone,0,nprobe);
for (i=1; i<=nclone; i++)
for (j=0; j<=nprobe; j++)
----- here goes some code

I  dont understand that why not just return the array without subtracting and indices references be just from 0 to its length...

thanks a lot for explaing this code snippet ..

Susanta
0
Question by:code4susant

LVL 48

Accepted Solution

Maybe this is done to use it by such way:

double* d = Dec_Vector(10, 20);

d[10] = 1.0;
d[11] = 2.0;
...
d[20] = 0.0;

If beg > 0, Dec_Vector returns pointer to incorrect memory location which should not be accessed directly. But using this pointer in index in  (beg, end) range is OK.
0

LVL 14

Assisted Solution

AlexFM is correct

as you know d[10] is equivalent to *(d+10)

it just forces you to access the vector/matrix elements by indices in the specified range
0

LVL 16

Assisted Solution

Hi code4susant,

Although I agree that this is a strange technique, there are some neat uses of it.

double v = Dec_Vector(-1,1);
int i;

for ( i = -1; i <= 1; i++ ) v[i] = ...;

See how it turns the sometimes unnatural need to index from 0 upwards to a much more natural walk through using a range that feels right?

Paul
0

LVL 16

Expert Comment

Hi code4susant,

Better still, say you are coding a Tic-Tac-Toe game.

double  ** board = Dec_Matrix ( -1, 1, -1, 1 );

You can now use board[-1][-1] to access one corner and board[1][1] for the opposite corner. That feels far more natural to me that the two corners being board[0][0] and board[2][2]!

Paul
0

Author Comment

Thank you all for such a prompt reply !  This is life at its best !

AflexFM hit the point instantly,hoomany spelled it out and thank you Paul for motivating its use . Overall , it was an excellent reply !

thanks again,
bye,
code4susant
0

Featured Post

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.