C에서 메모리 해제시 에러가 발생..

foxfree의 이미지

안녕하세요.

지뢰찾기 프로그램을 짜는데.. 알수없는 에러가..

#include <stdio.h>
#include <stdlib.h>
 
void printBoard(char **board, int h, int w) {
	int i, j;
	char* jChar;
	for (i=0; i< h; i++) {
		jChar = *(board + i);
		for (j=0; j < w; j++) {
			printf("%c ", *(jChar+(sizeof(char)*j)));
		}
		printf("\n");
	}
}
 
int mainMenu(char **board) {
	int h, w, i, j;
	char c; 
	scanf("%d %d", &h, &w);
	if (h <= 0) return 0;
 
	board = malloc(sizeof(char)*h);
	for (i=0; i < h; i++) {
		*(board+(i*sizeof(char))) = (char*)malloc(sizeof(char)*w);
	}
 
	i = j = 0;
	while (i < h) {
		while (j < w) {
			scanf("%c", &c);
			if (c == 10 || c == 32) continue; // 스페이스나 엔터는 무시
			board[i][j] = c;
			j++;
		}
		fflush(stdin);
		j = 0;
		i++;
	}
 
 
	printBoard(board, h, w);
 
 
	//메모리 해제
	for (i=0; i < h; i++) {
		free (*(board+(i*sizeof(char))));
		*(board+(i*sizeof(char))) = NULL;
	}
	free (board);
	board = NULL;
 
	return 1;
 
}
 
void main() {
	char **board = NULL;
	while(mainMenu(board) == 1); // 메인 입력메뉴 루프
}

코드는 위와 같은데요..

디버그 에러가 나네요.
DAMAGE : after normal block (#45) at 0x...

도저히 무슨 문제인지 깜깜해서 이렇게 질문드립니다.. ㅠ.ㅡ

klara의 이미지

for문에서 *(board+(i*sizeof(char))) 대신에 *(board+i)라고 해보세요.
그냥 board[i]라고 하는게 헷갈리지도 않고 편한데 왜 굳이 포인터연산을 직접 하셨는지=_=;;

왜 i*sizeof(char)가 아니라 i인지는 포인터+정수가 어떻게 돌아가는지를 찾아보시면 되겠습니다.

jick의 이미지

사실 sizeof(char)는 무조건 1이기 때문에, 저렇게 해도 돌아가기는 돌아갑니다.

즉 개념상 잘못된 코드이지만 오동작을 하지는 않는다는 얘기죠. (단지 저 줄만 놓고 볼 때.)

foxfree의 이미지

아.. 그렇게 쉽게 하는방법이.. _-_;
골머리 썩다보니 희안한 코드 짜고 있었습니다..
감사!!

bushi의 이미지

board = malloc(sizeof(*block)*h);

OTL

foxfree의 이미지

board = malloc(sizeof(*block)*h);
에서 block 은 어떤것을 말씀하시는지..
w*h 인가요? 가로x세로??

bushi의 이미지

오타 -.-;
board = malloc(sizeof(*board)*h);

OTL

foxfree의 이미지

//메모리 해제
for (i=0; i < h; i++) {
		free (board[i]);
		*(board+(i*sizeof(char))) = NULL;
	}
//	free (board);
	board = NULL;

에서 free (board); 부분만 빼면 에러 안나오는데,
이게 맞는건가요??
board = malloc(sizeof(char)*h);
를 해줬었는데, 저부분의 메모리를 해제해야하는거 아닌지??

klara의 이미지

malloc이 잘못되었기 때문에 free에서 에러가 나는 것입니다.
board = malloc(sizeof(char)*h);
for (i=0; i < h; i++) {
*(board+(i*sizeof(char))) = (char*)malloc(sizeof(char)*w);
}

board = (char **)malloc(sizeof(char*)*h);
for (i=0; i < h; i++) {
*(board+i) = (char*)malloc(sizeof(char)*w); // same as board[i] = (char*)malloc(sizeof(char)*w);
}
로 고쳐보세요.

foxfree의 이미지

아.. 잘됩니다. ㅠ.ㅠ
너무 감사드립니다!!

댓글 달기

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