0

I have a struct named clients, and i created this array of structs.

typedef struct auxiliarRegistre{
 char name[50];
 char CPF[20];
 char addr[100];
}clients;

clients PrimaryClients[100];

I'm calling a function to insert the data into this arrays, but i wanna to increase the amount of possible values until it reaches the limit. Is this the correct way?

int *pointer = (clients *) malloc(sizeof(clients));
PlayMa256
  • 6,603
  • 2
  • 34
  • 54
  • 1
    That `malloc` call allocates space for *one* `client` structure. You might also want to read about [`realloc`](http://en.cppreference.com/w/c/memory/realloc). – Some programmer dude Oct 28 '13 at 13:31
  • 3
    This cannot possibly be the correct way, since you are casting the return value of `malloc()`, [which is wrong](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). Apart from that, are you looking for `realloc()`? –  Oct 28 '13 at 13:31
  • Also watch out that you can't use `realloc()` to [embiggen](http://en.wiktionary.org/wiki/embiggen) `PrimaryClients` as it is not allocated with `malloc()`. – Kninnug Oct 28 '13 at 13:40
  • Why would `pointer` be of type `int*` that is a pointer to `int` and not a pointer to `clients`, `clients*`? – Jens Gustedt Oct 28 '13 at 13:55
  • if i use clients *pointer = malloc(sizeof(clients), i will need to access PromaryClients using pointer->name? – PlayMa256 Oct 28 '13 at 14:20

1 Answers1

2

Here's one example:

#include <stdlib.h>

typedef struct auxiliarRegistre { ... } clients;

int arrSize = SOME_START_SIZE;
clients *arr = malloc( arrSize * sizeof *arr );

/**
 * Do stuff with arr.  When you need to extend the buffer, do the following:
 */

clients *tmp = realloc( clients, sizeof *arr * ( arrSize * 2));
if ( tmp )
{
  arr = tmp;
  arrSize *= 2;
}

Doubling the size of the buffer each time you need to extend it is a common strategy; this tends to minimize the number of calls to realloc. It can also lead to serious internal fragmentation; if you have 128 elements and you need to store just one more, you wind up allocating 256 elements overall. You can also extend by a fixed amount, such as

clients *tmp = realloc( clients, sizeof *arr * ( arrSize + extent ));
if ( tmp )
{
  arr = tmp;
  arrSize += extent;
}

Note that you don't want to assign the result of realloc directly to your buffer; if it returns NULL due to an error, you'll lose your reference to the memory you've already allocated, leading to a memory leak. Also, you don't want to update your array size until you know the call has succeeded.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • interesting, to acess the fields on arr, i need to use arr->name, right? – PlayMa256 Oct 28 '13 at 17:25
  • @MatheusSilva: you'd use `arr[i].name`. The subscript operation implicitly dereferences `arr`; that is, if the type of `arr` is `client *`, then the type of `arr[i]` is `client`. – John Bode Oct 28 '13 at 17:30
  • aparently i hade some errors, its shows me syntax error before cliente `*tmp = realloc(client, sizeof(*arr * ( arrSize + 1 )));` and i dont know what is the error... – PlayMa256 Oct 31 '13 at 13:37
  • @MatheusSilva: what is the *exact* error message coming from the compiler? Note that the `realloc` call should be written as `realloc(client, sizeof *arr * (arrSize + 1))`; check your parentheses. – John Bode Oct 31 '13 at 14:51