Thread: Dynamic Memory Allocation

  1. #1
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62

    Question Dynamic Memory Allocation

    In this exercise I can't see what am I doing wrong. Compiler says:
    Code:
    wk2prog4.c: In function `main':
    wk2prog4.c:20: warning: char format, different type arg (arg 2)
    I don't understand it, it should be right. And My code is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int n,i;
        char **a;
        char buffer[101];
        
    	
        printf("How many words do you want to enter?\n");
        scanf("%d", &n);
    	
        a = (char **) malloc(n * sizeof(char *));
    	
        printf("Enter %d words, each containing no more than 100 characters\n", n);
        for(i = 0; i < n; i++)
        {
    	scanf("%s", &buffer);
            strcpy(a[i], buffer);
        }
    	
        printf("\nYou entered:\n");
        for(i = 0; i < n; i++)
        {
            printf("%s\n", a[i]);
        }
    
    
        for(i = 0; i < n; i++)
        {
            free(a[i]);
        }
    
        return 0;
    }
    Last edited by BoneXXX; 03-25-2007 at 12:15 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where's line 42?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Quote Originally Posted by laserlight
    Where's line 42?
    scanf("%s", &buffer);

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Compiles fine for me. Although it might be a good idea for you to change main to this:
    Code:
    int main(void)
    BTW you're not allocating that 2D array correctly. I would think you'd do something like this:
    Code:
    a = malloc(n);
    for (i = 0; i < n; i++) {
    	a[i] = malloc(101 * sizeof *buffer);
    }
    And by the way casting malloc is not always the best thing to do.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In that context, buffer is a pointer to its array; a pointer of type char. By asking its address again (&buffer) the type of object passed to scanf() is char**, which is wrong.

  6. #6
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    I compile my c programs
    Code:
    gcc -Wall -ansi -pedantic -O blabla.c -o blabla.out
    also if i compile short cut "gcc" the program does not give any error but when the program runs segmentation fault happens.
    Code:
    How many words do you want to enter?
    3
    Enter 3 words, each containing no more than 100 characters
    m
    Segmentation fault

  7. #7
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    >also if i compile short cut "gcc" the program does not give any error but when the program runs segmentation fault happens.
    That's probably because of the 2 problems mentioned already: you're not allocating memory properly, and there's no reason to use the & symbol because it's already a pointer (if you see what I mean).

  8. #8
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Quote Originally Posted by joeprogrammer
    >also if i compile short cut "gcc" the program does not give any error but when the program runs segmentation fault happens.
    That's probably because of the 2 problems mentioned already: you're not allocating memory properly, and there's no reason to use the & symbol because it's already a pointer (if you see what I mean).
    In the exercise it wants me to allocate the memory by
    Code:
    a = (char **) malloc(n * sizeof(char *));
    AND wants me to use
    Code:
    char **a
    I removed the ampersand , the program compiles fine but I get segmentation error at the middle of the program.
    Last edited by BoneXXX; 03-25-2007 at 01:05 AM.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by BoneXXX
    I removed the ampersand , the program compiles fine but I get segmentation error at the middle of the program.
    This is the reason:

    Quote Originally Posted by joeprogrammer
    BTW you're not allocating that 2D array correctly. I would think you'd do something like this:
    Code:
    a = malloc(n);
    for (i = 0; i < n; i++) {
    	a[i] = malloc(101 * sizeof *buffer);
    }
    You need to allocate memory for each char * in your array.

    Quote Originally Posted by joeprogrammer
    And by the way casting malloc is not always the best thing to do.
    Regarding casting malloc(), the FAQ concludes as follows:

    [Y]ou should use whichever you like provided you are aware of the issues.

  10. #10
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    >You should use whichever you like provided you are aware of the issues.
    Yes, I am aware of that. But it seemed that the OP wasn't aware of this, so I thought it might be a good idea for him/her to read it...

  11. #11
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Thank you Joeprogrammer and MacGyver, I fixed the problem.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        int n,i;
        char **a;
        char buffer[101];
        
        printf("How many words do you want to enter?\n");
        scanf("%d", &n);
    	
        a = (char **) malloc(n * sizeof(char *));
    	
        printf("Enter %d words, each containing no more than 100 characters\n", n);
        
    	for(i = 0; i < n; i++)
        {
    	scanf("%s", &buffer);
    	a[i] = (char *) malloc(strlen(buffer) + 1);
            strcpy(a[i], buffer);
        }
    	
        printf("\nYou entered:\n");
        
    	for(i = 0; i < n; i++)
        {
            printf("%s\n", a[i]);
        }
    
        for(i = 0; i < n; i++)
        {
            free(a[i]);
        }
    
        return 0;
    }
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > a = (char **) malloc(n * sizeof(char *));
    You're still casting malloc - this is C
    Also, there is no free(a) at the end.

    > scanf("%s", &buffer);
    You don't need the & in this case - it's the exception to the rule.
    Arrays are turned into a pointer to the first element when you pass them to a function anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM