Tuesday, December 6, 2016

Digest for comp.lang.c++@googlegroups.com - 15 updates in 3 topics

ruben safir <ruben@mrbrklyn.com>: Dec 06 01:42PM -0500

On 12/06/2016 10:10 AM, Scott Lurndal wrote:
 
>> I am storing the data in a uint32_t variable using the following code, but the value keeps showing up with a huge number 218103808 which happens to be the number that is evaluated by iostream for the value of the whole chunk
 
> https://en.wikipedia.org/wiki/Endianness
 
that doesn't help
scott@slp53.sl.home (Scott Lurndal): Dec 06 07:05PM


>> https://en.wikipedia.org/wiki/Endianness
 
>that doesn't help
 
And nobody here is obligated to help you - you should learn to
help yourself, and the link referenced above should be your starting
point.
ruben safir <ruben@mrbrklyn.com>: Dec 06 02:33PM -0500

On 12/06/2016 02:05 PM, Scott Lurndal wrote:
 
> And nobody here is obligated to help you - you should learn to
> help yourself, and the link referenced above should be your starting
> point.
 
no it is not really. Like most wikipedia articles it is written poorly
and leaves of coherent details. Your under no obligation to post, if
you don't want to contribute. Being an ass isn't helpful though and
treating me like I'm stupid makes me resentful
Luuk <luuk@invalid.lan>: Dec 06 08:51PM +0100

On 06-12-16 20:33, ruben safir wrote:
> and leaves of coherent details. Your under no obligation to post, if
> you don't want to contribute. Being an ass isn't helpful though and
> treating me like I'm stupid makes me resentful
 
Basically the wikipeadia page explains what is stated in the docs here:
http://www.libpng.org/pub/png/spec/1.2/PNG-DataRep.html#DR.Integers-and-byte-order
David Brown <david.brown@hesbynett.no>: Dec 06 09:41PM +0100

On 06/12/16 20:33, ruben safir wrote:
> and leaves of coherent details. Your under no obligation to post, if
> you don't want to contribute. Being an ass isn't helpful though and
> treating me like I'm stupid makes me resentful
 
The Wikipedia article there is reasonably written, and full of useful
information. But you may not have made the connection as to why it is
relevant to your problem.
 
Numbers bigger than single bytes in computing can be stored in two basic
formats - big endian with the most significant byte first, and little
endian with the least significant byte first. Some processors use one
format, other processors use the other. Some file formats and protocols
use one format, others use the other. If the processor and the file
format do not match, then you need to convert when reading or writing
the format.
 
x86 uses little endian format, so 13 is stored as 0b 00 00 00 as a
32-bit integer. PNG, like many network-related formats, uses big
endian. So it stores 32-bit 13 as 00 00 00 0b. (Incidentally, use hex
for this sort of thing - octal had no place in computing outside of
"chmod" since the 1970's.)
 
Assuming you are trying to learn and understand this, rather than
copy-and-paste working code, then this should be enough to get you going.
ruben safir <ruben@mrbrklyn.com>: Dec 06 05:12PM -0500

On 12/06/2016 03:41 PM, David Brown wrote:
> The Wikipedia article there is reasonably written
 
no it isn't. But I'll show you a means of properly answering a question
like this
 
http://www.nylxs.com/messages.html?id=543540&archive_learn=2016-12-01
 
 
All it takes is a basic assumption that your not talking to an idiot.
 
Specifically that wikipedea article, and really they all suck, don't
explain how the intel hardware is set up and who to evaluate the and
learn the problem solving mechanism, so that one can learn to evaluate
these kinds of problems in the future.
ruben safir <ruben@mrbrklyn.com>: Dec 06 05:14PM -0500

On 12/06/2016 03:41 PM, David Brown wrote:
> "chmod" since the 1970's.)
 
> Assuming you are trying to learn and understand this, rather than
> copy-and-paste working code, then this should be enough to get you going.
 
 
thanks, excellent. What I don't understand though is why when I set up
a loop and take it by the byte that the order is correct. It gets 00 00
00 and then 0d
 
(which is 13)
Jorgen Grahn <grahn+nntp@snipabacken.se>: Dec 06 11:24PM

On Tue, 2016-12-06, ruben safir wrote:
 
> thanks, excellent. What I don't understand though is why when I set up
> a loop and take it by the byte that the order is correct. It gets 00 00
> 00 and then 0d
 
I can't answer that, and I didn't read the code. However, treating
the file as a series of bytes /is/ the right thing to do, so it
doesn't surprise me if the result is correct. If the file looks like
 
f0 0d 12 34 00 00 00 0d 47 11
-----------
 
and the file format specification says "we store an integer in
big-endian form in the marked area", I'd read it using a function
similar to this one:
 
static unsigned get_bigendian32(const uint8_t* p)
{
unsigned n = 0;
n = (n<<8) | *p++;
n = (n<<8) | *p++;
n = (n<<8) | *p++;
n = (n<<8) | *p++;
return n;
}
 
(You also have to watch out for buffer overflows.)
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Paavo Helde <myfirstname@osa.pri.ee>: Dec 07 01:25AM +0200

On 7.12.2016 0:14, ruben safir wrote:
> a loop and take it by the byte that the order is correct. It gets 00 00
> 00 and then 0d
 
> (which is 13)
 
What you mean by "correct"? That the big-endian convention follows the
convention for hand-written numbers, at least in the Western cultural
tradition? This was probably the initial motivation behind the
big-endian format (?)
 
By the way, this is all explained on the Wikipedia page with graphs!
 
nrhth
Paavo
Paavo Helde <myfirstname@osa.pri.ee>: Dec 06 09:03PM +0200

On 6.12.2016 20:00, Stefan Ram wrote:
> integral type, the above program might not be ill-formed
> under such an implementation, so whether a program is
> ill-formed depends on the implementation used?
 
From the standard:
 
"A conforming implementation may have extensions (including additional
library functions), provided they do not alter the behavior of any
well-formed program. Implementations are required to diagnose programs
that use such extensions that are ill-formed according to this
International Standard. Having done so, however, they can compile and
execute such programs."
 
So, as long as the web compiler produced some warning message about the
too large number, it behaved correctly. For example, gcc 5.3.0 produces
a warning:
 
tmp>g++ test22.cpp
test22.cpp:3:29: warning: integer constant is too large for its type
int main() { ::std::cout << 340282366920938463463374607431768211456 <<
"\n"; }
Lap: tmp>./a.exe
0
ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 03:53PM

>Indeed, and that's what I am doing now, see my answer to Ã-ö. But whether
>this is straightforward (compared to my initial idea which seemed much
>more straightforward to me) lies in the eye of the beholder. ;-)
 
The documentation for »::std::set« says
 
template< class Key, class Compare ~~~ >
 
(»~~~« indicates an omission by me). So, when I wrote
"straightforward", I was thinking of passing an argument
as the parameter requires it, i.e., a type.
ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 04:08PM

>(»~~~« indicates an omission by me). So, when I wrote
>"straightforward", I was thinking of passing an argument
>as the parameter requires it, i.e., a type.
 
Here is a function-to-type-adaptor:
 
#include <functional>
#include <initializer_list>
#include <iostream>
#include <ostream>
#include <set>
 
using fptr =
bool( * )( const ::std::set< int >&, const ::std::set< int >& );
 
template< fptr f >struct fun_to_type_adaptor
{ bool operator()
( const ::std::set< int >& x, const ::std::set< int >& y )const
{ return f( x, y ); }};
 
bool f( const ::std::set< int >& x, const ::std::set< int >& y )
{ return x.size() == y.size() ? x < y : x.size() < y.size(); }
 
using setset =
::std::set < ::std::set< int >, fun_to_type_adaptor< f >>;
 
int main() { ::std::cout << 1 << '\n'; }
 
»setset« is now being defined using the function »f« using
the function-to-type adaptor »fun_to_type_adaptor«. Still,
it seems, one cannot use a lambda expression there.
ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 05:22PM

>»setset« is now being defined using the function »f« using
>the function-to-type adaptor »fun_to_type_adaptor«. Still,
>it seems, one cannot use a lambda expression there.
 
»A lambda-expression shall not appear in« ...
»a template-argument« 5.1.5p3
ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 06:00PM

I got this diagnostics
 
main.cpp:9:29: error: integer constant is too large for its type
int main() { ::std::cout << 340282366920938463463374607431768211456 << "\n"s; }
^
C++ says:
 
A program is ill-formed if one of its translation units contains an
integer literal that cannot be represented by any of the allowed types.
 
A web compiler printed »0« for the above program recently IIRC.
 
Is an implementation required to print a diagnostic message
for an ill-formed program¹, and - when being executed - does
an ill-formed program have undefined behavior?
 
(¹If so, then that web compiler might not be conforming.)
 
But since there might be an implementation with a very large
integral type, the above program might not be ill-formed
under such an implementation, so whether a program is
ill-formed depends on the implementation used?
ram@zedat.fu-berlin.de (Stefan Ram): Dec 06 06:57PM

>Is an implementation required to print a diagnostic message
>for an ill-formed program¹, and - when being executed - does
>an ill-formed program have undefined behavior?
 
The web says so.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: