cannot pass objects of non-POD type

V

Vijay

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

When i run the executable, a.out it fails with Illegal
instruction eror

[oracle@sahyagiri test]$ ./a.out
Illegal instruction
[oracle@sahyagiri test]$

Did any one face this problem, if yes is there any one
workaround to this problem.

i guess this is a issue with compiler gcc 3.2.3

because i tries same this with gcc 2.95, though it
gives warning while compilation, but executable runs
with out any runtime error.

Thanks and Regards
Vijay
 
J

John Harrison

Vijay said:
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);

No idea what you are trying to do, why pass str to Write when you don't use
str inside Write? Anyway try this

Write("Hello", "Debug out %s", str.c_str());

C strings and C++ strings are not the same.

john
 
R

Ron Natalie

Vijay said:
[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

The compiler is giving you a big hint here. You can not
pass arguments of non-POD type to VARARG'd functions.
std::string is a non-POD type.

Varargs has no clue about how to deal with non-POD types
(mostly because it doesn't know about the special
construction/copy/destruction sematics inherit in passing
them to subroutines).

I always theorized that an operator... function should
be invoked to convert to something that ... could handle
for classes. This would be in line with out ... args work
in general (there is a conversion, like integer promotion
etc... that makes things work in general)>
 
R

Rolf Magnus

Vijay said:
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.
 
M

Michael Kurz

Rolf Magnus said:
Vijay wrote:

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 would not say so, even without bothering with printf(...) and friends,
look at
the Loki::Conversion<T1, T2> implementation of Adrei Alexandrescu, where he
shows how nice you can use

SomeType1 Test(...) as a kind of catch all type function. (only at
compiletime of course)


Regards
Michael
 
J

Jonathan Turkanis

Michael Kurz said:
I would not say so, even without bothering with printf(...) and friends,
look at
the Loki::Conversion<T1, T2> implementation of Adrei Alexandrescu, where he
shows how nice you can use

SomeType1 Test(...) as a kind of catch all type function. (only at
compiletime of course)

True, this trick is used all over Boost Type Traits (and the rest of Boost) and
I think Andrei is largely responsible. But these functions are never actually
invoked.

Furthermore, IIRC, Andrei's original implementation was actually non-conforming
because it passed user-defined types through ellipses. In order to get a
completely conforming implementation you have to jump through a few more hoops.

See http://www.boost.org/boost/type_traits/is_convertible.hpp.

Jonathan
 
R

Ron Natalie

Jonathan said:
Furthermore, IIRC, Andrei's original implementation was actually non-conforming
because it passed user-defined types through ellipses. In order to get a
completely conforming implementation you have to jump through a few more hoops.
User defined types are fine.
It's user defined non-POD types that are not.

The problem is that the variable arg mechanism was never updated
to deal with C++. It's restricted to behavior from C (with the
goofy exception of allowing all the args to a function to be variable
but there's no defined way of extracting such).
 
J

Jonathan Turkanis

Ron Natalie said:
User defined types are fine.
It's user defined non-POD types that are not.

I should have said 'arbitrary user-defined types.' The type traits templates in
question are meant to be able to handle almost any arguments. (Unfortunately,
they still can't. I believe

is_convertible< noncopyable, noncopyable >::value

still causes a compiler error.)
The problem is that the variable arg mechanism was never updated
to deal with C++. It's restricted to behavior from C (with the
goofy exception of allowing all the args to a function to be variable
but there's no defined way of extracting such).

I understand.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top