황당한 c 면접문제..

qprk의 이미지

알고있는 후배가 면접을 보러가서 c 문제를 받았다고 합니다.

아래 코드에서 뭐가 잘못되었는지를 말하라는 문제였는데요..

그 코드는 아래와 같습니다.

-------------------------------

int& sum(int a, int b)
{
int result;

result = a+b;
return result;
}

main()
{
int& result = sum(3, 5);
printf("%d\n", result);
}
---------------------------------

문제의 정답은 sum() 에서 result 가 스택영역에 있어 함수가 리턴되면서 해당 부분이 스택에서 지워지기 때문에 main 에서는 그 값을 참조할 수 없다. 가 정답이라고 하내요..

그런데 문제는 'int& sum(int a, int b)' 가 문법적으로 맞는것인가요?

저렇게해도 컴파일이 되었다고 하는데요 신기해서 여러 플렛폼에서 컴파일 해보면

linux gcc 4.x 에서는 컴파일 에러가 발생하고

windows vs6.0 에서는 컴파일이 됩니다.

제 생각에는 & 가 아니고 * 여야 문제가 성립할꺼 같은데요..

저게 문법적으로 문제가 없나요?

klyx의 이미지

C가 아니라 C++코드입니다... 하지만 C++컴파일러에선 반대로 main함수가 선언이 저래서 컴파일안될수도 있겠네요.
설마 http://kldp.org/node/96036 랑 같은 곳인가-_-;

chadr의 이미지

데이터형 다음에 &를 붙이는 것은 c++의 참조자를 선언하는 문법입니다.
따라서 c++이라면 전혀 문법적 문제가 없는데..

면접관이 저걸 c코드라고 내밀었다면 면접관의 자질이 의심이 되네요..
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

snowall의 이미지

그걸 노린 걸 수도 있죠
"이것은 C코드인데, 문제가 있어. 뭐가 문제일까?"
"C코드인게 문제입니다. C++이면 문제가 없습니다"
이런거 아닐까요?

--------------------------
snowall의 블로그입니다.
http://snowall.tistory.com

피할 수 있을때 즐겨라! http://melotopia.net/b

hyunya77의 이미지

포인터를 쓴다고 해도 반환되는값이 그냥 정수형이니 문제가 있겠죠.
그냥 정수형을 반환한다고 하면 로컬변수 값을 반환해도 문제가 없지 않나요?
인수에 넣어서 받아간다면 스택에서 지워져버리니 문제가 있겠지만요.
예를 들어 아래와 같은 코드는 문제가 없습니다.
방금 Visual Studio 2005에서 확인해보니 괜찮네요.
일반 MFC 프로젝트에 살짝 넣어 테스트 했으니 컴파일러는 C++이 사용됐을것같구요.

int sum(int a, int b)
{
int result;

result = a+b;
return result;
}

main()
{
int result = sum(3, 5);
printf("%d\n", result);
}

댓글 달기

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