public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
@ 2020-07-29 20:50 Kannuswamy, Nanthakumar
  2020-07-29 21:18 ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Kannuswamy, Nanthakumar @ 2020-07-29 20:50 UTC (permalink / raw)
  To: 'gcc-help@gcc.gnu.org'; +Cc: Ramakrishnan, Murugan

Hello All,

We would like to use PRIxxx macros which is defined in <inttypes.h> (introduced in C99)  in our c++ code base which is running on centos5/gcc 4.1.2.  When we compiling the following program using gcc centos5/4.1.2 its compiled successfully

#include <inttypes.h>
#include <stdio.h>

Int main() {
Int val = 10;
printf("Value is %" PRId32, val);
return 0;
}

$> gcc PRImacros.c
Compiled and run successfully.


But at the same time, if we compile above program using centos5/g++ 4.1.2 comes as part of bundled gcc 4.1.2 package,  we got the following error

$> g++ PRImacros.cpp
PRImacros.cpp: In function 'int main()':
PRImacros.c:8: error: expected `)' before 'PRId32'

Since this PRIxxx is introduced in c99, we tried in the following way too, but got the error

$> g++ -std=c99 PRImacros.cpp
cc1plus: warning: command line option "-std=c99" is valid for C/ObjC but not for C++
PRImacros.c: In function 'int main()':
PRImacros.c:8: error: expected `)' before 'PRId32'

So our question is, Is it possible to use PRIxxx macros in c++ code running on centos5/g++ 4.1.2? If so, how to use it. Appreciate your help. Thanks in Advance.

Nantha


Internal Use Only



This communication is the property of E*TRADE Financial Corporation and its affiliates and does not constitute an offer to sell or the solicitation of an offer to buy any security. It is intended only for the person to whom it is addressed and may contain information that is privileged, confidential, or otherwise protected from disclosure. Distribution or copying of this communication, or the information contained herein, by anyone other than the intended recipient is prohibited. If you have received this communication in error, please immediately notify E*TRADE Financial Corporation at (800) 387-2331, and delete and destroy any copies hereof.


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

* Re: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
  2020-07-29 20:50 Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version Kannuswamy, Nanthakumar
@ 2020-07-29 21:18 ` Jonathan Wakely
  2020-07-29 23:38   ` Kannuswamy, Nanthakumar
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2020-07-29 21:18 UTC (permalink / raw)
  To: Kannuswamy, Nanthakumar; +Cc: gcc-help, Ramakrishnan, Murugan

On Wed, 29 Jul 2020 at 21:56, Kannuswamy, Nanthakumar via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Hello All,
>
> We would like to use PRIxxx macros which is defined in <inttypes.h> (introduced in C99)  in our c++ code base which is running on centos5/gcc 4.1.2.  When we compiling the following program using gcc centos5/4.1.2 its compiled successfully
>
> #include <inttypes.h>
> #include <stdio.h>
>
> Int main() {
> Int val = 10;
> printf("Value is %" PRId32, val);
> return 0;
> }
>
> $> gcc PRImacros.c
> Compiled and run successfully.
>
>
> But at the same time, if we compile above program using centos5/g++ 4.1.2 comes as part of bundled gcc 4.1.2 package,  we got the following error
>
> $> g++ PRImacros.cpp
> PRImacros.cpp: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> Since this PRIxxx is introduced in c99, we tried in the following way too, but got the error
>
> $> g++ -std=c99 PRImacros.cpp
> cc1plus: warning: command line option "-std=c99" is valid for C/ObjC but not for C++
> PRImacros.c: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> So our question is, Is it possible to use PRIxxx macros in c++ code running on centos5/g++ 4.1.2? If so, how to use it.

It should work if you use g++ -D__STDC_FORMAT_MACROS

The C99 standard said that the macros should not be defined for C++
unless that macro is defined, and the libc headers on CentOS 5 follow
that rule. Later C standards removed that rule, so that the macros
should be unconditionally defined for C++ programs.

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

* RE: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
  2020-07-29 21:18 ` Jonathan Wakely
@ 2020-07-29 23:38   ` Kannuswamy, Nanthakumar
  2021-04-01 14:47     ` Kannuswamy, Nanthakumar
  0 siblings, 1 reply; 6+ messages in thread
From: Kannuswamy, Nanthakumar @ 2020-07-29 23:38 UTC (permalink / raw)
  To: 'Jonathan Wakely'; +Cc: gcc-help, Ramakrishnan, Murugan

Thanks for your help.


Internal Use Only

-----Original Message-----
From: Jonathan Wakely <jwakely.gcc@gmail.com>
Sent: Wednesday, July 29, 2020 4:19 PM
To: Kannuswamy, Nanthakumar <nanthakumar.kannuswamy@etrade.com>
Cc: gcc-help@gcc.gnu.org; Ramakrishnan, Murugan <murugan.ramakrishnan@etrade.com>
Subject: Re: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.

On Wed, 29 Jul 2020 at 21:56, Kannuswamy, Nanthakumar via Gcc-help <gcc-help@gcc.gnu.org> wrote:
>
> Hello All,
>
> We would like to use PRIxxx macros which is defined in <inttypes.h>
> (introduced in C99)  in our c++ code base which is running on
> centos5/gcc 4.1.2.  When we compiling the following program using gcc
> centos5/4.1.2 its compiled successfully
>
> #include <inttypes.h>
> #include <stdio.h>
>
> Int main() {
> Int val = 10;
> printf("Value is %" PRId32, val);
> return 0;
> }
>
> $> gcc PRImacros.c
> Compiled and run successfully.
>
>
> But at the same time, if we compile above program using centos5/g++
> 4.1.2 comes as part of bundled gcc 4.1.2 package,  we got the
> following error
>
> $> g++ PRImacros.cpp
> PRImacros.cpp: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> Since this PRIxxx is introduced in c99, we tried in the following way
> too, but got the error
>
> $> g++ -std=c99 PRImacros.cpp
> cc1plus: warning: command line option "-std=c99" is valid for C/ObjC
> but not for C++
> PRImacros.c: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> So our question is, Is it possible to use PRIxxx macros in c++ code running on centos5/g++ 4.1.2? If so, how to use it.

It should work if you use g++ -D__STDC_FORMAT_MACROS

The C99 standard said that the macros should not be defined for C++ unless that macro is defined, and the libc headers on CentOS 5 follow that rule. Later C standards removed that rule, so that the macros should be unconditionally defined for C++ programs.


This communication is the property of E*TRADE Financial Corporation and its affiliates and does not constitute an offer to sell or the solicitation of an offer to buy any security. It is intended only for the person to whom it is addressed and may contain information that is privileged, confidential, or otherwise protected from disclosure. Distribution or copying of this communication, or the information contained herein, by anyone other than the intended recipient is prohibited. If you have received this communication in error, please immediately notify E*TRADE Financial Corporation at (800) 387-2331, and delete and destroy any copies hereof.


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

* RE: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
  2020-07-29 23:38   ` Kannuswamy, Nanthakumar
@ 2021-04-01 14:47     ` Kannuswamy, Nanthakumar
  2021-04-01 16:36       ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Kannuswamy, Nanthakumar @ 2021-04-01 14:47 UTC (permalink / raw)
  To: 'Jonathan Wakely'; +Cc: 'gcc-help@gcc.gnu.org'

Hello,
Hope doing great. I mimic the production code here and would like to know the reason why gcc 8.2 is not throwing any warnings. We provided -Wall and -Wextra. Please let me know the right flag to show the warning during compilation time. Thanks

#include <iostream>
#include <string>
using namespace std;

class Sample {
    string name ;
  public:
     void setData(string value)  { name = value; }
    string getName() const { return name; }                                        -- Returns a copy of the string
    const char* getName1() const { return name.c_str(); }              -- Returns a Char* to the actual instance variable
    const char * getDestination()    { return getName().c_str(); }.   – Issue is getName() returns a copy of the actual string which is local to this function. Once the function goes out of scope it return junk values. Why the compiler is not throwing any warnings.
};

int main () {
  Sample sample;
  sample.setData("ROUTEX_CI");
  cout << "String Return Name: " << sample.getName() << "\n";
  cout << "Const Char Return Name with Instance Variable: " << sample.getName1() << "\n";
  cout << "Const Char Return With Temp Variable Created: " << sample.getDestination() << "\n";
  return 0;
}


Different in output between CentOS7/gcc 8.2 compiler and CentOS5/gcc 4.2 compiler for the same code.

CentOS7/gcc 8.2 compiler:
String Return Name: ROUTEX_CI
Const Char Return Name with Instance Variable: ROUTEX_CI
Const Char Return With Temp Variable Created: 7���-\x7f

CentOS5/gcc 4.2 compiler:
String Return Name: ROUTEX_CI
Const Char Return Name with Instance Variable: ROUTEX_CI
Const Char Return With Temp Variable Created: ROUTEX_CI


-----Original Message-----
From: Kannuswamy, Nanthakumar
Sent: Wednesday, July 29, 2020 7:38 PM
To: 'Jonathan Wakely' <jwakely.gcc@gmail.com>
Cc: gcc-help@gcc.gnu.org; Ramakrishnan, Murugan <murugan.ramakrishnan@etrade.com>
Subject: RE: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.

Thanks for your help.


Internal Use Only

-----Original Message-----
From: Jonathan Wakely <jwakely.gcc@gmail.com>
Sent: Wednesday, July 29, 2020 4:19 PM
To: Kannuswamy, Nanthakumar <nanthakumar.kannuswamy@etrade.com>
Cc: gcc-help@gcc.gnu.org; Ramakrishnan, Murugan <murugan.ramakrishnan@etrade.com>
Subject: Re: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.

On Wed, 29 Jul 2020 at 21:56, Kannuswamy, Nanthakumar via Gcc-help <gcc-help@gcc.gnu.org> wrote:
>
> Hello All,
>
> We would like to use PRIxxx macros which is defined in <inttypes.h>
> (introduced in C99)  in our c++ code base which is running on
> centos5/gcc 4.1.2.  When we compiling the following program using gcc
> centos5/4.1.2 its compiled successfully
>
> #include <inttypes.h>
> #include <stdio.h>
>
> Int main() {
> Int val = 10;
> printf("Value is %" PRId32, val);
> return 0;
> }
>
> $> gcc PRImacros.c
> Compiled and run successfully.
>
>
> But at the same time, if we compile above program using centos5/g++
> 4.1.2 comes as part of bundled gcc 4.1.2 package,  we got the
> following error
>
> $> g++ PRImacros.cpp
> PRImacros.cpp: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> Since this PRIxxx is introduced in c99, we tried in the following way
> too, but got the error
>
> $> g++ -std=c99 PRImacros.cpp
> cc1plus: warning: command line option "-std=c99" is valid for C/ObjC
> but not for C++
> PRImacros.c: In function 'int main()':
> PRImacros.c:8: error: expected `)' before 'PRId32'
>
> So our question is, Is it possible to use PRIxxx macros in c++ code running on centos5/g++ 4.1.2? If so, how to use it.

It should work if you use g++ -D__STDC_FORMAT_MACROS

The C99 standard said that the macros should not be defined for C++ unless that macro is defined, and the libc headers on CentOS 5 follow that rule. Later C standards removed that rule, so that the macros should be unconditionally defined for C++ programs.


This communication is the property of E*TRADE Financial Holdings, LLC and its affiliates and does not constitute an offer to sell or the solicitation of an offer to buy any security. It is intended only for the person to whom it is addressed and may contain information that is privileged, confidential, or otherwise protected from disclosure. Distribution or copying of this communication, or the information contained herein, by anyone other than the intended recipient is prohibited. If you have received this communication in error, please immediately notify E*TRADE Financial Holdings, LLC at (800) 387-2331, and delete and destroy any copies hereof.


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

* Re: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
  2021-04-01 14:47     ` Kannuswamy, Nanthakumar
@ 2021-04-01 16:36       ` Jonathan Wakely
  2021-04-02 22:48         ` Jim Wilson
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2021-04-01 16:36 UTC (permalink / raw)
  To: Kannuswamy, Nanthakumar; +Cc: gcc-help

On Thu, 1 Apr 2021 at 15:47, Kannuswamy, Nanthakumar wrote:
>
> Hello,
> Hope doing great. I mimic the production code here and would like to know the reason why gcc 8.2 is not throwing any warnings.

This has nothing to do with inttypes.h, so it would have been helpful
to change the subject line for this new topic.

Your code has undefined behaviour. The compiler cannot detect all
forms of undefined behaviour at compile time.

> We provided -Wall and -Wextra. Please let me know the right flag to show the warning during compilation time.

There is no option that will give you a warning for this code at
compile time, sorry.

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

* Re: Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version.
  2021-04-01 16:36       ` Jonathan Wakely
@ 2021-04-02 22:48         ` Jim Wilson
  0 siblings, 0 replies; 6+ messages in thread
From: Jim Wilson @ 2021-04-02 22:48 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Kannuswamy, Nanthakumar, gcc-help

On Thu, Apr 1, 2021 at 9:36 AM Jonathan Wakely via Gcc-help <
gcc-help@gcc.gnu.org> wrote:

> On Thu, 1 Apr 2021 at 15:47, Kannuswamy, Nanthakumar wrote:
> > Hope doing great. I mimic the production code here and would like to
> know the reason why gcc 8.2 is not throwing any warnings.
>
> Your code has undefined behaviour. The compiler cannot detect all
> forms of undefined behaviour at compile time.
>

This kind of problem can be detected at runtime though by using the address
sanitizer.
https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterReturn

rohan:2480$ g++ -fsanitize=address tmp.cc
rohan:2481$ ASAN_OPTIONS="detect_stack_use_after_return=1" ./a.out
String Return Name: ROUTEX_CI
Const Char Return Name with Instance Variable: ROUTEX_CI
=================================================================
==31164==ERROR: AddressSanitizer: stack-use-after-return on address
0x7fc3a5b00030 at pc 0x7fc3aa36366e bp 0x7ffff53d2700 sp 0x7ffff53d1ea8
READ of size 10 at 0x7fc3a5b00030 thread T0
...

Jim

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

end of thread, other threads:[~2021-04-02 22:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-29 20:50 Need suggestion for using intypes.h features in centos5 gcc 4.1.2 compiler version Kannuswamy, Nanthakumar
2020-07-29 21:18 ` Jonathan Wakely
2020-07-29 23:38   ` Kannuswamy, Nanthakumar
2021-04-01 14:47     ` Kannuswamy, Nanthakumar
2021-04-01 16:36       ` Jonathan Wakely
2021-04-02 22:48         ` Jim Wilson

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).