Re: Multiple indirection mess-up...
- From: Barry Schwarz <schwarzb@xxxxxx>
- Date: Sun, 18 Dec 2005 14:20:28 -0800
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>>
.
- References:
- Multiple indirection mess-up...
- From: santosh
- Multiple indirection mess-up...
- Prev by Date: Re: Inconsistent
- Next by Date: The Zeus Compression Algorithm
- Previous by thread: Re: Multiple indirection mess-up...
- Next by thread: Indexing a Text File
- Index(es):
Relevant Pages
|