Thread: More malloc questions...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534

    More malloc questions...

    Up until now I have basically avoided using malloc when possible and just allocate space with a static array. Lately I have been experimenting with malloc, and I ran into a problem which left me scratching my head for a bit. Here is an example of what I was trying to do originally:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void foo(char **txt_ptr)
    {
        enum {
            BUF_MAX = 25
        };
        char *buffer = malloc(BUF_MAX * sizeof *buffer);
        char *p;
    
        if (buffer == NULL) {
            puts("No memory for you!");
            exit(EXIT_FAILURE);
        }
    
        printf("\nEnter some text: ");
        fflush(stdout);
    
        if (fgets(buffer, sizeof buffer, stdin) == NULL) {
            perror("fgets()");
        }
    
        if ((p = strchr(buffer, '\n')) != NULL) {
            *p = '\0';
        }
    
        *txt_ptr = buffer;
    }
    
    int main(void)
    {
        char *textp = NULL;
        foo(&textp);
        printf("\n%s\n\n", textp);
    
        return 0;
    }
    This line was giving me trouble:
    Code:
     if (fgets(buffer, sizeof buffer, stdin) == NULL) {
    IIRC, that line would work properly with a static array, but in this case, 'sizeof buffer' equals 8. Not what I wanted at all; I could enter as many characters as I wanted, but the printf in main would only print 7 of them. So I came up with a kludge that looked like this:

    Code:
     if (fgets(buffer, (BUF_MAX * sizeof *buffer), stdin) == NULL) {
    It 'works', but I hate learning this way in case there is something wrong with it. What is the nicest way to solve this problem?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is the nicest way to solve this problem, modulo the fact that sizeof *buffer is always 1 and can therefore be removed. That is: you know the size of buffer is BUF_MAX, since that's what you made it, so that's what you need to pass to fgets.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To clarify what happens here: sizeof is resolved at compile time. The compiler does not know what buffer points to, nor does "sizeof" solve the problem when you pass (for example) a string to a funciton and then call sizeof - it's the same situation here, you have a pointer, you ask for it's size, and you get 8 (a 64-bit OS, apparently), not the size of the block it points to.

    When you use pointers and dynamic memory, YOU need to know how big the block of memory is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM