Thread: realloc(): invalid size:

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    realloc(): invalid size:

    I'm trying to resize an array using realloc and it's telling me that the size is invalid but I'm not sure why since I'm using sizeof to get the correct size. Any ideas?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int numberOfInputs;
    char inputs[1];
    
    void resize(char* c, int i);
    
    int main()
       printf("%s", "How many characters would you like to enter? ");
       scanf("%d", &numberOfInputs);
       if (numberOfInputs > 1)
       {
          resize(inputs, numberOfInputs);
       }
       printf("You input: %s", inputs);
    }
    
    void resize(char* c, int i)
    {
       c = (char*) realloc(c, sizeof(char) * i);
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Well, for starters you never actually read anything into your string.

    I don't think you can malloc arrays declared as you have there - try declaring it as char * instead of char[]

    realloc:

    Parameters

    ptr
    Pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated.
    If this is NULL, a new block is allocated and a pointer to it is returned by the function.
    size
    New size for the memory block, in bytes.
    If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.
    Last edited by KBriggs; 06-03-2010 at 05:34 PM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    I don't think you can malloc arrays declared as you have there - try declaring it as char * instead of char[]
    Yep, that is the error. "c" aka "inputs" is not a genuine pointer so cannot be reassigned (which realloc could require a reassignment -- anyway you need to use a real pointer, not an array name).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Even if you declare inputs as char * and initialize to NULL and pass to function.
    It still won't work. You need to pass address of pointer &input or return the pointer value.
    c-faq

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bayint Naung View Post
    Even if you declare inputs as char * and initialize to NULL and pass to function.
    It still won't work. You need to pass address of pointer &input or return the pointer value.
    c-faq
    Also a true observation.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Replies: 3
    Last Post: 08-22-2008, 11:12 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. establishing new size using realloc
    By sanu in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2002, 09:40 AM