c언어 문자열 malloc에 대하여...

익명 사용자의 이미지

for(int i=0; i<word_num[n]; i++){
	char * ptr = (char *)malloc(sizeof(char)*200);  //free 어떻게 호출하지?  
	fgets(ptr, 200, fp);
	*(ptr + strlen(ptr)-1) = '\0';
	Word_list[i] = ptr;
}

이런 식으로 파일입출력으로
111
222
333
이런 식이면 한문장씩 동적으로 할당해

shint의 이미지


- 각 함수에 인자값. 리턴값. 오류값 확인
- 각 데이터값을 출력해서 확인

- 문자열은 마지막 값이 NULL 혹은 0x00 이어야 문자열로 인식 됩니다.

- 배열의 크기 data[5] 5개라면. 아래와 같이 사용합니다.
- 배열의 순서 data[0] data[1] data[2] data[3] data[4]
- data[4] = 0x00; 으로 문자열에 마지막을 지정할 수 있습니다.

- strncpy() 는 크기를 정해서. 문자열 복사가 가능합니다.

- #if 0
- #endif 는 주석과 같습니다.

- #if 1
- #endif 는 사용과 같습니다.

http://codepad.org/BX9U5fH0

#include <stdio.h>
 
int main()
{
 
#if 0
    char * p = NULL;
    p = (char*) malloc (5);
    strcpy(p, "12345");
    printf("%s\n", p);
    free(p);
 
    //memory clobbered past end of allocated block
    //Exited: ExitFailure 127
 
    char * p = NULL;
    p = (char*) malloc (5);
    strncpy(p, "12345", 5);
    printf("%s\n", p);
    free(p);
    //12345�
 
 
    char * p = NULL;
    p = (char*) malloc (5+1);
    memset(p, 0x00, 6);
    *(p+6) = 0x00;
    strncpy(p, "12345", 5);
    printf("%s\n", p);
    free(p);
    //memory clobbered past end of allocated block
    //Exited: ExitFailure 127
 
#endif
 
    char * p = NULL;
    p = (char*) malloc (5+1);
    memset(p, 0x00, 6);
    *(p+5) = 0x00;
    strncpy(p, "12345", 5);
    printf("%s\n", p);
    free(p);
    //12345
 
    return 0;
}

KLDP - malloc 검색
https://kldp.org/search/google/malloc

구글 - malloc 검색
https://www.google.com/search?source=hp&ei=xlqJW6rQLoum8QWV_rmYAg&q=malloc&oq=malloc&gs_l=psy-ab.3..0l10.696.3524.0.3661.7.7.0.0.0.0.122.768.2j5.7.0....0...1.1.64.psy-ab..0.7.767...0i131k1.0.yphAdYPxPLQ

구글 - free() 검색
https://www.google.com/search?ei=JFuJW4CcE4mY8gWhoJioDw&q=free%28%29&oq=free%28%29&gs_l=psy-ab.3..0l10.23344.23418.0.23860.2.2.0.0.0.0.133.236.0j2.2.0....0...1.1.64.psy-ab..0.2.235....0.bO6WU0aCneA

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

김정균의 이미지

Word_list 를 strdup 로 받으면 되겠죠.

Word_list[i] = strdup(ptr);
free(ptr);

물론 Word_list 를 어떻게 free 할 것인지도 고민을 해 두어야 겠지요.

댓글 달기

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