Gyuhhjooo

rohi404의 이미지

Hhhh

 의 이미지

listpointer newnode = (listpointer)malloc(sizeof(newnode));

...

초보자들이 이런 실수를 심심찮게 저지르는 걸 보면 새삼 malloc이란 게 꽤나 쓰기 불편하구나 싶기도 해요.
뭐, C언어 철학 상 어쩔 수 없는 부분이고, 이제 와서 달리 어쩔 수도 없으니 그냥 조심해서 쓰는 수밖에요.

나그네나그네의 이미지

listpointer가 listnode *와 같으니 괜찮지 않을까용..?

 의 이미지

sizeof(newnode) = sizeof(listpointer) = sizeof(listnode *) < sizeof(listnode)

sizeof(listpointer)만큼의 메모리를 할당해놓고선 마치 sizeof(listnode)만큼 할당해 놓은 것처럼 쓰면 괜찮을까요, 안 괜찮을까요?

Heap corruption 일으키기 딱 좋은 코드인데, T *p = (T *)malloc(sizeof(T));꼴의 코드가 워낙에 번잡하다 보니 실수하기 너무 쉽습니다.
자바에서 너무 자주 나오는 구문 T obj = new T;도 두 번씩 타이핑하게 만드는 게 실수하기 쉽다고 까이는 판에...

rohi404의 이미지

그러게요 이런 실수를 하고 못찾고 있었네요 ㅠㅠ 감사합니다

rohi404의 이미지

위에는 해놓고 이런실수를... sizeof(*newnode)로 하니까 되네요

나그네나그네의 이미지

malloc했던 것을 마지막에 free 다 해주지 않으면 main() 끝날 때 에러가 뜹니다.

rohi404의 이미지

free하니까 free하는 부분에서 오류가 나더라구요 근데 메모리 할당을 잘못했네요 제가ㅋㅋㅋ...

김정균의 이미지

일단 gdb로 debugging을 하셔서 어디서 segfault가 발생하는지를 정확히 확인 해 보세요. 그런데 오류가 난다는 것이 segfault 인가요? 아니면 논리적 오류 인가요?

일단은 의심이 가는 부분이..

    if (fp == NULL){
        printf("error!\n");
    }

부분이네요. fp 가 NULL인데 code를 중지 시키지 않을테니 fread에서 segfault가 발생하겠죠.

그리고, 변수 초기화 시에

char file[100] = { NULL, };

이 부분도 컴파일러가 알아서 고쳐주기는 하겠지만,

char file[100] = { 0, };

으로 초기화를 해야 겠지요.

rohi404의 이미지

그부분은 다 잘됏는데 메모리할당이 문제였어요.. 아예 터져버려서ㅜㅜ

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.