Thread: Concerning Dynamic Arrays

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Concerning Dynamic Arrays

    The current problem I have is that I want to use a dynamic array of structures to create a very rudimentary database. I think that using realloc() in it's own function would be best, but I'm not too sure how realloc works.
    Code:
    void addOne(struct * record){
        realloc(record, sizeof((strlen(record)+1));
    }
    realloc() is used to add one more element containing a structure to the array by passing a pointer to the array to the addOne function. I get the length of the array, add one to it and use sizeof to determine how much more memory to allocate.

    I'm sure this is wrong, but it's all I can come up with without seeing realloc in action. I've looked around for an example and nothing offers one.
    C code. C code run. Run code, run...please!

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Have you ever worked with malloc before? Well, malloc would work something like this:
    Code:
    char *string;
    
    string = malloc(11); /* the size of 10 chars and the last for null */
    Well, anyway, let's say you want string to have 20 chars (not including null), now. You would do:
    Code:
    char *string;
    
    string = malloc(11);
    string = "This is a t";
    
    realloc(string, 21);
    string = "This is a test for a";
    Understand?

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    So my addOne function would look like this
    Code:
    void addOne(structType * record){
        realloc(record, strlen(record)+1);
    }
    Since I will have no way of knowing what numbers to put into realloc as you did at runtime, will I need to use strlen() to determine how long the array is and add one to that to create a new element in the array?
    C code. C code run. Run code, run...please!

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> So my addOne function would look like this
    Code:
    void addOne(structType * record){
        realloc(record, strlen(record)+1);
    }
    <<

    Well, what you would want to do for a struct is use sizeof, instead of strlen. So, you would replace strlen with sizeof.

    >> will I need to use strlen() to determine how long the array is and add one to that to create a new element in the array? <<

    Um...I think you are confused here. This seems to be a totally different subject (by your description, linked lists). Do you want to use linked lists?

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    string = "This is a t";
    Garfield..... tut tut.... have you learnt nothing yet?
    You cannot assign strings like that you must use strcpy(dest,source)
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    >Um...I think you are confused here. This seems to be a totally >different subject (by your description, linked lists). Do you want to >use linked lists?

    No, I want to use an array of structs. The way I was seeing it is that if I have an array, I have to get the length of that array, add one to it and then use sizeof to determine the total bytes for realloc. Is this correct?

    By all rights, a linked list would be easier :P
    C code. C code run. Run code, run...please!

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Arg!!! I keep falling into the strcpy trap! Sorry. I do know that you need to use strcpy, and I can give you the technical reasoning.

    You see, when you say string = "This is a t", you are working with addresses, not what you think you are working with (my mistake). You would need to call a function that will copy the array of char's into another.

    Sorry!

    --Garfield
    1978 Silver Anniversary Corvette

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> By all rights, a linked list would be easier <<

    So, do you want to use a linked list?

    --Garfield
    1978 Silver Anniversary Corvette

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    No, I understand linked lists. I want to use a dynamic array of structs so that I can learn how and why it works.

    Also
    Code:
    char *string;
    
    string = malloc(11);
    string = "This is a t";
    I thought malloc took the amount of bytes as an argument, depending on different platforms, a char could be more than one byte so you wouldn't be allocating 11 characters. Is this wrong?
    Code:
    char * string;
    
    string = malloc(sizeof(char * 11));
    strcpy(string, "This is a t");
    Last edited by CeeCee; 11-25-2001 at 10:27 AM.
    C code. C code run. Run code, run...please!

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    pretty much works the same as a dynamic array of anything else except you allocate sizeof(your struct) instead of sizeof(int) for a dynamic array of ints.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    That's what I thought, but how do I increment the size to add another element to the array?
    Code:
    realloc(record, sizeof(record));
    That should resize the array to have only one element, because I used the size of a single structure, right?
    Code:
    realloc(record, sizeof((strlen(record)+1));
    Should resize the array to the current number of elements plus one, each element of size struct by using strlen to get the length and sizeof to get the total size in bytes, yes?
    C code. C code run. Run code, run...please!

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A realloc example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* this is an example of the use of the realloc function */
    
    /* the RESIZE is the amount of memory we want to extend by */
    /* on each call.  Too small, and realloc thrashes, wasting */
    /* time, and potentially fragmenting memory.  Too large,   */
    /* and we waste memory which won't be used */
    /* You have to know some things about your data to make */
    /* a good guess at this */
    #define RESIZE  7
    
    /* Typically this is either unknown, an estimate, or */
    /* determined at run-time by some means */
    #define MAX     53
    
    int main ( ) {
      int *int_array = NULL; /* MUST be initialised to NULL */
      int max_i = 0; /* number of useable slots in the array */
      int i;
    
      for ( i = 0 ; i < MAX ; i++ ) {
        if ( i >= max_i ) {
          int *new;
          max_i += RESIZE;      /* get a bit more array */
          printf("Extending array to %d slots\n", max_i );
          new = realloc(int_array,max_i*sizeof(int));
          if ( new == NULL ) {
            /* panic - no memory left */
            /* save and quit, or popup window saying no more */
          } else {
            /* update the array pointer */
            /* realloc can move the memory to a whole new */
            /* place in memory */
            int_array = new;
          }
        }
    
        /* use the array (which grows in size) */
        int_array[i] = i;
      }
    
      /* just to show you it's there! */
      for ( i = 0 ; i < MAX ; i++ ) {
        printf("%d=%d\t", i, int_array[i] );
      }
    
      /* all done with the array now */
      free(int_array);
    
      return 0;
    }

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    I understand basically how realloc() is used now, but I'm still a bit confused by how to get the correct amount of memory to allocate. Which one of these is right for allocating enough memory to add one element to an array of structs?
    Code:
    record = realloc(record, sizeof((strlen(record)+1));
    /*I think this allocates 11 bytes if the array is 10 elements long*/
    /*because strlen returns an int*/
    
    record = realloc(record, sizeof structType * (strlen(record)+1));
    /*Allocates 11 elements of size struct?*/
    
    record = realloc(record, (strlen(record)+1) * sizeof(record));
    /*Allocates 11 elements of size record[10]?*/
    Assuming my guesses are right, the middle one is the one that I want to use.
    C code. C code run. Run code, run...please!

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    strlen is of no use to you. It is used to measure the length of a string. You need to keep track of the size of your array as you build it and realloc it. sizeof will help you keep track of the size of the individual records but as sizeof is a compile time operator and you declare your array at runtime then sizeof cannot be used to find the size of the array.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    I see, so each time I add an element to the array I increment a counter variable. Then I use that variable and sizeof with my structure to realloc the array?
    Code:
    record = realloc(record, (++counter)*sizeof(structType));
    C code. C code run. Run code, run...please!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM