Allocating memory inside a function 
Author Message
 Allocating memory inside a function

I have this problem: I'm allocating memory inside a function to save an
array of char, I  print this string inside the function and it looks ok, but
the calling program gets an empty string... Why???

getpar(fini, array)
FILE *fini;
char ** array;
{
    char buffer[200];

       int qtd_other_files=0;

      while(!feof(fini)){
         fgets(fini, 150, buffer);
         qtd_other_files++;
         array=(char **) realloc(array, qtd_other_files * sizeof(char *));
         array[qtd_other_files - 1]=(other_filename_type)
malloc((strlen(parametro)+1)*sizeof(char));

         printf("array[0] = %s \n", array[0]);            /* this prints
ok!!!      */
      }

Quote:
}

main()
{

    FILE *fini;
    char **array;

    fini = fopen("fic.ini","r");

    getpar(fini, array);

    printf("array[0] = %s \n", array[0]);            /* this time it prints
nothing...   */

Quote:
}



Sun, 28 Oct 2001 03:00:00 GMT  
 Allocating memory inside a function


Quote:
> I have this problem: I'm allocating memory inside a function to save
> an array of char, I  print this string inside the function and it
> looks ok, but the calling program gets an empty string... Why???

    First of all, repeated usage of realloc doesn't seem a very good
practice. When you have sets of data that may grow to an arbitrary size
in increments of one, a linked list does much better.

Quote:
> getpar(fini, array)
> FILE *fini;
> char *** array;  /* array, in main, is a ptr to ptrs to char. */

                   /* Since you intend to modify it here, you should */
                   /* declare a ptr to a ptr to ptrs to char */
Quote:
> {
>     char buffer[200];

>        int qtd_other_files=0;

>       while(!feof(fini)){
>          fgets(fini, 150, buffer);
>          qtd_other_files++;
>          *array=(char **) realloc(array, qtd_other_files * sizeof
(char *));
>          *array[qtd_other_files - 1]=(other_filename_type)malloc

((strlen(parametro)+1)*sizeof(char));
Quote:
>          printf("array[0] = %s \n", array[0]); /* this prints ok */
>       }
> }

> main()
> {

>     FILE *fini;
>     char **array;

>     fini = fopen("fic.ini","r");

>     getpar(fini, array);

>     printf("array[0] = %s \n", array[0]); /* this time it prints

nothing */

Quote:
> }

   There might be additional errors. I am not quite sure that this
corrected version will work (it's sort of awkward thinking). But you
might try it.

   Believe me: it is easier to learn to work with linked lists than
striving to force concepts to work for what they weren't designed.

Salud!

--
Solo hay calma en la perfeccion

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Mon, 29 Oct 2001 03:00:00 GMT  
 Allocating memory inside a function

Quote:
>char ** array;

should be char *array;

Quote:
>    getpar(fini, array);

should be getpar(fini, &array);

...and you function needs to be modified accordingly too. What you pass into
getpar should be the address of the variable array, which just happens to be
a pointer.

you could think of array like this, in the function:

(char *)* array

i.e. you want to pass the address of a (char *) so you can change it, i.e.
store a new pointer value in it.



Mon, 29 Oct 2001 03:00:00 GMT  
 Allocating memory inside a function
Yes, I got it wrong...
The address is passed by value only one way, in the stack. It is not passed
out again... I've solve it declaring my function to return a pointer to the
array of strings. It works ok now.
Thanks for all your help!


Mon, 29 Oct 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Freeing memory allocated inside a function

2. Help with Re-Allocating Memory inside a structure

3. how to allocate a pointer with changing memory size inside a program

4. allocating memory in one function and freeing it with another function

5. allocating memory with functions

6. allocating memory and processing thru function.

7. Do the string functions allocate memory?

8. standard variadic functions allocate memory?

9. getline function that allocates memory on-the-fly

10. allocate memory for function

11. allocate memory in a function

12. C functions that allocate memory

 

 
Powered by phpBB® Forum Software