왜 이렇게 메모리 초기화을 하는지요?

hurryon의 이미지

#include "list.h"

int add_data(data_t data);

int add_data(data_t data)
{
    list_t *new_node;

    if((new_node = (list_t *)(malloc(sizeof(list_t) + strlen(data.string) + 1))) == NULL)
        return(-1);

    new_node->item.time = data.time;
    new_node->item.index = data.index;
    new_node->item.string = (char *)(new_node + sizeof(list_t)); // 이 부분이 뭘하는건지요? 단지 문자열이 들어 갈만큼의 메모리 초기화 인지요?

    printf("index:%d\nstring:%s\n", new_node->item.index, new_node->item.string);
    printf("value:%d\n", new_node + sizeof(list_t));

    return(0);
}

int main(int argc, char *argv[])
{
    data_t o0o;

    o0o.index = 1;

    add_data(o0o);

    return(0);
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <sys/types.h>
#include <time.h>

typedef struct data_struct
{
    time_t time; 
    char *string;
    int index;
} data_t;
    
typedef struct list_struct
{
    data_t item;
    struct list_struct *next;
} list_t;

주석을 달아 놓은 부분이 의문입니다. 출력 결과는 아래와 같습니다.

index:1
string:
value:134518760
pynoos의 이미지

malloc 에서부터 자세히 보시면, string을 control하기 위한 node_t와 string 자체를 위한 공간을 모두 확보하고, control 하기 위한 node의 string위치를 malloc에서 확보한 뒷부분을 가리키도록 되어 있군요.

whenicu의 이미지

string의 address 를 잡아주는 부분
이 맞는거 같습니닷 ^^;

메모리 낭비가 있는 코드군여..

moonzoo의 이미지

시간이 지나서 이미 해결하신 듯 하지만 ..

위의 리스트 구조체 뒤에 Data Block를 붙여놓은 형태네요.

: list_t : Data Block :

위와 같은 구조를 malloc 으로 할당하고
malloc(sizeof(list_t) + strlen(data.string) + 1))

list_t의 멤버인 .string 이 Data Block의 처음을 가리키려는 것입니다.
(char *)(new_node + sizeof(list_t));

다시 말하자면 new_node 로부터 list_t만큼 떨어진 곳의 주소이므로

Data Block의 처음을 가리키겠다는 의도입니다.

hurryon의 이미지

whenicu wrote:
string의 address 를 잡아주는 부분
이 맞는거 같습니닷 ^^;

메모리 낭비가 있는 코드군여..

음...왜 이렇게 메모리를 잡아 놓았는지...냠냠...그나저나 메모리 낭비가 없는 코드을 작성하려면 위의 예에서는 어떻게 해야 할지요? ㅡㅡ;

pynoos의 이미지

add_data 함수에 두 가지 정도 오류가 있습니다.

받은 data 변수에 원하는 string이 들어 있습니다만, 그것이 새로 할당되는 list에 복사되지는 않는 군요.

새로 할당된 list 변수가 return 되어야할 것 같은데 혹은, global linked list에 들어가야할 것 같은데 pointer를 놓치고 있습니다.

hurryon의 이미지

pynoos wrote:
add_data 함수에 두 가지 정도 오류가 있습니다.

받은 data 변수에 원하는 string이 들어 있습니다만, 그것이 새로 할당되는 list에 복사되지는 않는 군요.

새로 할당된 list 변수가 return 되어야할 것 같은데 혹은, global linked list에 들어가야할 것 같은데 pointer를 놓치고 있습니다.

아...지적하신 오류는 제가 올린 소스 코드가 일부분이기 때문에...으흐...발생된거 같네요. 전 단지 주석을 달아 놓은 부분이 왜 저렇게 잡아야 하는지 궁금했기 때문이였습니다. 으흠. 그나저나 위에 다른분께서 지적하신 메모리 누수는 별 관계가 없는 이야기인지요?

체스맨의 이미지

코드를 뜯어보진 않았지만, 메모리 누수가 있다면, 코드 자체의 문제일 것이구요...

이 방법은 오히려 메모리 낭비를 줄이는 방법입니다. 예를 들어, 좀 극단적인 얘기지만, 1바이트를 10번할당한 것과, 10바이트를 한번 할당한 것은 후자가 메모리를 덜 잡아먹으니까요. 지금 예는 두번 할당을 한번할당으로 줄였지요.

어쨌든, 그런 이유로 위와같이 한 것인데, 위의 예는 1바이트 문자열에 대해 그렇게 해서 다행히 문제가 없지만, 2바이트 이상의 타입인 경우는 bus error 가 발생할 수도 있습니다.

대개는 다음과 같은 방법이 더 자주 쓰입니다. 제시하신 예처럼 문자열이 하나라면 포인터 멤버를 둘 필요도 없어서 메모리가 더 절약되죠.

struct list_t {
    ...;
    char s[1];
};

char* s = "Hello, world";
struct list_t* l = (struct list_t*)
    malloc( sizeof(struct list_t)+strlen(s) );
strcpy( l->s, s );

Orion Project : http://orionids.org

ㅡ,.ㅡ;;의 이미지

체스맨 wrote:
코드를 뜯어보진 않았지만, 메모리 누수가 있다면, 코드 자체의 문제일 것이구요...

이 방법은 오히려 메모리 낭비를 줄이는 방법입니다. 예를 들어, 좀 극단적인 얘기지만, 1바이트를 10번할당한 것과, 10바이트를 한번 할당한 것은 후자가 메모리를 덜 잡아먹으니까요. 지금 예는 두번 할당을 한번할당으로 줄였지요.

어쨌든, 그런 이유로 위와같이 한 것인데, 위의 예는 1바이트 문자열에 대해 그렇게 해서 다행히 문제가 없지만, 2바이트 이상의 타입인 경우는 bus error 가 발생할 수도 있습니다.

대개는 다음과 같은 방법이 더 자주 쓰입니다. 제시하신 예처럼 문자열이 하나라면 포인터 멤버를 둘 필요도 없어서 메모리가 더 절약되죠.

struct list_t {
    ...;
    char s[1];
};

char* s = "Hello, world";
struct list_t* l = (struct list_t*)
    malloc( sizeof(struct list_t)+strlen(s) );
strcpy( l->s, s );

4바이트가 이득일수 있으나 질문자의 코드에서는 구조체의 구조상 저렇게 처리할수 없는구조입니다. 낭비라 할수 없죠.
가변데이터 다음에 다른 데이터가 있기때문이죠..
물론 구조체맴버의 순서를 바꾸면되겠지만.. 겨우4바이트 아끼기위해 그러고 싶지않은경우가 있죠..


----------------------------------------------------------------------------

댓글 달기

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