C언어의 구조체가 소멸하는 문제

-@Naver의 이미지

C언어로 단어들의 집합을 만드는 중에 구조체와 관련된 문제가 생겼습니다.
메인에서 두 개의 구조체 포인터(S1, S2)를 선언하고 CreateSet()으로 초기화했습니다.
초기화 직후에는 %p로 출력해보았을때 둘 다 주소가 잘 찍히는데, 함수에서 접근한 직후로는 S2 포인터가 NULL이 되어버립니다.
아무리 생각해봐도 왜 NULL값으로 변하는지 잘 모르겠어서 질문남깁니다.. ㅠㅠ 코드 첨부합니다.
어떻게 해야 정상적으로 작동할까요?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define defaultSize 5
 
typedef enum {FALSE, TRUE} Boolean;
 
typedef struct {
	int nElements;
	int arraySize;
	char **element;
} strset_t;
 
strset_t *createSet()
{
	strset_t *s = (strset_t *)malloc(sizeof(strset_t));
	s->element = (char **)malloc(sizeof(char *) * defaultSize);
 
	s->nElements = 0;
	s->arraySize = defaultSize;
 
	return s;
}
 
void deleteSet(strset_t *s)
{
	free(s->element);
	free(s);
}
 
Boolean isEmpty(strset_t *s)
{
	if(s->nElements == 0)
		return TRUE;
	return FALSE;
}
 
int cardinality(strset_t *s)
{
	return s->nElements;
}
 
int isElement(char *x, strset_t *a)
{
	for(int i = 0; i < a->nElements; i++)
		if(strcmp(x, a->element[i]) == 0)
			return i;
	return -1;
}
 
Boolean addElement(char *x, strset_t *a)
{
	if(a->nElements == a->arraySize){
		a->arraySize *= 2;
		a = realloc(a, a->arraySize);
 
		puts("== extended ==");
	}
 
	if(a == NULL){
		puts("=== NULL ===");
		return FALSE;
	}
 
	a->element[a->nElements] = (char *)malloc(strlen(x) + 1);
	strcpy(a->element[a->nElements], x);
	a->nElements++;	//deep copy를 사용하여 값 전달
 
	return TRUE;
}
 
Boolean removeElement(char *x, strset_t *a)
{
	int idx = isElement(x, a);
 
	if(idx == -1){
		puts("=== Not found ===");
		return FALSE;
	}
 
	for(int i = idx; i < a->nElements - 1; i++){
		free(a->element[i]);
		a->element[i] = a->element[i + 1];
	}
 
	free(a->element[a->nElements - 1]);
	a->nElements--;
}
 
 
void showAll(strset_t *a, strset_t *b)
{
	int i;
 
	printf("A[%d] : ", a->arraySize);
	for(i = 0; i < a->nElements; i++)
		printf("%s ", a->element[i]);
 
	printf("\nB[%d] : ", b->arraySize);
	for(i = 0; i < b->nElements; i++)
		printf("%s ", b->element[i]);
 
}
 
int main()
{
	strset_t *S1, *S2;
 
	S1 = createSet();
	S2 = createSet();
 
	Boolean run = TRUE;
 
	char target, selection;
	char buff[100];
 
	while(run){
		puts("====== MENU ======");
		puts("0. Show All");
		puts("1. Add element");
		puts("2. Remove element");
		puts("3. Check the element(valid or not)");
		puts("4. Check the subset(valid or not)");
		puts("5. Compare and match the sets");	//Equal
		puts("6. Check the disjoint");
		puts("7. Make intersection");
		puts("8. Make Union");
		puts("9. Make difference");
 
		printf("%p %p\n", S1, S2);
 
		fflush(stdin);
 
		switch (getchar()) {
		case '1':
			printf("Target(1, 2)) : ");
			scanf("%d", &target);
			fflush(stdin);
			printf("Element : ");
			if(target == 1)
				addElement(gets(buff), S1);
			else if(target == 2)
				addElement(gets(buff), S2);
 
			break;
 
		case '2':
			printf("Target(1, 2)) : ");
			scanf("%d", &target);
			fflush(stdin);
			printf("Element : ");
			if(target == 1)
				removeElement(gets(buff), S1);
			else if(target == 2)
				removeElement(gets(buff), S2);
 
			break;
 
		case '0':
			showAll(S1, S2);
			break;
 
		case 'X':
			run = FALSE;
			break;
 
		default:
			puts("== Invalid menu ==");
			getchar();
			break;
		}
 
		printf("\n");
	}
 
	deleteSet(S1);
	deleteSet(S2);
 
	system("pause");
    return 0;
}
peecky의 이미지

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