473,396 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

realloc() help

1
I can't figure out what might be wrong in that piece of code. The window keeps shutting down after having entered a few integers and -1 (in order to terminate the function). Am I using realloc() improperly or what? Thanks in advance for any help?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <stdio.h>
  4. using namespace std;
  5. #define CHUNKSZ 5
  6.  
  7. void printScores( int ** );
  8. void getScores( int** );
  9.  
  10. int main()
  11. {    
  12.     int *score = NULL;
  13.  
  14.     getScores( &score );
  15.  
  16.     printScores( &score );    
  17.  
  18.     system("pause");
  19. }
  20.  
  21. void getScores( int ** test )
  22. {
  23.      int score;
  24.      int i = 0;
  25.      int number;
  26.      int* hold = NULL;
  27.  
  28.      number = CHUNKSZ;
  29.      *test = ( int* )malloc( sizeof( int ) * CHUNKSZ );
  30.  
  31.      while(9)
  32.       {
  33.         for( ; i < number; i++ )
  34.            {  cout << endl <<"Enter a score ( -1 to stop )"<<i<<" : ";
  35.               cin >> score;
  36.               if ( score == -1 )
  37.                 {  (*test)[i] = -1;
  38.                     return;
  39.                  }
  40.              (*test)[i] = score;
  41.            }  
  42.  
  43.         if( ( hold = ( int* )realloc( *test , sizeof( int ) * CHUNKSZ )) == NULL)
  44.           cout << endl << "Allocation error." ;
  45.         else { *test = hold; 
  46.                number += CHUNKSZ;
  47.                hold=NULL; }
  48.      }
  49. }
  50.  
  51. void printScores( int **test ) 
  52. {
  53.      int i = 0;
  54.      for( ; *( *test + i ) != -1; i++ )
  55.         cout << endl <<"Score : " << *( *test + i );
  56.      cout << endl; 
  57. }
  58.  
Feb 19 '07 #1
1 1297
Ganon11
3,652 Expert 2GB
Please don't double post your question - your second thread has been deleted.
Feb 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Henrik J | last post by:
Hello group... I have this little problem: I'm using a struct **foo. I have allocated x foo's using malloc: foo=(FOO**)malloc(Amount*sizeof(FOO*)); No problem....!
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
18
by: ifmusic | last post by:
I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct: typedef struct{ int socket; char...
2
by: solanki.chandrakant | last post by:
hi i have fedora linux 4 and i have simple realloc program which i am included here... plz help me to overcome the realloc to pointer to character variable. The program working 2 times but more...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.