Discussion:
[Bug-apl] cast from pointer to smaller type 'int'
Xiao-Yong Jin
2017-09-11 16:55:28 UTC
Permalink
At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Juergen Sauermann
2017-09-11 18:08:51 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi </font>Xiao-Yong,<br>
<br>
thanks, maybe fixed in SVN 1011.<br>
Problem with that error is that the casted type is not a pointer, at
least on my machine.<br>
<br>
/// Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 09/11/2017 06:55 PM, Xiao-Yong Jin
wrote:<br>
</div>
<blockquote
cite="mid:EBECA868-7F85-4BAA-894D-***@gmail.com"
type="cite">
<pre wrap="">At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out &lt;&lt; "thread #" &lt;&lt; setw(2) &lt;&lt; N &lt;&lt; ":" &lt;&lt; setw(16) &lt;&lt; int(thread)



</pre>
</blockquote>
<br>
</body>
</html>
Xiao-Yong Jin
2017-09-11 18:38:15 UTC
Permalink
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Juergen Sauermann
2017-09-11 20:01:31 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi </font>Xiao-Yong,<br>
<br>
I see. In this particular case the <b>pthread_t</b> is only used to
identify a thread and<br>
to distinguish it from other threads for debugging purposes. So as
long as the<br>
compiler does not complain about the cast everything is fine. Cast
to <b>void *</b> instead of<br>
<b>int</b> would also be an option.<br>
<br>
/// Jürgen<br>
 <br>
<br>
<div class="moz-cite-prefix">On 09/11/2017 08:38 PM, Xiao-Yong Jin
wrote:<br>
</div>
<blockquote
cite="mid:166E75CF-133C-402A-B34F-***@gmail.com"
type="cite"> <pre wrap="">I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast&lt;int&gt; is alright, but may not be useful in the future. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 1:08 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a> wrote:

Hi Xiao-Yong,

thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.

/// Jürgen


On 09/11/2017 06:55 PM, Xiao-Yong Jin wrote:
</pre>
<blockquote type="cite">
<pre wrap="">At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out &lt;&lt; "thread #" &lt;&lt; setw(2) &lt;&lt; N &lt;&lt; ":" &lt;&lt; setw(16) &lt;&lt; int(thread)




</pre>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">

</pre>
</blockquote>
<br>
</body>
</html>
Xiao-Yong Jin
2017-09-19 21:44:18 UTC
Permalink
Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.

Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
<< static_cast<int>(thread)
^~~~~~~~~~~~~~~~~~~~~~~~

I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.
Hi Xiao-Yong,
I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.
/// Jürgen
Post by Xiao-Yong Jin
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Juergen Sauermann
2017-09-20 20:00:42 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi Xiao-Yong,<br>
<br>
with</font><b><font face="Courier New, Courier, monospace">
reinterpret_cast&lt;size_t&gt; </font></b>
I am getting (<b>gcc 4.8</b>)  this:<br>
<br>
<b><font face="Courier New, Courier, monospace">Thread_context.cc:
In member function ‘void
Thread_context::print(std::ostream&amp;) const’:<br>
Thread_context.cc:73:42: error: invalid cast from type
‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka
unsigned int}’<br>
        &lt;&lt; reinterpret_cast&lt;size_t&gt;(thread)<br>
                                          ^<br>
make[1]: *** [apl-Thread_context.o] Error 1<br>
<br>
</font></b>It seems a little ridiculous to me that replacing a
good old C-style cast that worked fine for the<br>
last 10 years cannot be replaced by one of the 3 members of the C++
zoo of casts in a portable way?<br>
<br>
Maybe some intermediate cast to <b>const void *</b> can be done on
your machine?<br>
<br>
Best Regards,<br>
Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 09/19/2017 11:44 PM, Xiao-Yong Jin
wrote:<br>
</div>
<blockquote
cite="mid:A2E1D4EA-3BC7-4031-B103-***@gmail.com"
type="cite">
<pre wrap="">Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.

Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
&lt;&lt; static_cast&lt;int&gt;(thread)
^~~~~~~~~~~~~~~~~~~~~~~~

I need reinterpret_cast&lt;size_t&gt; here. I cannot reinterpret_cast&lt;int&gt; either, because of the difference in size. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 3:01 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a> wrote:

Hi Xiao-Yong,

I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.

/// Jürgen


On 09/11/2017 08:38 PM, Xiao-Yong Jin wrote: </pre> <blockquote type="cite"> <pre wrap="">I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast&lt;int&gt; is alright, but may not be useful in the future. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 1:08 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a>
wrote:

Hi Xiao-Yong,

thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.

/// Jürgen


On 09/11/2017 06:55 PM, Xiao-Yong Jin wrote:

</pre>
<blockquote type="cite">
<pre wrap="">At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out &lt;&lt; "thread #" &lt;&lt; setw(2) &lt;&lt; N &lt;&lt; ":" &lt;&lt; setw(16) &lt;&lt; int(thread)





</pre>
</blockquote>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">

</pre>
</blockquote>
<br>
</body>
</html>
Juergen Sauermann
2017-09-20 20:26:14 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi Xiao-Yong,<br>
<br>
I can compile this:<br>
<br>
<font face="Courier New, Courier, monospace"><b>       &lt;&lt;
reinterpret_cast&lt;uint64_t&gt;(</b><b><br> </b><b>          reinterpret_cast&lt;void *&gt;(thread))</b><b><br>
</b></font><br>
Please let me know if it compiles on you box as well.<br>
<br>
Best Regards,<br>
Jürgen<br>
<br>
</font><br>
<div class="moz-cite-prefix">On 09/20/2017 10:00 PM, Juergen
Sauermann wrote:<br>
</div>
<blockquote
cite="mid:7acc7890-b893-bedd-287d-***@t-online.de"
type="cite">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<font face="Helvetica, Arial, sans-serif">Hi Xiao-Yong,<br>
<br>
with</font><b><font face="Courier New, Courier, monospace">
reinterpret_cast&lt;size_t&gt; </font></b> I am getting (<b>gcc
4.8</b>)  this:<br>
<br>
<b><font face="Courier New, Courier, monospace">Thread_context.cc:
In member function ‘void
Thread_context::print(std::ostream&amp;) const’:<br>
Thread_context.cc:73:42: error: invalid cast from type
‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka
unsigned int}’<br>
        &lt;&lt; reinterpret_cast&lt;size_t&gt;(thread)<br>
                                          ^<br>
make[1]: *** [apl-Thread_context.o] Error 1<br>
<br>
</font></b>It seems a little ridiculous to me that replacing a
good old C-style cast that worked fine for the<br>
last 10 years cannot be replaced by one of the 3 members of the
C++ zoo of casts in a portable way?<br>
<br>
Maybe some intermediate cast to <b>const void *</b> can be done
on your machine?<br>
<br>
Best Regards,<br>
Jürgen<br>
<br>
<br>
<div class="moz-cite-prefix">On 09/19/2017 11:44 PM, Xiao-Yong Jin
wrote:<br>
</div>
<blockquote
cite="mid:A2E1D4EA-3BC7-4031-B103-***@gmail.com"
type="cite">
<pre wrap="">Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.

Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
&lt;&lt; static_cast&lt;int&gt;(thread)
^~~~~~~~~~~~~~~~~~~~~~~~

I need reinterpret_cast&lt;size_t&gt; here. I cannot reinterpret_cast&lt;int&gt; either, because of the difference in size. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 3:01 PM, Juergen Sauermann <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a> wrote:

Hi Xiao-Yong,

I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.

/// Jürgen


On 09/11/2017 08:38 PM, Xiao-Yong Jin wrote: </pre> <blockquote type="cite"> <pre wrap="">I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast&lt;int&gt; is alright, but may not be useful in the future. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 1:08 PM, Juergen Sauermann <a moz-do-not-send="true" class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a>
wrote:

Hi Xiao-Yong,

thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.

/// Jürgen


On 09/11/2017 06:55 PM, Xiao-Yong Jin wrote:

</pre>
<blockquote type="cite">
<pre wrap="">At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out &lt;&lt; "thread #" &lt;&lt; setw(2) &lt;&lt; N &lt;&lt; ":" &lt;&lt; setw(16) &lt;&lt; int(thread)





</pre>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<br>
</blockquote>
<br>
</body>
</html>
Xiao-Yong Jin
2017-09-20 20:36:16 UTC
Permalink
reinterpret_cast works from smaller sizes to larger sizes.

So simply
reinterpret_cast<uint64_t *>(thread)

should work for both of our machines (size_t is uint64_t for me).

... until some pthread implementation decides to hand you a larger sized struct for pthread_t.
Hi Xiao-Yong,
<< reinterpret_cast<uint64_t>(
reinterpret_cast<void *>(thread))
Please let me know if it compiles on you box as well.
Best Regards,
Jürgen
Hi Xiao-Yong,
Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’
<< reinterpret_cast<size_t>(thread)
^
make[1]: *** [apl-Thread_context.o] Error 1
It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the
last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way?
Maybe some intermediate cast to const void * can be done on your machine?
Best Regards,
Jürgen
Post by Xiao-Yong Jin
Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.
Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
<< static_cast<int>(thread)
^~~~~~~~~~~~~~~~~~~~~~~~
I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.
Hi Xiao-Yong,
I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.
/// Jürgen
Post by Xiao-Yong Jin
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Xiao-Yong Jin
2017-09-20 20:37:27 UTC
Permalink
Post by Xiao-Yong Jin
reinterpret_cast works from smaller sizes to larger sizes.
So simply
reinterpret_cast<uint64_t *>(thread)
Sorry, I meant
reinterpret_cast<uint64_t>(thread)
Post by Xiao-Yong Jin
should work for both of our machines (size_t is uint64_t for me).
... until some pthread implementation decides to hand you a larger sized struct for pthread_t.
Hi Xiao-Yong,
<< reinterpret_cast<uint64_t>(
reinterpret_cast<void *>(thread))
Please let me know if it compiles on you box as well.
Best Regards,
Jürgen
Hi Xiao-Yong,
Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’
<< reinterpret_cast<size_t>(thread)
^
make[1]: *** [apl-Thread_context.o] Error 1
It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the
last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way?
Maybe some intermediate cast to const void * can be done on your machine?
Best Regards,
Jürgen
Post by Xiao-Yong Jin
Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.
Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
<< static_cast<int>(thread)
^~~~~~~~~~~~~~~~~~~~~~~~
I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.
Hi Xiao-Yong,
I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.
/// Jürgen
Post by Xiao-Yong Jin
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Juergen Sauermann
2017-09-21 11:44:08 UTC
Permalink
Hi,

except that it did not compile on my machine:

*Thread_context.cc:73:44: error: invalid cast from type ‘pthread_t {aka
long unsigned int}’ to type ‘uint64_t {aka long long unsigned int}’**
** << reinterpret_cast<uint64_t>(thread)**
** ^**
**make[1]: *** [apl-Thread_context.o] Error 1*

This is primarily because *pthread_t *is not a pointer on my box.

Best Regards,
/// Jürgen
Post by Xiao-Yong Jin
reinterpret_cast works from smaller sizes to larger sizes.
So simply
reinterpret_cast<uint64_t *>(thread)
should work for both of our machines (size_t is uint64_t for me).
... until some pthread implementation decides to hand you a larger sized struct for pthread_t.
Hi Xiao-Yong,
<< reinterpret_cast<uint64_t>(
reinterpret_cast<void *>(thread))
Please let me know if it compiles on you box as well.
Best Regards,
Jürgen
Hi Xiao-Yong,
Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’
<< reinterpret_cast<size_t>(thread)
^
make[1]: *** [apl-Thread_context.o] Error 1
It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the
last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way?
Maybe some intermediate cast to const void * can be done on your machine?
Best Regards,
Jürgen
Post by Xiao-Yong Jin
Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.
Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
<< static_cast<int>(thread)
^~~~~~~~~~~~~~~~~~~~~~~~
I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.
Hi Xiao-Yong,
I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.
/// Jürgen
Post by Xiao-Yong Jin
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Xiao-Yong Jin
2017-09-20 20:26:24 UTC
Permalink
So you have a 32bit linux? Things got more complicated when you want memory safety unfortunately.
POSIX basically tells us pthread_t can be anything, so if you really want to print the content, perhaps the following would work without the compiler complaining. (I think only char can be used for type punning without breaking the strict aliasing rule, but that may only be true in C?)

const unsigned char* a = reinterpret_cast<const unsigned char*>(&thread);
for (size_t i = 0; i < sizeof(thread); ++i)
out << hex << setw(2) << static_cast<unsigned int>(a[i]);
Hi Xiao-Yong,
Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’
<< reinterpret_cast<size_t>(thread)
^
make[1]: *** [apl-Thread_context.o] Error 1
It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the
last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way?
Maybe some intermediate cast to const void * can be done on your machine?
Best Regards,
Jürgen
Post by Xiao-Yong Jin
Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.
Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
<< static_cast<int>(thread)
^~~~~~~~~~~~~~~~~~~~~~~~
I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.
Hi Xiao-Yong,
I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.
/// Jürgen
Post by Xiao-Yong Jin
I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.
Hi Xiao-Yong,
thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.
/// Jürgen
Post by Xiao-Yong Jin
At revision 1010
Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
Juergen Sauermann
2017-09-20 20:42:08 UTC
Permalink
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<font face="Helvetica, Arial, sans-serif">Hi,<br>
<br>
I do NOT want to access the content of the pthread, but I need
some identification that allows me<br>
to distinguish the different threads that are running in parallel
(on different cores). So I dont care if pthread is<br>
a pointer or an int as long as I can cast it to something that
&lt;&lt; can print (preferably in hex). No need to even<br>
print the content of the pthread.<br>
<br>
Best Regards,<br>
Jürgen<br>
<br>
<br>
</font><br>
<div class="moz-cite-prefix">On 09/20/2017 10:26 PM, Xiao-Yong Jin
wrote:<br>
</div>
<blockquote
cite="mid:4E6D3254-DF03-4BA2-909B-***@gmail.com"
type="cite">
<pre wrap="">So you have a 32bit linux? Things got more complicated when you want memory safety unfortunately.
POSIX basically tells us pthread_t can be anything, so if you really want to print the content, perhaps the following would work without the compiler complaining. (I think only char can be used for type punning without breaking the strict aliasing rule, but that may only be true in C?)

const unsigned char* a = reinterpret_cast&lt;const unsigned char*&gt;(&amp;thread);
for (size_t i = 0; i &lt; sizeof(thread); ++i)
out &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; static_cast&lt;unsigned int&gt;(a[i]); </pre> <blockquote type="cite"> <pre wrap="">On Sep 20, 2017, at 3:00 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a> wrote:

Hi Xiao-Yong,

with reinterpret_cast&lt;size_t&gt; I am getting (gcc 4.8) this:

Thread_context.cc: In member function ‘void Thread_context::print(std::ostream&amp;) const’:
Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’
&lt;&lt; reinterpret_cast&lt;size_t&gt;(thread)
^
make[1]: *** [apl-Thread_context.o] Error 1

It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the
last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way?

Maybe some intermediate cast to const void * can be done on your machine?

Best Regards,
Jürgen


On 09/19/2017 11:44 PM, Xiao-Yong Jin wrote:
</pre>
<blockquote type="cite">
<pre wrap="">Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types.

Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed
&lt;&lt; static_cast&lt;int&gt;(thread)
^~~~~~~~~~~~~~~~~~~~~~~~

I need reinterpret_cast&lt;size_t&gt; here. I cannot reinterpret_cast&lt;int&gt; either, because of the difference in size. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 3:01 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a>
wrote:

Hi Xiao-Yong,

I see. In this particular case the pthread_t is only used to identify a thread and
to distinguish it from other threads for debugging purposes. So as long as the
compiler does not complain about the cast everything is fine. Cast to void * instead of
int would also be an option.

/// Jürgen


On 09/11/2017 08:38 PM, Xiao-Yong Jin wrote: </pre> <blockquote type="cite"> <pre wrap="">I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast&lt;int&gt; is alright, but may not be useful in the future. </pre> <blockquote type="cite"> <pre wrap="">On Sep 11, 2017, at 1:08 PM, Juergen Sauermann <a class="moz-txt-link-rfc2396E" href="mailto:***@t-online.de">&lt;***@t-online.de&gt;</a>

wrote:

Hi Xiao-Yong,

thanks, maybe fixed in SVN 1011.
Problem with that error is that the casted type is not a pointer, at least on my machine.

/// Jürgen


On 09/11/2017 06:55 PM, Xiao-Yong Jin wrote:


</pre>
<blockquote type="cite">
<pre wrap="">At revision 1010

Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information
out &lt;&lt; "thread #" &lt;&lt; setw(2) &lt;&lt; N &lt;&lt; ":" &lt;&lt; setw(16) &lt;&lt; int(thread)






</pre>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">
</pre>
</blockquote>
<pre wrap="">

</pre>
</blockquote>
<br>
</body>
</html>
Continue reading on narkive:
Loading...