#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif // _DEBUG #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif // _DEBUG int* allocateArray_1(const int size,const int val) { int* arr = (int*)malloc(size * sizeof(int)); for(int i=0;i<size;i++) { arr[i]=val; } return arr; } int* allocateArray_2(int *arr,int size,int val) { if(arr != NULL) { for(int i=0;i<size;i++) { arr[i]=val; } } return arr; } void allocateArray_3(int **arr,int size,int val) { *arr = (int*)malloc(size* sizeof(int)); if(*arr != NULL) { for(int i=0;i<size;i++) { *(*arr+i)=val; } } } int main() { int* vector = allocateArray_1(5,45); int* vector_1 = (int*)malloc(5*sizeof(int)); int* vector_2=NULL; // for(int i=0;i<5;i++) { printf("addr :%x ------------ value : %d \r\n",(vector+i),*(vector+i)); } free(vector); allocateArray_2(vector_1,5,30); for(int i=0;i<5;i++) { printf("addr :%x ------------ value : %d \r\n",(vector_1+i),*(vector_1+i)); } free(vector_1); allocateArray_3(&vector_2,5,20); for(int i=0;i<5;i++) { printf("addr :%x ------------ value : %d \r\n",(vector_2+i),*(vector_2+i)); } free((vector_2)); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); return 0; }