invalid lvalue in assignment 가 나오는데 어떻게 고치면 될까요?

narusas의 이미지

오픈 소스를 컴파일하려고 했는데

*((unsigned char *)cursor))++ = cbyte;

*((unsigned int *)cursor)++ = mask;

이런 코드가 포함되어 있는 부분에서 컴파일 에러가 납니다.
컴파일 에러는
invalid lvalue in assignment
invalid lvalue in increment

이 두개인데요.

개인적으로 해석하기로는 왼쪽에 있는 놈에게 할당이나 증가를 시킬수 없다.. 라는 의미로 보입니다.

그런데 다른 메일링 리스트를 보니 이런 문제를 겪은 사람이 없어 보이는 것으로 보아 잘들 컴파일 해서 쓰는거 같은데요...

테스트해 본 gcc버전은
Suse 9.2에 포함된 gcc 3.3.4 와 fedora core 5에 포함된 gcc4.1.0 두개를 테스트해 봤습니다.
이걸 어떻게 해야 컴파일 시킬수 있을까요?

lovewar의 이미지

*((unsigned char *) cursor)++ = cbyte;
                                       /* ) 하나가 더 있음 */
*((unsigned int *) cursor)++ = mask;

오타가 아니라면, 컴파일러 버그로 보입니다
(여기서 cursor는 수정가능한 lvalue여야 합니다).

동일한 코드가 많지 않다면, 다음과 같이 수정해 보세요.

*((unsigned char *) cursor) = cbyte;
 ((unsigned char *) cursor)++;
*((unsigned int *) cursor) = mask;
 ((unsigned int *) cursor)++;

이것도 안된다면, 풀어써야 할것 같습니다.

가져온 open 소스의 게시판을 확인해 부는 것도 좋겠습니다.

doldori의 이미지

컴파일러의 버그가 아니라 코드가 잘못된 것입니다.
후위형 ++ 연산의 결과는 lvalue가 아니므로 대입을 하는 것은 잘못이죠.
코드의 의도도 이해하기 힘들군요. 이런 것일까요?

*((unsigned char *)cursor)) = cbyte + 1;
narusas의 이미지

Rehat 6.x rehdat 7.2까지는 No problems, should work straight out of the box.하다고 하는데요..

예전 gcc에서는 제대로 컴파일 된거 같은데요.. 이걸 요즘 gcc에서 컴파일하게 변경하려면 어떻게 해야 하는지 모르겠네요.

원본 코드는 다음과 같습니다.

inline static void *inline_memset(void *address, unsigned int cbyte, unsigned int size) {
 
  unsigned int mask = cbyte & 0x000000ff;
  unsigned int prebytecopies   = min(4 - ((unsigned int)address % 4), size) % 4;
  unsigned int postbytecopies  = (size - prebytecopies) % 4;
  unsigned int wordcopies = max((int)size - (int)prebytecopies - (int)postbytecopies, 0) >> 2;
  unsigned int duffs = (wordcopies + 63) / 64;
  void *cursor = address;
 
  cbyte &= 0x000000ff;
 
  /*
  ** Prepare the mask
  */
 
  mask <<= 8;
  mask |= cbyte;
  mask <<= 8;
  mask |= cbyte;
  mask <<= 8;
  mask |= cbyte;
 
  /*
  ** Do byte copying (to get aligned)
  */
 
  for (; prebytecopies; prebytecopies--) {
    *((unsigned char *)cursor)++ = cbyte;
  }
/*
  ** Do word copying
  */
 
  if(wordcopies) { 
    switch (wordcopies & 0x3f) {
      default:
      case  0: do { *((unsigned int *)cursor)++ = mask;
      case 63:      *((unsigned int *)cursor)++ = mask;
      case 62:      *((unsigned int *)cursor)++ = mask;
중간색략
      case  1:      *((unsigned int *)cursor)++ = mask;
               } while (--duffs > 0);
    }
  }
 
  //for (; wordcopies; wordcopies--) {
  //  *((unsigned int *)cursor)++ = mask;
  //}
 
  /*
  ** Do byte copying
  */
 
  for (; postbytecopies; postbytecopies--) {
    *((unsigned char *)cursor)++ = cbyte;
  }
 
  return address;
 
}
IDNed의 이미지

소스가 잘못된 거 같습니다.
cursor 포인터 자체는 분명 lvalue입니다.
그러나 cursor 포인터를 캐스팅하면 캐스팅된 결과는 rvalue가 됩니다.
즉 ((unsigned char *)cursor)은 rvalue입니다.

((unsigned char *)cursor)=some_ptr;

이 코드가 될지 안될지를 따져보세요.
thyoo의 이미지

캐스트는 lvalue를 만들지 않는다.
(단, C++에서 캐스트의 반환 값 type이 Ref인 경우에는 lvalue)

string copy하는 루틴은

void str_copy(char* dest, const char* src)
{
	while (*src) *dest++ = *src++;
	*dest = 0;
}

식으로들 곧잘 구현합니다.

위의 while문은 캐스트가 없으므로 전혀 문제가 안됩니다만,
*((unsigned char *)cursor))++ 는
cursor를 unsigned char*로 캐스트하고 ++를 하려고 하기 때문에
표준 위반입니다.

http://gcc.gnu.org/gcc-4.0/changes.html를 보니 4.0부터 표준대로 한다는군요.

The cast-as-lvalue, conditional-expression-as-lvalue and compound-expression-as-lvalue extensions, which were deprecated in 3.3.4 and 3.4, have been removed.

___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

doldori의 이미지

Quote:
*((unsigned char *)cursor))++ 는
cursor를 unsigned char*로 캐스트하고 ++를 하려고 하기 때문에
표준 위반입니다.

이 자체로는 위반이 아닙니다. 캐스팅한 결과는 lvalue가 아니지만 그것을 역참조한 값은 lvalue입니다.
(unsigned char *)cursor는 lvalue가 아니지만
*((unsigned char *)cursor))는 lvalue입니다.
따라서 *((unsigned char *)cursor))++ 역시 적법합니다.
원 코드에서 잘못된 점은 후위형 ++을 한 결과가 lvalue가 아니기 때문에
*((unsigned char *)cursor))++ = some_value
처럼 대입을 할 수 없다는 것입니다.
thyoo의 이미지

연산자 우선순위가 post increment가 앞설 텐데요.
http://www.swansontec.com/sopc.htm

*보다 ++가 먼저 겠지요...
___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

doldori의 이미지

Quote:
연산자 우선순위가 post increment가 앞설 텐데요.
http://www.swansontec.com/sopc.htm

*보다 ++가 먼저 겠지요...

그렇군요. 제가 착각했습니다.

narusas의 이미지

gcc 2.9.2에서 정상적으로 컴파일 되네요. -_-
음. 추측컨에 gcc 3.0으로 들어 오면서 무언가 처리가 바뀐것 같군요.

그럼 저걸 gcc 3.0이상에서 컴파일 시킬려면 어떻게 바꿔주면 될까요?

narusas의 이미지

switch (wordcopies & 0x3f) {
      default:
      case 0: do { *((unsigned int *)cursor)++ = mask;
      case 63: *((unsigned int *)cursor)++ = mask;
      case 62: *((unsigned int *)cursor)++ = mask;
중간색략
      case 1: *((unsigned int *)cursor)++ = mask;
               } while (--duffs > 0);
    }

이런식으로 do while 이 case 문의 scope를 넘어서도 되는 겁니까?

lovewar의 이미지

thyoo wrote:

*((unsigned char *)cursor))++ 는
cursor를 unsigned char*로 캐스트하고 ++를 하려고 하기 때문에
표준 위반입니다.

http://gcc.gnu.org/gcc-4.0/changes.html를 보니 4.0부터 표준대로 한다는군요.

The cast-as-lvalue, conditional-expression-as-lvalue and compound-expression-as-lvalue extensions, which were deprecated in 3.3.4 and 3.4, have been removed.

invalid lvalue in increment 이 에러는 cast-as-lvalue extension때문일것이고요
invalid lvalue in assignment 이것은 compound-expression-as-lvalue extension 때문일까요?
이 부분에서 좀더 설명을 해 주실수 있으신가요?

narusas wrote:

이런식으로 do while 이 case 문의 scope를 넘어서도 되는 겁니까?

case 문도 하나의 문장이기 때문에 사용해도 괜찮습니다. 하지만,
바람직하지는 않습니다.

thyoo의 이미지

disable된 lvalue관련 gcc extension입니다.
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC69

___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)

댓글 달기

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