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

Posted on 2006-04-10
Medium Priority
Last Modified: 2010-04-15
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");

 * Free a dec vector
void Free_Dec(double *vec, int beg, int end)
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;

 * 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++)


 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 ..

Question by:code4susant
LVL 48

Accepted Solution

AlexFM earned 1200 total points
ID: 16420029
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.
LVL 14

Assisted Solution

hoomanv earned 400 total points
ID: 16420849
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
LVL 16

Assisted Solution

PaulCaswell earned 400 total points
ID: 16420916
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?

LVL 16

Expert Comment

ID: 16420955
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]!


Author Comment

ID: 16421436
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,

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question