Help Request - Pointer Array "Loses" Data 
Author Message
 Help Request - Pointer Array "Loses" Data

BACKGROUND:
A C program is required to manipulate data records as an
alphabetically-ordered, single-linked linked-list.
Program to be written as collection of modules.
Each module to be designed so as to be tested separately.

PROBLEM:
A pointer array appears to be "losing" a variable immediately after it is
assigned...

I designed the program to experiment with data hiding.
The only functions that can access the linked list or modify it in any way
are located in a file: "nodes.c".
When any data is accessed in the list, it is copied and the copy returned as
return value of function.
In some cases, the copy is a local variable returned by value, in other
cases, the copy is created by malloc, and the pointer
to this malloc'ed copy is returned.

Pointer to the first node in list is a static global variable in "nodes.c".

Functions for user interface are located in a separate files, mainly:
"showstuf.c"
For example - fn_create_show() uses a struct to accept user input from
keyboard - verifies entries - then passes this struct to
fn_add_node()

Several functions in the user interface require a selection from a list of
the key values.
eg: selecting a node to be deleted.
This list is created in fn_create_array() in "nodes.c" and a pointer array
returned.

Adding a number of functions to view the data as it is copied across, shows
that the first entry IS copied but seems to be "lost" immediately after.

The nodes in the actual linked-list are uncorrupted by any of this.
Testing with different numbers of nodes shows this only ever occurs the
first time the routine is called.
Subsequent executions work correctly ( within the limits of several hours
testing ).

PSEUDO_CODE:

     fn_count_nodes() counts the number of nodes in the linked-list
     use this number to malloc a dynamic array of char pointers

     for each node in the linked list

          strlen() the key string
          malloc() a char pointer for a temporary string that is the length
of the key value
          strcpy() the key value to this temporary string
          assign this char pointerfor temporary string into the pointer
array

          printf string that was actually placed into pointer array
           /* here, the correct value is printed */
     }
     place a NULL in the final array position

     for each pointer in pointer_array
     {
          printf string that was placed into pointer array
           /* here, the value at pointer_array[0] has been "lost" */
     }

     return pointer array at end of function

SOURCE CODE:

( I_ZERO is a global #define for 0 )

/* structure for node in linked list */

struct node
{
     char *p_name_of_show;
     char *p_name_of_theatre;

     struct ticket_details ticket_1;
     struct ticket_details ticket_2;

     struct node *p_next_node;

Quote:
};

/***************************************************************************
****************/
char **fn_create_array(void)
{  /* create a dynamic array to hold keys from linked list */

     int i_num_of_nodes = I_ZERO;
     char **pointer_array = NULL;

     int i_read_count  = I_ZERO;
     int i_display_count = I_ZERO;
     struct node *p_this_node = NULL;

     char *p_temp_char = NULL ;      /* to hold malloc'ed temporary string
*/
     int i_len_of_str = I_ZERO;

     i_num_of_nodes = fn_count_nodes();   /* fn to count number of nodes in
linked list */
     pointer_array = (char**)(malloc ( i_num_of_nodes +1 * sizeof(char
*) ) );

     i_read_count = I_ZERO;
     for (p_this_node = p_first_node_in_list; (p_this_node); p_this_node =
p_this_node->p_next_node)
     {
          /* for each node in linked list - copy key value */
          p_temp_char = NULL;
          i_len_of_str = strlen(p_this_node->p_name_of_show);

          p_temp_char = (char*) malloc( (i_len_of_str +1 ) * sizeof(char) );

          if( p_temp_char == NULL)
          {
               printf("\n\nmalloc fatal error - I am crashing ...");
               exit(1);
          }

          strcpy(p_temp_char, p_this_node->p_name_of_show );
          pointer_array[i_read_count] = p_temp_char;

          /*   the following displays:
               array position, value supposed to go in, value that actually
went in, address of pointer */
          /*   at this stage the correct values are printed, as expected */

          printf("\nposition: %d intended: %10.10s; actual: %10.10s;
pointer: %p",
          i_read_count, p_this_node->p_name_of_show,
pointer_array[i_read_count], pointer_array[i_read_count] );

          ++i_read_count;
     }
     pointer_array[i_read_count] = NULL;

     /* display contents of nodes to ensure none have been corrupted */
     /* fn_display_all_nodes(); */

     /* now re-display contents of the pointer array just created */
     printf("\n\n");

     for (i_display_count = I_ZERO; i_display_count < i_read_count;
++i_display_count)
     {
          printf("\nposition: %d; actual: %10.10s; pointer: %p",
          i_display_count, pointer_array[i_display_count],
pointer_array[i_display_count] );

          /* by this stage, the pointer in array_pointer[0] has changed */
     }

     return(pointer_array) ;

Quote:
} /* end of fn_create_array */

/***************************************************************************
****************/

This is compiled on a PC using Borland C++     version 4.52  - the Windows
95 IDE

version 3.1 - the DOS IDE

  Both compilers have been running under     Windows 95A
                                                                  and
NT4 build 1381 with Service Pack 4

Any suggestions would be gratefully received ( "as long as they are legal
and decent ..." )
Thanks in advance.



Sat, 17 Nov 2001 03:00:00 GMT  
 Help Request - Pointer Array "Loses" Data
Probably the reason you are not getting response to this is because your
post is too detailed and long.  Perhaps you can try to track down the
problem a little further and post the critical issue.

Without reading and analyzing your entire post, the typical error when data
gets "lost" from a variable is due to variable scope.  You need to ensure
everything that needs to stay around is either global, static,  or malloc'd,
and that everything that points to the data is static or malloc'd.  Since
your program is a typical linked-list problem, there are numerous books with
complete working examples you can refer to.  Comparing your program with the
examples in books might help you identify the scope problem you are having.

--
****************************************
Richard Armstrong
State Of The Art Consulting, Inc.
http://www.stateoart.com
****************************************

Quote:
> BACKGROUND:
> A C program is required to manipulate data records as an
> alphabetically-ordered, single-linked linked-list.
> Program to be written as collection of modules.
> Each module to be designed so as to be tested separately.

> PROBLEM:
> A pointer array appears to be "losing" a variable immediately after it is
> assigned...

> I designed the program to experiment with data hiding.
> The only functions that can access the linked list or modify it in any way
> are located in a file: "nodes.c".
> When any data is accessed in the list, it is copied and the copy returned
as
> return value of function.
> In some cases, the copy is a local variable returned by value, in other
> cases, the copy is created by malloc, and the pointer
> to this malloc'ed copy is returned.

> Pointer to the first node in list is a static global variable in
"nodes.c".

> Functions for user interface are located in a separate files, mainly:
> "showstuf.c"
> For example - fn_create_show() uses a struct to accept user input from
> keyboard - verifies entries - then passes this struct to
> fn_add_node()

> Several functions in the user interface require a selection from a list of
> the key values.
> eg: selecting a node to be deleted.
> This list is created in fn_create_array() in "nodes.c" and a pointer array
> returned.

> Adding a number of functions to view the data as it is copied across,
shows
> that the first entry IS copied but seems to be "lost" immediately after.

> The nodes in the actual linked-list are uncorrupted by any of this.
> Testing with different numbers of nodes shows this only ever occurs the
> first time the routine is called.
> Subsequent executions work correctly ( within the limits of several hours
> testing ).

> PSEUDO_CODE:

>      fn_count_nodes() counts the number of nodes in the linked-list
>      use this number to malloc a dynamic array of char pointers

>      for each node in the linked list

>           strlen() the key string
>           malloc() a char pointer for a temporary string that is the
length
> of the key value
>           strcpy() the key value to this temporary string
>           assign this char pointerfor temporary string into the pointer
> array

>           printf string that was actually placed into pointer array
>            /* here, the correct value is printed */
>      }
>      place a NULL in the final array position

>      for each pointer in pointer_array
>      {
>           printf string that was placed into pointer array
>            /* here, the value at pointer_array[0] has been "lost" */
>      }

>      return pointer array at end of function

> SOURCE CODE:

> ( I_ZERO is a global #define for 0 )

> /* structure for node in linked list */

> struct node
> {
>      char *p_name_of_show;
>      char *p_name_of_theatre;

>      struct ticket_details ticket_1;
>      struct ticket_details ticket_2;

>      struct node *p_next_node;
> };

/***************************************************************************

- Show quoted text -

Quote:
> ****************/
> char **fn_create_array(void)
> {  /* create a dynamic array to hold keys from linked list */

>      int i_num_of_nodes = I_ZERO;
>      char **pointer_array = NULL;

>      int i_read_count  = I_ZERO;
>      int i_display_count = I_ZERO;
>      struct node *p_this_node = NULL;

>      char *p_temp_char = NULL ;      /* to hold malloc'ed temporary string
> */
>      int i_len_of_str = I_ZERO;

>      i_num_of_nodes = fn_count_nodes();   /* fn to count number of nodes
in
> linked list */
>      pointer_array = (char**)(malloc ( i_num_of_nodes +1 * sizeof(char
> *) ) );

>      i_read_count = I_ZERO;
>      for (p_this_node = p_first_node_in_list; (p_this_node); p_this_node =
> p_this_node->p_next_node)
>      {
>           /* for each node in linked list - copy key value */
>           p_temp_char = NULL;
>           i_len_of_str = strlen(p_this_node->p_name_of_show);

>           p_temp_char = (char*) malloc( (i_len_of_str +1 ) *
sizeof(char) );

>           if( p_temp_char == NULL)
>           {
>                printf("\n\nmalloc fatal error - I am crashing ...");
>                exit(1);
>           }

>           strcpy(p_temp_char, p_this_node->p_name_of_show );
>           pointer_array[i_read_count] = p_temp_char;

>           /*   the following displays:
>                array position, value supposed to go in, value that
actually
> went in, address of pointer */
>           /*   at this stage the correct values are printed, as expected
*/

>           printf("\nposition: %d intended: %10.10s; actual: %10.10s;
> pointer: %p",
>           i_read_count, p_this_node->p_name_of_show,
> pointer_array[i_read_count], pointer_array[i_read_count] );

>           ++i_read_count;
>      }
>      pointer_array[i_read_count] = NULL;

>      /* display contents of nodes to ensure none have been corrupted */
>      /* fn_display_all_nodes(); */

>      /* now re-display contents of the pointer array just created */
>      printf("\n\n");

>      for (i_display_count = I_ZERO; i_display_count < i_read_count;
> ++i_display_count)
>      {
>           printf("\nposition: %d; actual: %10.10s; pointer: %p",
>           i_display_count, pointer_array[i_display_count],
> pointer_array[i_display_count] );

>           /* by this stage, the pointer in array_pointer[0] has changed */
>      }

>      return(pointer_array) ;

> } /* end of fn_create_array */

/***************************************************************************

- Show quoted text -

Quote:
> ****************/

> This is compiled on a PC using Borland C++     version 4.52  - the Windows
> 95 IDE

> version 3.1 - the DOS IDE

>   Both compilers have been running under     Windows 95A
>                                                                   and
> NT4 build 1381 with Service Pack 4

> Any suggestions would be gratefully received ( "as long as they are legal
> and decent ..." )
> Thanks in advance.



Sun, 18 Nov 2001 03:00:00 GMT  
 Help Request - Pointer Array "Loses" Data
Very embarrasing, and very humbling

The problem was that there were too few brackets...

I had typed

malloc ( i_num_of_nodes + 1 * sizeof(char *) )

when I meant to type

malloc ( ( i_num_of_nodes +1)  * sizeof(char *) )

To all who made suggestions: many thanks for taking the time and interest



Wed, 21 Nov 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Displaying binary data as ascii "1"'s and "0"'s

2. Help "Lost Web page"

3. Displaying binary data as ascii "1"'s and "0"'s

4. ""help with TSR""

5. "Conversion may lose significant digits..."

6. "Conversion may lose significant digits..."

7. Request.ServerVariables("HTTP_HOST")

8. Request "lint" tool for SUN OS

9. Feedback request for "MFC Developers Workshop"

10. better C# "foreach" request

11. "Windows data types" vs "c data types"

12. remove() vrs fopen("""w")

 

 
Powered by phpBB® Forum Software