All pastes #1336018 Raw Edit

malloc.c

public c v1 · immutable
#1336018 ·published 2009-02-13 12:52 UTC
rendered paste body
#include <stdlib.h>		/* malloc				*/#include <stdio.h>int main(void){	/* Allocate space for an array with ten elements of type int. */	int *ptr1 = malloc(1 * 1024 * 1024 * 1024);	if (ptr1 == NULL) {		printf("1 Memory could not be allocated, the program should handle the error here as appropriate.\n");	} else {		printf("1 Allocation succeeded.  Do something.\n");		int *ptr2 = malloc(1 * 1024 * 1024 * 1024);		if (ptr2 == NULL) {			printf("2 Memory could not be allocated, the program should handle the error here as appropriate.\n");		} else {			printf("2 Allocation succeeded.  Do something.\n");			int *ptr3 = malloc(1 * 1024 * 1024 * 1024);			if (ptr3 == NULL) {				printf("3 Memory could not be allocated, the program should handle the error here as appropriate.\n");			} else {				printf("3 Allocation succeeded.  Do something.\n");				int *ptr4 = malloc(1 * 1024 * 1024 * 1024);				if (ptr4 == NULL) {					printf("4 Memory could not be allocated, the program should handle the error here as appropriate.\n");				} else {					printf("4 Allocation succeeded.  Do something.\n");					int *ptr5 = malloc(1 * 1024 * 1024 * 1024);					if (ptr5 == NULL) {						printf("5 Memory could not be allocated, the program should handle the error here as appropriate.\n");					} else {						printf("5 Allocation succeeded.  Do something.\n");						int *ptr6 = malloc(1 * 1024 * 1024 * 1024);						if (ptr6 == NULL) {							printf("6 Memory could not be allocated, the program should handle the error here as appropriate.\n");						} else {							printf("6 Allocation succeeded.  Do something.\n");							int *ptr7 = malloc(1 * 1024 * 1024 * 1024);							if (ptr7 == NULL) {								printf("7 Memory could not be allocated, the program should handle the error here as appropriate.\n");							} else {								printf("7 Allocation succeeded.  Do something.\n");								int *ptr8 = malloc(1 * 1024 * 1024 * 1024);								if (ptr8 == NULL) {									printf("8 Memory could not be allocated, the program should handle the error here as appropriate.\n");								} else {									printf("8 Allocation succeeded.  Do something.\n");									int *ptr9 = malloc(1 * 1024 * 1024 * 1024);									if (ptr9 == NULL) {										printf("9 Memory could not be allocated, the program should handle the error here as appropriate.\n");									} else {										printf("9 Allocation succeeded.  Do something.\n");										int *ptr10 = malloc(1 * 1024 * 1024 * 1024);										if (ptr10 == NULL) {											printf("10 Memory could not be allocated, the program should handle the error here as appropriate.\n");										} else {											printf("10 Allocation succeeded.  Do something.\n");											int *ptr11 = malloc(1 * 1024 * 1024 * 1024);											if (ptr11 == NULL) {												printf("11 Memory could not be allocated, the program should handle the error here as appropriate.\n");											} else {												printf("11 Allocation succeeded.  Do something.\n");												int *ptr12 = malloc(1 * 1024 * 1024 * 1024);												if (ptr12 == NULL) {													printf("12 Memory could not be allocated, the program should handle the error here as appropriate.\n");												} else {													printf("12 Allocation succeeded.  Do something.\n");													int *ptr13 = malloc(1 * 1024 * 1024 * 1024);													if (ptr13 == NULL) {														printf("13 Memory could not be allocated, the program should handle the error here as appropriate.\n");													} else {														printf("13 Allocation succeeded.  Do something.\n");														int *ptr14 = malloc(1 * 1024 * 1024 * 1024);														if (ptr14 == NULL) {															printf("14 Memory could not be allocated, the program should handle the error here as appropriate.\n");														} else {															printf("14 Allocation succeeded.  Do something.\n");															int *ptr15 = malloc(1 * 1024 * 1024 * 1024);															if (ptr15 == NULL) {																printf("15 Memory could not be allocated, the program should handle the error here as appropriate.\n");															} else {																printf("15 Allocation succeeded.  Do something.\n");																int *ptr16 = malloc(1 * 1024 * 1024 * 1024);																if (ptr16 == NULL) {																	printf("16 Memory could not be allocated, the program should handle the error here as appropriate.\n");																} else {																	printf("16 Allocation succeeded.  Do something.\n");																	int *ptr17 = malloc(1 * 1024 * 1024 * 1024);																	if (ptr17 == NULL) {																		printf("17 Memory could not be allocated, the program should handle the error here as appropriate.\n");																	} else {																		printf("17 Allocation succeeded.  Do something.\n");																		int *ptr18 = malloc(1 * 1024 * 1024 * 1024);																		if (ptr18 == NULL) {																			printf("18 Memory could not be allocated, the program should handle the error here as appropriate.\n");																		} else {																			printf("18 Allocation succeeded.  Do something.\n");																			free(ptr18);																		}																		free(ptr17);																	}																	free(ptr16);																}																free(ptr15);															}															free(ptr14);														}														free(ptr13);													}													free(ptr12);												}												free(ptr11);											}											free(ptr10);										}										free(ptr9);									}									free(ptr8);								}								free(ptr7);							}							free(ptr6);						}						free(ptr5);					}					free(ptr4);				}				free(ptr3);			}			free(ptr2);		}		free(ptr1); /* We are done with the int objects, and free the associated pointer.  			       The pointer must not be used again. */	}	printf("Ende!\n");	return 0;}/* EOF */