구조체 동적 할당 방법?

ssik425의 이미지

링크드 리스트 등을 사용하지 않고 동적 할당 자료형을 구현 해보려고 하는데
안되네요.. 아직은 허접인지라 :oops:
코드는 아래와 같습니다. 여러분들은 어떻게 처리하시나요?
폴트가 나는거 보니 제가 잘못한거 같은데 뭐가 틀린 거죠?

/* 구조체 동적 할당 testing */
typedef struct Cnt {
   int num1;
   char num2[10];
}COUNT;

int main(void)
{
   int i=0;

   COUNT *cnt;

   cnt=(COUNT *)malloc( sizeof(COUNT));

   save_data(cnt);

   for(i=1;i<100;i++)
      printf("%d %s \n", cnt[i].num1, cnt[i].num2);
   return 0;
}

int save_data(COUNT *cnt)
{
   int i=0;
   for(i=1;i<100;i++)
   {
      cnt=(COUNT *)realloc(cnt, (sizeof(COUNT) * i));
      memset(cnt + (sizeof(COUNT)*i), 0x00,sizeof(COUNT));
      cnt[i].num1=i;
      sprintf(cnt[i].num2, "%d", i);
   }
   return 0;
}
sozu의 이미지

Quote:
cnt=(COUNT *)realloc(cnt, (sizeof(COUNT) * (i + 1)));
memset(&cnt[i], 0x00,sizeof(COUNT));

이렇게 하면 되긴되지만..

차라리 링크드 리스트를 쓰시는게 정신 건강에 좋으실듯 8)

-----------
청하가 제안하는 소프트웨어 엔지니어로써 재미있게 사는 법
http://sozu.tistory.com

ssik425의 이미지

넵 감사합니다.
링크드 리스트를 쓰기에는 뭔가 허전 해서요 ^^

개발자들의 궁극적 비전은 ?

doldori의 이미지

포인터 연산을 잘못 하셨군요.

int save_data(COUNT *cnt)
{ 
   for(i=0;i<100;i++)
   { 
      cnt=(COUNT *)realloc(cnt, (sizeof(COUNT) * (i+1)));
      memset(cnt + i, 0x00, sizeof(COUNT));  // 여기
      cnt[i].num1=i; 
      sprintf(cnt[i].num2, "%d", i); 
   } 
   return 0; 
}

cnt가 COUNT*이므로 cnt + i는 cnt + sizeof(COUNT)*i 번지를 가리키는
COUNT*가 됩니다.
어떤 자료구조를 구현하려고 하시는지는 잘 모르겠지만 realloc을 루프 안에
넣는 것은 별로 좋지 않은 방법입니다. 미리 필요한 메모리를 계산해서 한 번에
할당하든지, 처음부터 좀 넉넉하게 할당하든지 해서 realloc을 호출하는 횟수를
줄이는 편이 좋습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.