dynamic memory allocation 
Author Message
 dynamic memory allocation

hi

        i wanted to dynamically allocate memory for a 2d array of characters/
array of strings .

Basically i want to be able to enter and display as many strings of as
much length.

int main()
{
        char **str;
        char *temp;
        int no,i;
        printf("Enter no of strings to be entered\n");
        scanf("%d",&no);
        str=(char**)malloc(sizeof(char*)*no);
        for(i=0;i<no;i++)
        {
                printf("Enter string : ");
                fflush(stdin);
                gets(temp);
           (1)  *(str+i)=(char*)malloc(strlen(temp)+1);
                strcpy(*(str+i),temp);
        }
        for(i=0;i<no;i++)
        {
                printf("%s\n",*(str+i));
        }
        return 0;

Quote:
}

i wanted to know

1) is this the right way to do this
2) can i directly write
        printf("Enter string : ");
        gets(*(str+i));
        if i dont allocate memory inside the loop(1) it still works
           why is that?

3) is there any other way i can do this without using *temp

thanks a lot

chris



Sat, 03 Sep 2005 04:07:13 GMT  
 dynamic memory allocation

Quote:

> hi
>    i wanted to dynamically allocate memory for a 2d array of characters/
> array of strings .
> Basically i want to be able to enter and display as many strings of as
> much length.
> int main()
> {
>    char **str;
>            char *temp;
>         int no,i;
>         printf("Enter no of strings to be entered\n");
>         scanf("%d",&no);
>         str=(char**)malloc(sizeof(char*)*no);

Don't cast the return value of malloc(), you will only keep the
compiler from warning you when you forgot the include stdlib.h,
so use
          str = malloc( no * sizeof *str );

And don't forget to check the return value of malloc().

Quote:
>         for(i=0;i<no;i++)
>         {
>                 printf("Enter string : ");
>                 fflush(stdin);
>            gets(temp);

Now, this is as wrong as you can get in a single line of code. First
of all, temp is a char pointer that does not point to any memory you
own. If you're lucky your program will crash at this point with a
segmentation fault, if you are unlucky it will look like it's working.
Before you can write anything to temp you *must* allocate memory that
temp is the pointing to or you must make temp an array of chars.

Second, never ever use gets(). It will read as many characters as the
user types, and if this are more than the amount of the memory you
allocated for temp you have a buffer overrun. Use the fgets() function
instead where you can specify the maximum number of chars to be read
in. If you find it wasn't long enough for the line you have to increase
the size of the unput buffer and continue reading in the rest of the
line.

Quote:
>            (1)  *(str+i)=(char*)malloc(strlen(temp)+1);
>                 strcpy(*(str+i),temp);

Now it would already be too late if temp is just a char pointer that
wasn't initialized... And again, check the return value of malloc()
before yo try to use the allocated memory. But otherwise it's ok,
especially that you allocte one more byte than the length of the
string you're going to copy.

Quote:
>         }
>         for(i=0;i<no;i++)
>    {
>            printf("%s\n",*(str+i));
>    }
>    return 0;
> }
> i wanted to know
> 1) is this the right way to do this
> 2) can i directly write
>    printf("Enter string : ");
>    gets(*(str+i));
>    if i dont allocate memory inside the loop(1) it still works
>       why is that?

It may look as if it works but it really isn't. It might not crash
somewhere down the line at places that don't seem to be related to
what you did here, making such problems a PITA to find.

                                    Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring



Sat, 03 Sep 2005 07:56:12 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Multi-dimensional array using dynamic memory allocation

2. Need help with dynamic memory allocation (malloc) in C

3. Help Please!(linked lists/dynamic memory allocation etc.)

4. dynamic memory allocation

5. Binary Search Tree using dynamic memory allocation (URGENT request for example)

6. Linked List using dynamic memory allocation (URGENT request for example)

7. 2-d dynamic memory allocation

8. Dynamic Memory Allocation For Multi-Dimensional Arrays

9. dynamic memory allocation with arrays in C

10. Unsized array and dynamic memory allocation

11. Multi-dimensional array using dynamic memory allocation

12. Dynamic Memory allocation

 

 
Powered by phpBB® Forum Software