Guru's Question 
Author Message
 Guru's Question

Here's a toughy:

Using conventional C-style code,
something I wish to be portable,
how would you load in data for a
two-dimensional string array...
dynamically allocated:

example:
1- - - - - - - - - - - - - - - - -
unsigned char **StringArray;
2- - - - - - - - - - - - - - - - -
    if ( (StringArray = (char **)malloc(1024*sizeof(char*)))==NULL ) {
       printf("Error allocating buffer.\n");
       exit(-1);
       }
3- - - - - - - - - - - - - - - - -
         if (((char*)StringArray[i] = (char*)malloc(13))==NULL) {
          printf("Have reached the limit for the StringArray!.\n");
          break;
          }
         memset(StringArray[i], 0, 13);
        strcpy(StringArray[i], data);
        ++i;
- - - - - - - - - - - - - - - - -
NOW, here's the really tricky part!

in #3 I am getting data currently from a file,
reading a line at a time, and feeding that into
the freshy allocated string_array[i],

HOW CAN I SIMPLY LOAD THE _entire_ FILE
INTO MEMORY in one swift operation ?
say, using    fread()   ???

the string array in memory has the actual format
of:
char *StringArray1[i] = {
  "item1a", "item1b",
 "item2a", "item2b",
 etc.
 etc.

Quote:
};

the sizes of each string is unique....
the size of the array may change the next time
the program runs,
any ideas?

THANKS!


http://www.*-*-*.com/ ~RadSurfer/



Wed, 19 May 2004 09:17:13 GMT  
 Guru's Question

Quote:

>Here's a toughy:

>...
>HOW CAN I SIMPLY LOAD THE _entire_ FILE
>INTO MEMORY in one swift operation ?
>say, using    fread()   ???

>the string array in memory has the actual format
>of:
>char *StringArray1[i] = {
>  "item1a", "item1b",
> "item2a", "item2b",
> etc.
> etc.
>};

>the sizes of each string is unique....
>the size of the array may change the next time
>the program runs,
>any ideas?

Is there anything wrong with reading the entire array into a
dynamically allocated memory block, and then calling strtok to pickout
the individual strings, assigning the pointers to the appropriate
entries in your array?

Pete Barrett



Thu, 20 May 2004 02:08:45 GMT  
 Guru's Question
I see that something along that way might be needed...
just not sure how to go about it yet...

I'll always know the number of paired elements.

On Sat, 01 Dec 2001 18:08:45 +0000, Pete Barrett

Quote:


>>Here's a toughy:
>>...

>>HOW CAN I SIMPLY LOAD THE _entire_ FILE
>>INTO MEMORY in one swift operation ?
>>say, using    fread()   ???

>>the string array in memory has the actual format
>>of:
>>char *StringArray1[i] = {
>>  "item1a", "item1b",
>> "item2a", "item2b",
>> etc.
>> etc.
>>};

>>the sizes of each string is unique....
>>the size of the array may change the next time
>>the program runs,
>>any ideas?

>Is there anything wrong with reading the entire array into a
>dynamically allocated memory block, and then calling strtok to pickout
>the individual strings, assigning the pointers to the appropriate
>entries in your array?

>Pete Barrett



Thu, 20 May 2004 04:15:04 GMT  
 Guru's Question

Quote:

>Here's a toughy:

>Using conventional C-style code,
>something I wish to be portable,
>how would you load in data for a
>two-dimensional string array...
>dynamically allocated:

>example:
>1- - - - - - - - - - - - - - - - -
>unsigned char **StringArray;
>2- - - - - - - - - - - - - - - - -
>    if ( (StringArray = (char **)malloc(1024*sizeof(char*)))==NULL ) {
>       printf("Error allocating buffer.\n");
>       exit(-1);
>       }
>3- - - - - - - - - - - - - - - - -
>         if (((char*)StringArray[i] = (char*)malloc(13))==NULL) {
>          printf("Have reached the limit for the StringArray!.\n");
>          break;
>          }
>         memset(StringArray[i], 0, 13);
>        strcpy(StringArray[i], data);
>        ++i;
>- - - - - - - - - - - - - - - - -
>NOW, here's the really tricky part!

>in #3 I am getting data currently from a file,
>reading a line at a time, and feeding that into
>the freshy allocated string_array[i],

>HOW CAN I SIMPLY LOAD THE _entire_ FILE
>INTO MEMORY in one swift operation ?
>say, using    fread()   ???

>the string array in memory has the actual format
>of:
>char *StringArray1[i] = {
>  "item1a", "item1b",
> "item2a", "item2b",
> etc.
> etc.
>};

>the sizes of each string is unique....
>the size of the array may change the next time
>the program runs,
>any ideas?

>THANKS!


>http://members.tripod.com/~RadSurfer/

In order to do what you want, it is necessary for the array to be in
contiguous memory.  The code you posted doesn't do that.  There is no
reason to expect any consistent relationship between the values in
StringArray[i] and StringArray[i+1].

The following is valid only if your file contains already contains
valid c strings, each at most 12 characters long and padded to 13
characters with '\0'.  If you file contains normal text lines,
terminated with '\r' and '\n' characters, this will not work.

You can achieve what you want by allocating the array all at once.
Define StringArray to be a pointer to an array of 13 char
        char (*StringArray)[13];
Allocate space for 1024 of these arrays
        StringArray = malloc(1024 * sizeof *StringArray);
and perform the usual check to insure malloc succeeded.
You can then read all 1024 c strings in with one command
        var=fread(StringArray, sizeof *StringArray, 1024, your_file_ptr);
and perform the usual checks to insure success.

<<Remove the del for email>>



Thu, 20 May 2004 07:14:56 GMT  
 Guru's Question
Interesting.... and sound given the parameters mentioned!

Thanks.


Quote:

>In order to do what you want, it is necessary for the array to be in
>contiguous memory.  The code you posted doesn't do that.  There is no
>reason to expect any consistent relationship between the values in
>StringArray[i] and StringArray[i+1].

>The following is valid only if your file contains already contains
>valid c strings, each at most 12 characters long and padded to 13
>characters with '\0'.  If you file contains normal text lines,
>terminated with '\r' and '\n' characters, this will not work.

>You can achieve what you want by allocating the array all at once.
>Define StringArray to be a pointer to an array of 13 char
>    char (*StringArray)[13];
>Allocate space for 1024 of these arrays
>    StringArray = malloc(1024 * sizeof *StringArray);
>and perform the usual check to insure malloc succeeded.
>You can then read all 1024 c strings in with one command
>    var=fread(StringArray, sizeof *StringArray, 1024, your_file_ptr);
>and perform the usual checks to insure success.

><<Remove the del for email>>



Fri, 21 May 2004 10:26:26 GMT  
 Guru's Question
If the size of each string is unique, don't overlook strdup() for the
allocation of each individual string after the original allocation of  char
**.  If you use this, it's not unreasonable to "count the lines in the file,
prior to your first allocation, independent of length, and then allocate
them.  It doesn't however, load your array in a single shot.

The above technique maximizes space efficiency.  Back from the days when
memory was precious.

balboos

--


Quote:
> Here's a toughy:

> Using conventional C-style code,
> something I wish to be portable,
> how would you load in data for a
> two-dimensional string array...
> dynamically allocated:

> example:
> 1- - - - - - - - - - - - - - - - -
> unsigned char **StringArray;
> 2- - - - - - - - - - - - - - - - -
>     if ( (StringArray = (char **)malloc(1024*sizeof(char*)))==NULL ) {
>        printf("Error allocating buffer.\n");
>        exit(-1);
>        }
> 3- - - - - - - - - - - - - - - - -
>          if (((char*)StringArray[i] = (char*)malloc(13))==NULL) {
>           printf("Have reached the limit for the StringArray!.\n");
>           break;
>           }
>          memset(StringArray[i], 0, 13);
>         strcpy(StringArray[i], data);
>         ++i;
> - - - - - - - - - - - - - - - - -
> NOW, here's the really tricky part!

> in #3 I am getting data currently from a file,
> reading a line at a time, and feeding that into
> the freshy allocated string_array[i],

> HOW CAN I SIMPLY LOAD THE _entire_ FILE
> INTO MEMORY in one swift operation ?
> say, using    fread()   ???

> the string array in memory has the actual format
> of:
> char *StringArray1[i] = {
>   "item1a", "item1b",
>  "item2a", "item2b",
>  etc.
>  etc.
> };

> the sizes of each string is unique....
> the size of the array may change the next time
> the program runs,
> any ideas?

> THANKS!


> http://members.tripod.com/~RadSurfer/



Fri, 21 May 2004 23:51:21 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. This is a biztalk question but probably an easy question for you C# guru's

2. question for the guru's about data types

3. Question for the Guru's

4. A few questions for the real guru's

5. A few questions for the real guru's

6. to all the 'gurus'....

7. Need occasional guru for 'C'...

8. Need occasional guru for 'C'...

9. Need help from MSVC guru's - GPF's with 16-bit

10. Need help from MSVC guru's - GPF's with 16-bit

11. IO performance C# Versus C++ (For C# GURU's only)

12. RegExp guru's - help me please!

 

 
Powered by phpBB® Forum Software