#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <limits>

using namespace std;

void call(int i, int * test)
{
     cin >> test[i];
}



int main()
{
    
    int sizeOfArray;
      int *temp;  
     int i;
     cin >> sizeOfArray;
    int * test;
    string * command;
    command = new string[sizeOfArray];
    test = new int[sizeOfArray];

    for (int i=0; i<sizeOfArray ;) {
    cin >> command[i];
    if (command[i] == "call"){
                   call(i,test);}
    i++;
    if (i >= sizeOfArray) {
        sizeOfArray = sizeOfArray * 2;            
        int* temp = new int[sizeOfArray]; 
        for (int p=0; p<i; p++) {
            temp[p] = test[p]; 
            cout << temp[p] << endl;  
        }
        delete [] test;             
        test = temp;    
                   
    }
            
}

    


system("PAUSE");


}

this is my entire code,
i want the array expand when the memory used up, so that i can infinitely input number to it and it will output all it stores.

however, here is the result, input is bolded:
1
call 1
1
call 2 // <-- once i press enter ,my program got error here and terminate

try another input:
2
call 1
call 2
1
2
call 3// <-- again, the program terminate when i press enter


WHats wrong with my code... i need help!!!!!

Recommended Answers

All 3 Replies

You are only resizing the "test" array, you also need to resize the "command" array (or use command as a temporary string since you don't really need to keep a record of it anyways).

If you want an easier solution, I highly recommend you use std::vector (#include <vector>).

thanks for your reply, just got my program run happily :)
for others reference, i can post my code here, feel free to use

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <limits>
#include <new>


using namespace std;

void call(int i, int * test)
{
     cin >> test[i];
}



int main()
{
    
    int sizeOfArray;
      int *temp;  
     int * test;
    string * command;
     cin >> sizeOfArray;
    
    command = new string[sizeOfArray];
    test = new  int[sizeOfArray];

    for (int i=0; i<sizeOfArray ;) {
    cin >> command[i];
    if (command[i] == "call"){
                   call(i,test);}
    i++; 
    
    if (i >= sizeOfArray) {
        sizeOfArray = sizeOfArray * 2;            
        temp = new int[sizeOfArray]; 
        
        for (int p=0; p<i; p++) {
            temp[p] = test[p]; 
            cout << test[p] << endl;
        }
        delete [] test;    
        command = new string[sizeOfArray];
        test = new  int[sizeOfArray];         
        test = temp;    

                  
    }
            
}

    


system("PAUSE");


}

You have a memory leak in your code, you need to delete the "command" array before you expand it. This is bad because you have a loop that leaks memory at each iteration, this leads to a sustained growth of memory usage in time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.