convert list to array, arrays of pointers (to strings) 
Author Message
 convert list to array, arrays of pointers (to strings)

I'm developing a program where I read lines of text from a file.  Since
the number of lines could be anywhere from two to two thousand, I load the
lines into a simple linked list that I created.

Once I've got the information from the file, I no longer need to do
anything with the data except reference it.  So I'm trying to convert the
data to an array.  Since I already have the data stored in memory, I
figure all I need is to create an array of pointers.  Each pointer (in the
array) points to its respective data in the linked list.

Since I do not know the size of the array ahead of time, I declare my
array as a double pointer:

        char** array;

When I figure out how many elements are in my linked list, I malloc the
size needed to store all those pointers:

        num = number_of_list_elements(my_list);
        *array = malloc (num * sizeof(char *));

Then in a for() loop, I point the array pointers to the list elements:

        for (i=0; (i<num) && (my_list!=NULL); i++) {
                array[i] = my_list->item;
                my_list = my_list->next;
        }

So now if I try to print out a string from this array, I get a seg fault:

        printf("element 0 is: %s\n", array[0]);

I used gdb (GNU de{*filter*}) to step through my program to determine that the
print statement is causing the segfault.

What really gets me, though, is that if, from within gdb, I manually print
one of the strings

        (gdb) print array[0]

Then the correct string is printed!  So it appears everything points to
the right stuff... but still somewhere I'm doing something wrong.  Anyone
have any ideas on this?

Thanks,
Matt (who has been spoiled by Java's toArray() linkedList method :)

--

"I'll tip my hat to the new constitution, Take a bow for the new revolution
 Smile and grin at the change all around, Pick up my guitar and play
 Just like yesterday, Then I'll get on my knees and pray..."
            -- Pete Townshend/The Who, "Won't Get Fooled Again"



Tue, 09 Mar 2004 07:40:53 GMT  
 convert list to array, arrays of pointers (to strings)
See annotations in code

Quote:

>I'm developing a program where I read lines of text from a file.  Since
>the number of lines could be anywhere from two to two thousand, I load the
>lines into a simple linked list that I created.

>Once I've got the information from the file, I no longer need to do
>anything with the data except reference it.  So I'm trying to convert the
>data to an array.  Since I already have the data stored in memory, I
>figure all I need is to create an array of pointers.  Each pointer (in the
>array) points to its respective data in the linked list.

>Since I do not know the size of the array ahead of time, I declare my
>array as a double pointer:

>    char** array;

>When I figure out how many elements are in my linked list, I malloc the
>size needed to store all those pointers:

>    num = number_of_list_elements(my_list);
>    *array = malloc (num * sizeof(char *));

This should be array = malloc(...  You cannot dereference array yet,
it doesn't point to anything until after the malloc.

- Show quoted text -

Quote:

>Then in a for() loop, I point the array pointers to the list elements:

>    for (i=0; (i<num) && (my_list!=NULL); i++) {
>            array[i] = my_list->item;
>            my_list = my_list->next;
>    }

>So now if I try to print out a string from this array, I get a seg fault:

>    printf("element 0 is: %s\n", array[0]);

>I used gdb (GNU de{*filter*}) to step through my program to determine that the
>print statement is causing the segfault.

>What really gets me, though, is that if, from within gdb, I manually print
>one of the strings

>    (gdb) print array[0]

>Then the correct string is printed!  So it appears everything points to
>the right stuff... but still somewhere I'm doing something wrong.  Anyone
>have any ideas on this?

>Thanks,
>Matt (who has been spoiled by Java's toArray() linkedList method :)

<<Remove the del for email>>


Tue, 09 Mar 2004 09:35:38 GMT  
 convert list to array, arrays of pointers (to strings)

Quote:
> >       num = number_of_list_elements(my_list);
> >       *array = malloc (num * sizeof(char *));
> This should be array = malloc(...  You cannot dereference array yet,
> it doesn't point to anything until after the malloc.

Awesome, thanks!  I knew it was something rather subtle.

However, at one time my code was actually like you suggest (without the
attempted dereferencing), but for some reason I thought that generated a
compiler error.

Thanks again!
Matt

--

"I'll tip my hat to the new constitution, Take a bow for the new revolution
 Smile and grin at the change all around, Pick up my guitar and play
 Just like yesterday, Then I'll get on my knees and pray..."
            -- Pete Townshend/The Who, "Won't Get Fooled Again"



Tue, 09 Mar 2004 11:01:14 GMT  
 convert list to array, arrays of pointers (to strings)
You can solve Your promble by using efence libray.You may accessing
Illegal memory.That is if allocated memory for your array but you are
accessing out of that memory.Tha may be overrun or underrun.By efence
you can solve your problem very easily.When ever you are using
pointers concept please complie program
using efence library.


Tue, 09 Mar 2004 19:43:56 GMT  
 convert list to array, arrays of pointers (to strings)

Quote:
> You can solve Your promble by using efence libray.You may accessing
> Illegal memory.That is if allocated memory for your array but you are
> accessing out of that memory.Tha may be overrun or underrun.By efence
> you can solve your problem very easily.When ever you are using
> pointers concept please complie program
> using efence library.

Latha,

please consider to send replies to posted messages as replies
but not as new posts to the group. Most readers use a threaded
newsreader which puts referenced messages in a thread. So please
use the reply-button next time. Anyway, I really don't
understand what you are talking about. What exactly is efence
library? For sure it isn't part of the C-standard.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Tue, 09 Mar 2004 20:15:04 GMT  
 convert list to array, arrays of pointers (to strings)


Quote:
> <...> What exactly is efence
> library? For sure it isn't part of the C-standard.

e-fence (electric fence) It's a memory checking library.

--
-hs- emdel at noos.fr "support Afghans against Talebans"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Thu, 11 Mar 2004 02:18:26 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Copy Array string to another Pointer array string in ANSI-C

2. Converting array lists to arrays

3. Converting a string array to a int array

4. convert c++ string class string into a character array

5. Array of pointers, pointer to array...

6. array pointer/pointer array

7. arrays pointers array of pointers

8. Pointer to Array of Pointers to Strings.

9. How do I convert a VARIANT array to a CString array

10. converting 2D array to pointer

11. Using array index in array initialization list's

12. Lists VS. Dynamic Arrays VS. Re-defining Arrays

 

 
Powered by phpBB® Forum Software