횽들 rle압축방식을 c언어로 코딩해 봤는데
문자열을 입력받을 때 배열을 사용해서 받았거든
근데 이게 너무 메모리낭비가 심한거같아서 동적할당으로 받으려고해
근데 c++이라면 new delete써서 하면 되겠는데
c언어는 어떻게해야하는지 모르겠어..
malloc을 쓰면 된다는데 아직 초보라 잘 모르겠어 ㅠㅠ
코드를 어떻게짰냐면..
#include <stdio.h>
#include <string.h>
int main()
{
char rle[1024];
char *pt;
int i, count;
i = 0;
count = 1;
printf(\"input : \");
pt = gets(rle);
printf(\"output : \");
for(i=1; i<=strlen(rle); i++)
{
if(rle[i-1]==rle[i])
{
count++;
}
else
{
if(count==1)
{
printf(\"%c\", rle[i-1]);
}
else
{
printf(\"%c%d\",rle[i-1], count);
count=1;
}
}
}
printf(\"\\n\");
system(\"pause\");
return 0;
}
이렇게 했거든?
어떻게하면 메모리낭비를 줄일 수 있을까? ㅠㅠ
그냥 받아서 배열에 저장하고 남은 배열공간 삭제하면 되는거 아니야?
C언어? malloc,free 개새끼야