dynamic memory allocation 
Author Message
 dynamic memory allocation


Quote:

> I am relatively new to C programming and have a question about dynamic memory
>allocation. What is the best method for dunamically allocating a large number
>of small blocks of memory? e.g. reading 700 - 800 strings into a dynamically
>allocated array, list, etc from a text file. It seems that there must be a
>better method than calling malloc 700 times!

What you could do is allocate larger chunks and then take the string storage
piecemeal from this chunk.

Wait a minute, that's what malloc typically does already. :)

What you would gain by this is a little saving in memory and speed because
you could avoid having any sort of overhead or alignment padding in the string
storage. In other words, you could just store the strings consecutively,
with each subsequent string starting immediately after the null terminating
byte of the previous one.

This is a good technique for setting up invariant string tables such
as symbol tables. If you need to be able to release the memory of each
string individually, or to change the length of the string, you can't
use this approach.



Fri, 19 Oct 2001 03:00:00 GMT  
 dynamic memory allocation

Quote:

>  I am relatively new to C programming and have a question about dynamic memory
> allocation. What is the best method for dunamically allocating a large number
> of small blocks of memory? e.g. reading 700 - 800 strings into a dynamically
> allocated array, list, etc from a text file. It seems that there must be a
> better method than calling malloc 700 times!

Under certain circumstances you might be able to dynamically a large block of
memory and create a pointer table to manage the strings. But, I think that the
best way to do this is to write a function that will dynamically allocate the
memory as you need it. With this function you can grow an array of string pointers
as you need it. Below is a sample function,alloc_string().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *alloc_string(const char *str,char ***ps,int *count) {
    int i;
    char **tmp;

    if((tmp = realloc(*ps,sizeof **ps * (*count+1))) == NULL) return NULL;
    *ps = tmp;
    if(((*ps)[*count] = malloc(strlen(str) + 1)) == NULL) return NULL;
    strcpy((*ps)[*count],str);
    (*count)++;
    }

int main(void) {
   char *buf[4]  = {"string 1","string 2","string 3","string 4"};
   char textfile[80],**arr = NULL;
   int i,count = 0;
   FILE *fp;

   for(i = 0;i < 4;i++)
      if(alloc_string(buf[i],&arr,&count) == NULL) break;
   for(i = 0; i < count;i++) puts(arr[i]);
   for(i = 0;i < count;i++) free(arr[i]);
   free(arr);
   arr = NULL;
   count = 0;
   if((fp = fopen("test.c","r")) != NULL) {
      while(fgets(textfile,sizeof textfile,fp) != NULL)
  if(alloc_string(textfile,&arr,&count) == NULL) break;
      fclose(fp);
      }
   else puts("Unable to open file");
   for(i = 0;i < count;i++) {
      printf(arr[i]);
      free(arr[i]);
      }
   free(arr);
   return 0;
   }

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Sat, 20 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Multi-dimensional array using dynamic memory allocation

2. Need help with dynamic memory allocation (malloc) in C

3. dynamic memory allocation

4. Help Please!(linked lists/dynamic memory allocation etc.)

5. Binary Search Tree using dynamic memory allocation (URGENT request for example)

6. Linked List using dynamic memory allocation (URGENT request for example)

7. 2-d dynamic memory allocation

8. Dynamic Memory Allocation For Multi-Dimensional Arrays

9. dynamic memory allocation with arrays in C

10. Unsized array and dynamic memory allocation

11. Multi-dimensional array using dynamic memory allocation

12. Dynamic Memory allocation

 

 
Powered by phpBB® Forum Software