[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cannot pass objects of non-POD type
From: |
Rolf Magnus |
Subject: |
Re: cannot pass objects of non-POD type |
Date: |
Wed, 13 Oct 2004 18:17:13 +0200 |
Vijay wrote:
> Hi all,
> Im using
> gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)
> on 64bit linux server
>
> im trying to compile following code
> --------------------sam.cpp---------------------
> #include <string>
> #include <iostream>
> #include <stdarg.h>
> using namespace std;
>
> void Write( const char* msg, const char* msg2, ...)
> {
> cout <<msg <<" "<<msg2<<endl;
> }
>
> int main()
> {
> string str("World");
> Write("Hello","Debug out %s" ,str);
> return 0;
> }
> -------------------------------------------
>
> When i compile this code i get following compilation warning .
>
> [oracle@sahyagiri test]$ g++ sam.cpp
> sam.cpp: In function `int main()':
> sam.cpp:17: warning: cannot pass objects of non-POD
> type `struct std::string'
> through `...'; call will abort at runtime
That just tells it. You cannot pass non-POD types through '...' in C++.
std::string is a non-POD type, so you can't pass it.
> When i run the executable, a.out it fails with Illegal
> instruction eror
>
> [oracle@sahyagiri test]$ ./a.out
> Illegal instruction
> [oracle@sahyagiri test]$
The compiler did warn you about that, didn't it?
> Did any one face this problem, if yes is there any one
> workaround to this problem.
Don't pass non-PODs through variable argument lists. Or more general, don't
use variable argument lists at all. They already were dangerous in C, but
in C++, they are also pretty much useless.
> i guess this is a issue with compiler gcc 3.2.3
No, it isn't.
> because i tries same this with gcc 2.95, though it
> gives warning while compilation, but executable runs
> with out any runtime error.
That was just bad luck than.