[질문] 전역변수 사용시 주의점이 있나요?

bizzare의 이미지

A.h

class ClassA {

public:
std::list<int*> m_lstID;
...
}

A.cpp
ClassA ex_A;
...
class A 관련 코드들

B.cpp
extern ClassA ex_A;
...
B관련 코드들

C.cpp
extern ClassA ex_A;
...
C관련 코드들

위와 같이 코드를 작성했습니다.
즉, A라는 클래스가 정의된 A.cpp에 A클래스타입변수를 전역변수로 선언하고, B와 C에서 그 전역변수를 사용하는 코드죠.
그리고 A클래스에는 list타입 변수가 있고요.

VC++에선 잘 돌아가더군요.
그런데 Linux, GCC로 컴파일해서 쓸 때는 오류가 생깁니다.
다른 변수들은 괜찮은데, A클래스의 list타입 변수에 접근하면 오류가 생기더군요.

이상한 것은 VC++에선 A클래스의 생성자에 break point를 찍어 놓으면 debug모드에서 해당 point에 들어오는데, 리눅스에서는 안들어오더군요 -_-;; (이게 제일 이상하더군요.)

제 생각에 list타입변수가 뭔가 제대로 초기화가 안되어 있는 듯 합니다. size를 가져온다던지 하려고 하면 무조건 런타임에러가 나거든요.

혹시 리눅스 아니면 GCC에서 전역변수를 사용할 때 주의해야할 점 같은 것이 있는지요?
저에게 희망을 주세요 T_T

buffmail의 이미지

- B.cpp 와 C.cpp 에 A.h 인클루드 하셨나요?

- B.cpp 와 C.cpp 에 std::list 가 인클루드 되어 있나요?

이게 되었다면, 다른 하자는 없어 보이는데..

에러 메시지는 어떻게 나오나요?

bizzare의 이미지

컴파일 에러가 아니라 런타임 에러가 납니다.

정말 깨끗하고 멋진 프로그램을 짜고 싶습니다.

doldori의 이미지

지금 떠오르는 것은 전역변수의 생성 순서 문제입니다만,
소스를 보기 전에는 알 수가 없군요.

kihongss의 이미지

그러고 보니
클래스의 object가 전역일경우
생성타임이 궁금하군요.
처음 참조할때 생기는 건가요?

댓글 달기

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