구조체를 동적 메모리 할당을 해서 free를 할경우 에러가 발생합니다. 아르켜 주세요~~

joungpil의 이미지

아래 소스를 보시면 //param1 = (param_t*)malloc(sizeof(param_t*));
구조체를 동적 메모리 할당을 했는데요. malloc을 사용했기 때문에 , free를 사용하는데
세그멘테이션 에러가 발생하네요. 책에는 반드시 malloc을 사용할 경우
free를 사용해야 한다고 해서 사요한건데요. 이거 왜그런지 잘 모르겠습니다.
고수분들의 조언과 답변을 부탁드릴게요~^^
=============================================================================================
=============================================================================================
#include
#include
#include

//gcc `pkg-config --libs glib-2.0 --cflags glib-2.0` -g -o glist_exam3 glist_exam3.c

typedef struct _param_t{
char* name;
char* address;
}param_t;

param_t* param1;
param_t* param2;

GList* extern_list = NULL;
GList* extern_node = NULL;

void get_param(){
param1->name = "get_name";
param1->address = "get_address";

extern_list = g_list_append(extern_list , param1);
}

void put_param(){

extern_node = g_list_first(extern_list);
param2 = (param_t*)(extern_node->data);

printf("param2->name is %s\n" , param2->name);
printf("param2->address is %s\n" , param2->address);

//GList 메모리 해제해도 세그멘테이션 에러가 발생하네요. 왜 그럴까요?
//g_free(extern_list);
//g_free(extern_node);
}

int main(){
char* data1 = "aaa";
char* data2 = "bbb";
char* data3 = "ccc";

GList* list = NULL;

param1 = (param_t*)malloc(sizeof(param_t*));
param2 = (param_t*)malloc(sizeof(param_t*));

list = g_list_append(list , data1);
list = g_list_append(list , data2);
list = g_list_append(list , data3);

GList* node;
g_print("%d\n" , g_list_length(list));
node = g_list_nth(list , 1);

g_print("%s\n" , (char*)(node->data));

node = g_list_find(list , data3);
g_print("%s\n" , (char*)(node->data));

get_param();
put_param();

//GList 메모리 해제해도 세그멘테이션 에러가 발생하네요. 왜 그럴까요?
//g_free(list);
//g_free(node);

//구조체를 동적 메모리 할당을 해서 free를 할경우 세그멘테이션 에러가 발생하네요. 왜그럴까요?
//free(param1);
//free(param2);
return 0;
}

M.W.Park의 이미지

일단 list는 g_list_free를 사용하셔야합니다.

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

mg2000의 이미지

저 같은 경우는 거의 대부분이 할당한 메모리 영역을 벗어나서 쓴 경우...

댓글 달기

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