C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-22-2008, 07:22 PM   #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
wd_kendrick is offline   Reply With Quote
Old 05-22-2008, 08:25 PM   #2
Beginner
 
leiming's Avatar
 
Join Date: Jan 2008
Location: Fujian, China
Posts: 23
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......
leiming is offline   Reply With Quote
Old 05-22-2008, 08:33 PM   #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 */
birkelbach is offline   Reply With Quote
Old 05-22-2008, 09:35 PM   #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
wd_kendrick is offline   Reply With Quote
Old 05-23-2008, 02:41 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Also note that it is a bad idea to cast the return of malloc in a C program (explanation is in FAQ).
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
c programming, calloc, malloc, memory, realloc

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
writing a pack-style function, any advices? isaac_s C Programming 10 07-08-2006 08:09 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
using realloc bobthebullet990 C Programming 14 12-06-2005 05:00 PM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM
half ADT (nested struct) problem... CyC|OpS C Programming 1 10-26-2002 08:37 AM


All times are GMT -6. The time now is 03:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22