Re: Multiple indirection mess-up...



On 18 Dec 2005 08:13:11 -0800, "santosh" <santosh.k83@xxxxxxxxx>
wrote:

>Hi all,
>
>In the following program I allocate a block of pointers to type char,
>initialised to zero. I then point each of those pointers to a block of
>allocated memory of fixed size (33 bytes). A unique 'string' is then
>generated using itoa() and rand() for each block of memory.
>
>Finally using pointer-to-pointer of type char, I print the contents of
>the blocks of memory, i.e. the strings, using both printf() and
>manually, character by character.
>
>When trying to print character by character, the program is terminated
>for a GPF. I suspect the fault is in the pointer manipulations between
>lines 61 and 74.
>
>Can anyone find the exact mistake?
>
>The code follows:
>1: /* File: 006.c */
>2: #include <stdio.h>

Please leave the line numbers of postings. Someone trying to
duplicate your problem needs to delete them before compiling. If you
need to highlight a particular line, do so with a comment.

>3: #include <stdlib.h>
>4: #include <time.h>
>5:
>6: #define DEF_ALLOC_ITEMS 32
>7: #define DEF_STR_SIZE 33 /* Because itoa() can return upto 33 bytes
>*/
>8:
>9: int main( void )
>10: {
>11: char **mptr = NULL;
>12: char **ptr = NULL;
>13: char **sptr = NULL;
>14: int rnd;
>15: size_t nitems = DEF_ALLOC_ITEMS;
>16: size_t ctr;
>17:
>18: /* Allocate an array of pointers to type char, set to NULL */
>19: mptr = calloc(nitems, sizeof (char *));

calloc sets each byte to all bits zero. For the bytes that make up a
pointer, this may or may not be a valid value and, if valid, may or
may not represent NULL. Since you do properly initialize each pointer
before use, the resources spent by calloc performing this
initialization are wasted.

>20: if(mptr == NULL)

A short error message identifying the failure would be nice.

>21: exit(EXIT_FAILURE);
>22: else
>23: ptr = mptr;

A more common idiom is to include this in the first clause of the
following for statement.

>24:
>25: /* Seed the pseudo-random number generator */
>26: srand((unsigned) time(NULL));
>27:
>28: /* Use itoa() with rand() as parameter to get a character string.
>29: * Initialise each of the allocated pointers to a block of
>30: * allocated, zero'ed out memory, and pass it to itoa().
>31: * Thus we get a block of pointers to type char, each pointing to
>32: * a block of memory of size DEF_STR_SIZE, containing the character
>33: * string representation of a pseudo-random number.
>34: */
>35: nitems = DEF_STR_SIZE;
>36: for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)
>37: {
>38: *ptr = calloc(nitems, sizeof (char));

While an all bits zero char is guaranteed to be valid, this
initialization is also wasted since you initialize each allocated area
with a string prior to use.

>39: if(*ptr == NULL)
>40: exit(EXIT_FAILURE);
>41: else
>42: {
>43: rnd = rand();
>44: printf("\nGenerated random number for string %u is: %d",
>45: ctr+1, rnd);

ctr+1 probably has type size_t. This is an unsigned integer type but
not necessarily an unsigned int, which is required by the %u format.
Either cast the expression to unsigned int or, if you have a C99
compiler, use the %zu format.

>46: itoa(rnd, *ptr, 10);

itoa is a non-standard function. You can achieve the same result with
sprintf.

>47: }
>48: ptr++;

A more common idiom is to include this in the third clause of the for
statement.

>49: }
>50:
>51: /* Now print the strings */
>52: ptr = mptr;
>53: puts("\nPrinting strings via printf():");
>54: for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)
>55: {
>56: printf("\nString %u is: %s", ctr+1, *ptr);
>57: ptr++;
>58: }
>59:
>60: /* Now print the strings manually using multiple-indirection... */
>61: ptr = mptr;
>62: sptr = ptr;

This statement is superfluous.

>63: puts("\n\nPrinting strings manually using multiple-indirection:");
>64: for(ctr = 0; ctr < DEF_ALLOC_ITEMS; ctr++)
>65: {
>66: printf("\nString %u is: ", ctr+1);
>67: sptr = ptr;
>68: while(**sptr != '\0')
>69: {
>70: printf("%c", **sptr);

putc or fputc is probably more efficient here.

>71: *sptr++;
>72: }

You need to output a \n here to avoid having all your strings appear
on one line.

>73: ptr++;

Previously identified as the source of your problem.

>74: }
>75:
>76: return 0;
>77: }
>
>Thanks.


<<Remove the del for email>>
.



Relevant Pages

  • Re: About Information Hiding Design Rules
    ... NULL or returns a pointer to a myadt_t instance that has been ... Here, being a very absent-minded man:), because of I forgot to initialize an instance of myadt_t. ... In practice, IMHO, it's highly unlikely that the pointer ADT ) contains the address of a memory location in which the integer corresponding to "int uninitialized_data" has all its 32 bits set to zero. ... value as the signature for a properly-initialized struct. ...
    (comp.lang.c)
  • Re: (part 10) More Schildt-like errors in Dicky Heathens book
    ... like mallocif mallocreturns a non-NULL pointer. ... If one is using a debugging malloc which checks that all accesses are ... when sz is zero, it gives the debugging allocator an opportunity to ...
    (comp.lang.c)
  • Re: Triplet 630APL meter movement adjustment
    ... was the same whether the meter was standind up or laying down. ... mechanical zero with the unit in the vertical position and took a ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ... support malloc/calloc requests for 0 bytes and whether or not the ...
    (comp.arch.embedded)
  • Re: Apple III Business BASIC Invokables
    ... I'm using Apple II Pascal 1.3 to assemble to a Pascal object module, then transfer to a physical /// disk... ... It pulls this off with extra hardware to snoop zero page address read/writes used by indirect indexed addressing mode instructions. ... When you get a 16 bit pointer to a buffer, it appears that you have to determine if it is an absolute pointer to somewhere in the current 64K, or an indirect pointer that has been already set up in the zero page/X-byte page. ...
    (comp.sys.apple2.programmer)