# Concerning Dynamic Arrays

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-25-2001
CeeCee
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.
• 11-25-2001
Garfield
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
• 11-25-2001
CeeCee
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?
• 11-25-2001
Garfield
>> 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
• 11-25-2001
Stoned_Coder
Quote:

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) :p
• 11-25-2001
CeeCee
>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
• 11-25-2001
Garfield
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
• 11-25-2001
Garfield
>> By all rights, a linked list would be easier <<

So, do you want to use a linked list?

--Garfield
• 11-25-2001
CeeCee
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");```
• 11-25-2001
Stoned_Coder
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.
• 11-25-2001
CeeCee
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?
• 11-25-2001
Salem
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; }```
• 11-25-2001
CeeCee
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.
• 11-25-2001
Stoned_Coder
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.
• 11-25-2001
CeeCee
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));`
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last