코딩중에 메모리 할당되는 부분에서 계속 에러가 나서 질문드립니다....

wildwar의 이미지

현재 리눅스에서 find 명령어와 동일한 기능을 가지는 프록램을 구현하고 있는데

( 입력한 디렉토리의 하위디렉토리를 모두 들려서 찾고자 하는 파일명과 같은 파일이 존재하는지

확인 후 있을경우 그 파일의 pathname을 출력해주는 프로그램 )

[ 에러가 나는 부분의 function입니다 ]

static int myfunc(const char *pathname, const struct stat *statptr, int type)
{
	char tmpbuf[128];
	char *check;
	int result, copyLen;
	static char *fname, *copypath;
	regex_t namePattern;
 
	regcomp(&namePattern,name,REG_EXTENDED);//name은 찾고자 하는 파일명입니다
	fname=malloc(strlen(pathname));
	strcpy(copypath,pathname);
 
	check=strtok(copypath,"/");
	while(check!=NULL)
	{
		strcpy(fname, check);
		check-strtok(NULL,"/");
	}
	if(fname!=NULL)result=regexec(&namePattern,fname,0,NULL,0); //이 부분에서 에러가 나는듯 합니다
	free(fname);
	free(copypath)
	if(result==0)
		printf((%s\n",pathname);
	free(fname);
	free(copypath); // free는 일단 혹시나 하는 마음으로 아무데나 다 넣어봤습니다
	return 0;
}

( 기존에 있던 코드를 활용해서 사용하는 것이라 정리가 아직 덜 되서 필요없는 parameter나 변수들이

있는 점은 양해 부탁드립니다)

이렇게 코드를 작성하고 프로그램 실행시 memory corruption이 일어났다고 에러가 발생합니다..

참고로 디렉토리 검사하는 경우의수가 한가지인 경우는 잘 되지만 2개 이상 확인할 경우에만 에러가 발생합니다

저는 한번만 하는 경우가 된다면 free만 잘 시켜주면 문제가 없어야 한다고 생각되서

뭐가 문제인지 잘 모르겠습니다...

혹시 원인이 뭔지 아시는분은 댓글 달아주시면 감사하겠습니다

김정균의 이미지

코드 등록시에는 <code> ~ </code> block을 이용하여 글을 등록해 주세요.

...!의 이미지

gcc 에 다음 플래그를 주고 컴파일해서 실행해보세요. 힌트를 줄 겁니다.

-g -Wall -fsanitize=address -fsanitize=leak -fsanitize=undefined -fno-omit-frame-pointer

아니면 -g 플래그만 주고 컴파일해서 valgrind를 사용해서 실행해보시던지요.

> 기존에 있던 코드를 활용해서 사용하는 것이라 정리가 아직 덜 되서 필요없는 parameter나 변수들이 있는 점은 양해 부탁드립니다.

양해 부탁하지 마시고 정리해보세요. 같은 문제가 발생하는 가장 작은, 컴파일해서 실행해 볼 수 있는 코드를 만들어서 올리세요. 문제를 찾는 기본적인 과정입니다. 그 정도는 스스로 하셔야지요.

> 참고로 디렉토리 검사하는 경우의수가 한가지인 경우는 잘 되지만 2개 이상 확인할 경우에만 에러가 발생합니다

무슨 말씀이신지? 무슨 경우의 수요? 글 잘 쓰는 능력이 생각하는 능력입니다.

댓글 달기

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