public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/35835]  New: Compiler fails to recognize match of local "extern" declarations
@ 2008-04-05 18:33 oder at eleks dot lviv dot ua
  2008-04-05 19:56 ` [Bug c++/35835] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: oder at eleks dot lviv dot ua @ 2008-04-05 18:33 UTC (permalink / raw)
  To: gcc-bugs

Consider following program
======= begin of test.cpp ========
#include <stdio.h>

static inline void SetValue(int i)
{
  extern int g_iValue;

  g_iValue = i;
}

static inline int GetValue()
{
  extern int g_iValue;

  return g_iValue;
}



int g_iValue = 0;


int main()
{
  int iValueSave = GetValue();

  SetValue(1);
  printf("%d\n", GetValue());

  SetValue(iValueSave);

  return 0;
}
======= end of test.cpp ========
I use this apptoach to only publish get/set global variable accessors in header
while keeping variable itself hidden in cpp.

If compiled with optimizations turned on, compiler fails identify two local
"extern" declarations as a single memory object and uses initial value
retrieved for iValueSafe in call to printf as well (as if SetValue() was
unrelated to g_iValue).

If compiled and run, program outputs 0 instead of 1.
osx-leopard:build oder$ g++ -O3 -o test test.cpp
osx-leopard:build oder$ ./test
0

Output of "g++ -v" is:
Using built-in specs.
Target: powerpc-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5478~1/src/configure --disable-checking
-enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
--with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib
--build=powerpc-apple-darwin9 --host=powerpc-apple-darwin9
--target=powerpc-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5478)


-- 
           Summary: Compiler fails to recognize match of local "extern"
                    declarations
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: oder at eleks dot lviv dot ua
 GCC build triplet: ppc
  GCC host triplet: ppc
GCC target triplet: ppc


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35835


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-20  8:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-35835-4@http.gcc.gnu.org/bugzilla/>
2021-12-17 11:48 ` [Bug c++/35835] Compiler fails to recognize match of local "extern" declarations pinskia at gcc dot gnu.org
2021-12-20  8:11 ` egallager at gcc dot gnu.org
2008-04-05 18:33 [Bug c++/35835] New: " oder at eleks dot lviv dot ua
2008-04-05 19:56 ` [Bug c++/35835] " pinskia at gcc dot gnu dot org
2008-04-05 20:02 ` oder at eleks dot lviv dot ua
2008-04-05 21:16 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).