Thread: Problem with realloc

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    Problem with realloc

    Here is a snippet of code that displays the problem I am having with realloc in a much larger program:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        
        int *int_array,counter, size,size1;
        
        printf("How many members in the array do you want: ");
        scanf("%d",&size);
    
    // Allocates memory for the int_array
    
        int_array=(int *)malloc(size*sizeof(int));
    
    // Places a value in each index position    
    
        for(counter=0;counter<size;counter++)
            int_array[counter]=10+counter;
    
        printf("\nThe array has the following elements: \n");
    
    // Prints values
    
        for(counter=0;counter<size;counter++)    
            printf("%d\n",int_array[counter]);
        
    
    
        printf("What would you like the new size to be: ");
        scanf("%d",&size1);
        
    // Should reallocate the array to size size1
    
        int_array=realloc(int_array,size);
    
        printf("\nThe array now has the following elements: \n");
    
    // Runs until counter is equal to the original size...When I increase the size using
    // realloc the values up until the original size should remain unchanged, but 
    // they are not when i print them out
    
        for(counter=0;counter<size;counter++)    
            printf("%d\n",int_array[counter]);
        system("PAUSE");
        return 0;
    }
    HERE IS THE OUTPUT I GET: (I ran this on two machines and it came up with the same numbers)

    Like I said, I am running into this problem in a much larger project, if anyone can help it would be greatly appreciated.

    How many members in the array do you want: 10

    The array has the following elements:
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    What would you like the new size to be: 11
    10
    11
    12
    13
    196697
    15
    3998784
    3998784
    18
    393302

  2. #2
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    int_array=realloc(int_array,size1);

    You forget to multiplus sizeof(int) to size.

    LeiMing.
    Last edited by leiming; 05-22-2008 at 10:07 PM. Reason: I don't find that it's size1 just now......

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    You also are not actually using the new size1. You might also want to test the return value from realloc before you reassign your existing pointer.

    Code:
    new_array = realloc(int_array, size1 * sizeof(int));
    if(new_arr) int_array = new_array;
    else return -1; /* or whatever else */

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    11

    Thanks

    Wow...I fell like a dummy.

    Code:
    int_array=realloc(int_array,size1*sizeof(int));
    works perfectly.

    ...Also, thanks for the error checking hints...always a good idea

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note that it is a bad idea to cast the return of malloc in a C program (explanation is in FAQ).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

Tags for this Thread