![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 11
| Problem with realloc 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;
}
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 | |
| | #2 |
| Beginner 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 | |
| | #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 | |
| | #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)); ...Also, thanks for the error checking hints...always a good idea |
| wd_kendrick is offline | |
| | #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:
| |
| Elysia is offline | |
![]() |
| Tags |
| c programming, calloc, malloc, memory, realloc |
| Thread Tools | |
| Display Modes | |
|
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 |