포인터를 반환하는 함수에서 구조체 선언은 어떤 방법으로 하나

superkkt의 이미지

예를들어 getpwent 함수는 struct passwd 구조체 포인터를 반환을 합니다. 그렇다면 getpwent 함수 내부에서는 passwd 구조체를 선언을 해야하는데..

{
struct passwd temp;
...
return(&temp);
}

이렇게 되는건가요? 아니면..

{
struct passwd *temp = (struct passwd *)malloc(sizeof(struct passwd));
...
return(temp);
}

이렇게 되는건가요?

전자의 경우에는 구조체도 지역변수일테니까 리턴받은 값을 참조하는것 자체가 잘못된 방법일듯 싶고.. 후자의 경우에는 메모리 반환은 어떻게 되는건지....

체스맨의 이미지

첫번째는 아시다시피 당연히 안되구요.
두번째처럼 되어있다면 free 해줘야겠죠.
아니면 상황에 따라 static 변수나 전역 변수를 리턴하는
경우도 있구요.

Orion Project : http://orionids.org

cronex의 이미지

superkkt wrote:
예를들어 getpwent 함수는 struct passwd 구조체 포인터를 반환을 합니다. 그렇다면 getpwent 함수 내부에서는 passwd 구조체를 선언을 해야하는데..

{
struct passwd temp;
...
return(&temp);
}

이렇게 되는건가요? 아니면..

{
struct passwd *temp = (struct passwd *)malloc(sizeof(struct passwd));
...
return(temp);
}

이렇게 되는건가요?

전자의 경우에는 구조체도 지역변수일테니까 리턴받은 값을 참조하는것 자체가 잘못된 방법일듯 싶고.. 후자의 경우에는 메모리 반환은 어떻게 되는건지....


조금 주제를 벗어난 방법일지 모르겠지만....
XXX_XXX(struct passwd *temp){
     ...
     temp.xxx = ...
     ...
}

main () {
    struct passwd temp;
    XXX_XXX( &temp);
}

의 방법도 있습니다.
배열이 넘어와야 하는 거라면 말씀하신 방법중 2번으로 해야 하지만 단지 하나의 포인터만 넘겨주기만 하면 된다면 값을 넘겨받을 포인터를 호출하는 쪽에서 넘겨줘도 됩니다. (이런 경우 구조체의 boundary를 넘어서지 않도록 조심해야 합니다. side effect가 생기면 골치아프거든요.)
이때는 return은 값을 받기 보다는 error코드등을 리턴하는 것이 좋겠네요.

------------------------------------------------------------
이 멍청이~! 나한테 이길 수 있다고 생각했었냐~?
광란의 귀공자 데코스 와이즈멜 님이라구~!

purluno의 이미지

체스맨님의 말씀대로 static 공간을 사용할것입니다. 이것이 멀티스레드 프로그램에서 안전하지 않기때문에 cronex님께서 설명하신 형태대로 getpwent_r이라는 함수가 있습니다.

댓글 달기

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