c/c++ 에서 구조체 선언에 관해 질문드려요~~~

pogusm의 이미지

libosip2 라이브러리 소스를 보고 있는데요...
잘 모르겠어서 질문드립니다.
(소스 : http://ftp.gnu.org/gnu/osip/libosip2-4.0.0.tar.gz )

# vim include/osip2/osip_mt.h
 
struct osip_thread;
 
struct osip_thread *osip_thread_create (int stacksize, void *(*func) (void *), void *arg);

# vim src/osip2/port_thread.c
 
struct osip_thread *osip_thread_create (int stacksize, void *(*func) (void *), void *arg)
{
  int i;
  osip_thread_t *thread = (osip_thread_t *) osip_malloc (sizeof (osip_thread_t));
 
  if (thread == NULL)
    return NULL;
 
  i = pthread_create (thread, NULL, func, (void *) arg);
  if (i != 0) {
    OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Error while creating a new thread\n"));
    osip_free (thread);
    return NULL;
  }
  return (struct osip_thread *) thread;
}

위처럼 되어 있는데...

struct osip_thread; 라는건 구조체 아닌가요?
근데 왜 달랑 이름만 있는건가요?

감사합니다.

klara의 이미지

전방 선언(forward declaration)이라고 합니다. 구조체 멤버에 접근할 필요가 없을 때에는 전방 선언으로 이것이 구조체의 이름이라는 것만 알려주면 컴파일이가능합니다.
실제로 멤버에 접근해야 할 때에는 구조체의 정의가 필요하므로 별도로 정의가 포함된 해더파일을 포함시켜야 합니다.

pogusm의 이미지

그런데,
실제 정의가 되어 있는 소스코드도 없는거 같은데...
제가 잘 못찾는거 같은데..
시간 되시면 구조체 확인 좀 해주시면 감사하겠습니다. 꾸벅~

klara의 이미지

osip2/internal.h 을 확인해보세요.
osip_thread라는건 더미 이름이고, 실제 할당하고 있는건 osip_thread_t입니다.
동적할당후 형 변환은 포인터간의 형변환이기 때문에 마찬가지로 전방 선언만 있으면 가능합니다.
osip_thread_t는 각 플랫폼에 따라서 적절한 thread 구조체로 typedef되어 있습니다.
내부에서 쓸때에는 다시 osip_thread_t로 형변환하고 사용합니다.
단 라이브러리의 사용자입자에서는 모든 API가 osip_thread 에 대한 포인터만 주고 받으면 되니까 실제 정의가 없어도 사용하는데는 문제가 없습니다.

pogusm의 이미지

- 동적할당후 형 변환은 포인터간의 형변환이기 때문에 마찬가지로 전방 선언만 있으면 가능합니다.
- 내부에서 쓸때에는 다시 osip_thread_t로 형변환하고 사용합니다.
- 모든 API가 osip_thread 에 대한 포인터만 주고 받으면 되니까 실제 정의가 없어도 사용하는데는 문제가 없습니다.

처음 알았습니다. ㅎㅎ

궁금하던게 해결되었습니다~

감사합니다. 꾸벅~

댓글 달기

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