Programing - C/C Basic grammar

094 memcmp 두메모리 비교

junmoyo 2017. 7. 28. 14:21

개념 :


strcmp 와 같이 비교를 합니다 대상이 메모리 데이터 자체냐 아니면 문자열이냐의 차이점을 두고있습니다. 사실 둘의 차이는 가독성에 문제이지 전혀 


다른기능을 하는 것이 없이 동일합니다.




소스코드 : main.c

#include <stdio.h>

#include <string.h>

#include <malloc.h>


int main(void)

{

char *pString = "Hello JunmoZzi";

char *pAdd = " ^^a ha ha ha!";


char *pBuffer = NULL;

int nSize1 = strlen(pString) + 1;

int nSize2 = strlen(pAdd) + 1;

pBuffer = calloc(nSize1 + 1, sizeof(char));


if (pBuffer == NULL)

{

puts("memory 할당 실패");

}

else

{

puts("memory 할당 성공");

printf("메모리 주소 : %d\n", pBuffer);

//strcpy_s(pBuffer, nSize1, pString);

memcpy_s(pBuffer, nSize1, pString, nSize1);

}


puts("Run memcmp : pBuffer, pStirng");

if (memcmp(pBuffer, pString, nSize1) == 0)

{

puts("동일한 메모리");//동일한 데이터

}

else

{

puts("동일하지 않는 메모리");

}


puts(pBuffer);

puts("메모리 재 할당");


realloc(pBuffer, nSize1 + nSize2);


pBuffer[nSize1 - 1] = ' ';// 문자열의 끝 \0을 공백으로 변경


 //strcat_s(pBuffer, nSize1+nSize2, pAdd);

memcpy_s(pBuffer + nSize1, nSize2, pAdd, nSize2);

printf("메모리 주소 : %d\n", pBuffer);

puts(pBuffer);


free(pBuffer);

pBuffer = NULL;

return 0;

}




결과


 #c언어, #c언어입문, #프로그램입문, #memcmp, #메모리검사, #메모리비교함수