Re: arrays question



Eric wrote:
I think this is correct but i wanted to be sure, I'm wanting someone to tell
me if I'm doing the array allocation and deallocation correctly.

int i, wc = 0;
char *vptr;
char **Array;

Array = malloc(sizeof(char **));

This is probably wrong. You're assigning to Array, (which is a object
of type pointer to pointer to char), sizeof(char **) bytes. This would
make Array point to the start of a region of memory large enough to
hold on variable of type char **, which is what Array already is.
Furthermore if you change the type of Array in the future, then the
value returned by the sizeof will not be correct. You've also failed to
check weather malloc() has succeeded or not.

In fact given rest of your code, I don't think this statement is needed
at all.

while(some condition)
{
vptr = some char array with a length 1 and 20 bytes

Not as you've declared above. It's a pointer to type char. It won't
automatically point to anywhere. I'm assuming you've initialised it to
the start of an array in code you've not shown above.

Array = realloc(Array, sizeof(char**)*wc+1);

Many mistakes here. Firstly always pass realloc() a *copy* of the
pointer. If realloc() fails to resize the memory block, it will return
a null pointer *without* altering the original memory block, in which
case the assignment above means that you've lost access to it. Again
you've failed to check the return value of realloc(). Again hard-coding
the type of Array into the sizeof operator is inflexible. The following
form of the statement may be better:

/* Copy the return value of realloc() into a temp. pointer */
tmp_ptr = realloc(Array, sizeof **Array * wc + 1);
if(tmp_ptr)
Array = tmp_ptr;
else
free(Array);
/* Do something else */

Array[wc] = malloc(strlen(vptr)+1);

Again check for malloc() failure.

strcpy(WordArray[wc], vptr);

Are you sure this is what you want? You haven't included the
declaration of WordArray so I can't say anything definite at this
point. Always include the declaration of objects shown in the code, or
atleast mention their types.

go for liftoff?

Not quite. First make all your flight checks.

.



Relevant Pages

  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: Malloc code
    ... malloc() not return NULL? ... returned from malloc to an array of pointers, and then return this array of ... So I just returned a void pointer and assign the value to ... TCP_LOAD_MCB_X and I am assigning the values to the items of the strucutre ...
    (microsoft.public.vc.language)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)
  • Re: Simple question on Pointers
    ... int main ... It stores 12 char and only 12 ... pointer to the first element of the array with type pointer to element ...
    (microsoft.public.vc.language)