Home arrow C programming arrow A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 3

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 3 PDF Print E-mail
Written by Hemanshu   
Saturday, 29 December 2007
Article Index
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 3
Page 2
Page 3
Page 4
Page 5

METHOD 4:


In this method we allocate a block of memory to hold the whole array first. We then create an array of pointers to point to each row. Thus even though the array of pointers is being used, the actual array in memory is contiguous. The code looks like this:


----------------- Program 9.3 -----------------------------------
/* Program 9.3 from PTRTUT10.HTM              6/13/97 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
      int **rptr;
      int *aptr;
      int *testptr;
      int k;
      int nrows = 5;          /* Both nrows and ncols could be evaluated */
      int ncols = 8;         /* or read in at run time */
      int row, col;
      /* we now allocate the memory for the array */
      aptr = malloc(nrows * ncols * sizeof(int));
      if (aptr == NULL)
      {
            puts("\nFailure to allocate room for the array");
            exit(0);
      }
      /* next we allocate room for the pointers to the rows */
      rptr = malloc(nrows * sizeof(int *));
      if (rptr == NULL)
      {
            puts("\nFailure to allocate room for pointers");
            exit(0);
      }
    /* and now we 'point' the pointers */
    for (k = 0; k < nrows; k++)
    {
         rptr[k] = aptr + (k * ncols);
    }
    /* Now we illustrate how the row pointers are incremented */
    printf("\n\nIllustrating how row pointers are incremented");
    printf("\n\nIndex        Pointer(hex) Diff.(dec)");
    for (row = 0; row < nrows; row++)
    {
         printf("\n%d               %p", row, rptr[row]);
         if (row > 0)
         printf("                     %d",(rptr[row] - rptr[row-1]));
    }
    printf("\n\nAnd now we print out the array\n");
    for (row = 0; row < nrows; row++)
    {
         for (col = 0; col < ncols; col++)
         {
              rptr[row][col] = row + col;
              printf("%d ", rptr[row][col]);
         }
         putchar('\n');
    }
    puts("\n");
    /* and here we illustrate that we are, in fact, dealing with
        a 2 dimensional array in a contiguous block of memory. */
    printf("And now we demonstrate that they are contiguous in
memory\n");
    testptr = aptr;
    for (row = 0; row < nrows; row++)
    {
         for (col = 0; col < ncols; col++)
         {
              printf("%d ", *(testptr++));
         }
         putchar('\n');
    }
    return 0;
}
------------- End Program 9.3 -----------------


Consider again, the number of calls to malloc()
    To get room for the array itself                1      call
    To get room for the array of ptrs             1      call
                                                ----
                              Total                                  2      calls


Now, each call to malloc() creates additional space overhead since malloc() is generally implemented by the operating system forming a linked list which contains data concerning the size of the block. But, more importantly, with large arrays (several hundred rows) keeping track of what needs to be freed when the time comes can be more
cumbersome. This, combined with the contiguousness of the data block that permits initialization to all zeroes using memset() would seem to make the second alternative the preferred one.
As a final example on multidimensional arrays we will illustrate the dynamic allocation of a three dimensional array. This example will illustrate one more thing to watch when doing this kind of allocation. For reasons cited above we will use the approach outlined in alternative two. Consider the following code:


------------------- Program 9.4 -------------------------------------
/* Program 9.4 from PTRTUT10.HTM             6/13/97 */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int X_DIM=16;
int Y_DIM=5;
int Z_DIM=3;
int main(void)
{
      char *space;
      char ***Arr3D;
      int y, z;
      ptrdiff_t diff;
      /* first we set aside space for the array itself */
      space = malloc(X_DIM * Y_DIM * Z_DIM * sizeof(char));
      /* next we allocate space of an array of pointers, each
          to eventually point to the first element of a
          2 dimensional array of pointers to pointers */
      Arr3D = malloc(Z_DIM * sizeof(char **));
      /* and for each of these we assign a pointer to a newly
          allocated array of pointers to a row */
      for (z = 0; z < Z_DIM; z++)
      {
            Arr3D[z] = malloc(Y_DIM * sizeof(char *));
            /* and for each space in this array we put a pointer to
                the first element of each row in the array space
                originally allocated */
          for (y = 0; y < Y_DIM; y++)
          {
               Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM);
          }
      }
      /* And, now we check each address in our 3D array to see if
         the indexing of the Arr3d pointer leads through in a
         continuous manner */
      for (z = 0; z < Z_DIM; z++)
      {
          printf("Location of array %d is %p\n", z, *Arr3D[z]);
          for ( y = 0; y < Y_DIM; y++)
          {
               printf(" Array %d and Row %d starts at %p", z, y,
Arr3D[z][y]);
               diff = Arr3D[z][y] - space;
               printf("       diff = %d ",diff);
               printf(" z = %d y = %d\n", z, y);
          }
      }
      return 0;
}
------------------- End of Prog. 9.4 ----------------------------


If you have followed this tutorial up to this point you should have no problem deciphering the above on the basis of the comments alone. There are a couple of points that should be made however. Let' start with the line which reads:


      Arr3D[z][y] = space + (z*(X_DIM * Y_DIM) + y*X_DIM);


Note that here space is a character pointer, which is the same type as Arr3D[z][y]. It is important that when adding an integer, such as that obtained by evaluation of the expression (z*(X_DIM * Y_DIM) + y*X_DIM), to a pointer, the result is a new pointer value. And when assigning pointer values to pointer variables the data types of the value and variable must match.





Digg!Reddit!Del.icio.us!Google!Live!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Spurl!Wists!Simpy!Newsvine!Blinklist!Furl!Fark!Blogmarks!Yahoo!Smarking!Netvouz!Shadows!RawSugar!Ma.gnolia!PlugIM!Squidoo!BlogMemes!FeedMeLinks!BlinkBits!Tailrank!linkaGoGo!Free social bookmarking plugins and extensions for Joomla! websites! title=
Comments
Add NewSearch
Only registered users can write comments!

Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved.



Last Updated ( Saturday, 29 December 2007 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits

Powered password keylogger is a driver-based software keylogger by Eltima

Tired of MS Office ? Try Ashampoo Office 2008 . All OS supported