
Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch4 / malloc
.C #include <iostream.h>
#include <string.h>
#include <malloc.h>
#include <malloc.h>
int main()
{
char** inList = 0, buf[256];
int numIn = 0, max_size = 0;
/* get all input lines from a user */
while (cin.getline(buf, sizeof(buf))) {
if ( ++numIn > max_size ) {
max_size += 2;
if ( !inList )
inList = (char**)calloc( max_size,sizeof(char*) );
else
inList = (char**)realloc( inList,sizeof(char*)*max_size );
}
/* store an input line */
inList[numIn-1] = (char*)malloc( strlen( buf )+1 );
strcpy( inList[numIn-1], buf );
}
/* now print back all input lines from a user */
while ( --numIn >= 0 ) {
cout << numIn << ": " << inList[numIn] << endl;
free( inList[numIn] );
}
free ( inList );
return 0;
}