Monday, October 26, 2015

Digest for comp.lang.c++@googlegroups.com - 25 updates in 7 topics

Rosario19 <Ros@invalid.invalid>: Oct 26 11:20AM +0100

do you like my first try to the code golf problem:
http://codegolf.stackexchange.com/questions/61684/calculate-the-bounded-cumulative-sum-of-a-vector
?
my OS tel me they are 800 bytes...
----------------------------------
#include <iostream.h>
#include <vector.h>
 
#define u32 unsigned
#define i32 int
#define R return
#define F for
#define SA(a) (sizeof(a)/(sizeof a[0]))
#define ooo cout
#define FF(a, b) for(a=0; a<b.size(); ++a)
 
template<class T> ostream& operator<<(ostream& ost, vector<T> m)
{u32 c;
FF(c, m){if(c==0) ooo<<"{";
ost<<m[c]<< (c+1!= m.size()? ", ": "}");
}
R ost;
}
 
void make(vector<i32>* a, vector<i32> o, i32 m, i32 b)
{u32 i;
i32 k, r=0;
FF(i,o){r+=o[i];
if(r<m) r=m;
if(r>b) r=b;
(*a)[i]=r;
}
}
 
int main(void)
{i32 v[]={1, 4, 6};
vector<i32> res(SA(v)), org(v,v+SA(v));
u32 c;
 
ooo<<"org="<<org<<"\n";
make(&res, org, 5, 10);
ooo<<"res="<<res<<"\n";
R 0;
}
seeplus <gizmomaker@bigpond.com>: Oct 26 03:51AM -0700

On Monday, October 26, 2015 at 9:20:17 PM UTC+11, Rosario19 wrote:
> do you like my first try to the code golf problem:
 
/shiver/
bartekltg <bartekltg@gmail.com>: Oct 26 02:41PM +0100

On 26.10.2015 11:20, Rosario19 wrote:
> #include <vector.h>
 
> #define u32 unsigned
> #define i32 int
 
Are You sure that int is 32 bits;>
 
 
> #define SA(a) (sizeof(a)/(sizeof a[0]))
> #define ooo cout
> #define FF(a, b) for(a=0; a<b.size(); ++a)
 
Do not do that:)
You aren't teenagers running in programming contents.
This method have some questionable advantages if you try solve
10 mathematically/algorithmically heavy but short problems in
a couple of hours and do not have time to write code, but
outside rapid solving competition is just awful style.
 
> #define ooo cout
 
WTF? ;-)
 
 
> ooo<<"res="<<res<<"\n";
> R 0;
> }
 
 
You arent solving the task - starting with that you do not read the
input:/
 
Hmm, you want to write the shortest code possible, but you create
a long head with defines and generic vector-stream operator?
You either write a good c++ code, or code for 'shortest code'
competition. Pick one;)
 
best
bartekltg
scott@slp53.sl.home (Scott Lurndal): Oct 26 04:42PM


>> #define u32 unsigned
>> #define i32 int
 
>Are You sure that int is 32 bits;>
 
In the vast majority of cases, yes. PDP-11 had a 16-bit
int; but there haven't been many systems using 16-bit int
since 1979.
Rosario19 <Ros@invalid.invalid>: Oct 26 06:03PM +0100

On Mon, 26 Oct 2015 11:20:03 +0100, Rosario19 <Ros@invalid.invalid>
wrote:
 
>?
>my OS tel me they are 800 bytes...
>----------------------------------
 
always wrong this is "2.432 byte"
 
#include <iostream.h>
#include <stdlib.h>
 
#define u8 unsigned char
#define i8 signed char
#define u16 unsigned short
#define i16 signed short
#define u32 unsigned
#define i32 int
#define f64 double
 
#define P printf
#define R return
#define F for
#define GG(a,b) if(a)goto b
#define G goto
#define S sizeof
#define SA(a) (sizeof(a)/(sizeof a[0]))
#define ooo cout
#define FE(a,b) for(a=0; a<b.len; ++a)
#define MM malloc
#define FF free
 
template<class T> class V{
public:
 
V()
{size=0; len =0; ele=0;
va =(T*)MM(10*S(T));
if(va==0){ele=1; e=1; R;}
size=10;
}
 
V(u32 s)
{size=0; len =0; ele=0;
va =(T*)MM(s*S(T));
if(va==0){ele=1; e=1; R;}
size=s;
}
 
V(T* s, u32 sz)
{u32 i;
size=0; len =0; ele=0;
va =(T*)MM(sz*S(T));
if(va==0){ele=1; e=1; R;}
F(i=0; i<sz; ++i)
va[i]=s[i];
len =sz;
size=sz;
}
 
V(T& o)
{u32 i;
size=0; len =0; ele=0;
va =(T*)MM(o.len*S(T));
if(va==0){ele=1; e=1; R;}
F(i=0; i<o.len; ++i)
va[i]=o.va[i];
len =o.len;
size=o.len;
}
 
~V(){FF(va);}
 
T& operator[](u32 i)
{T *p;
 
if(i>=size)
{if(i>=0xFFFFFE00)
R meno1;
p=(T*) realloc(va, (i+256)*S(T));
if(p==0) R meno1;
va=p; size=i+256;
}
if(i>=len) len=i+1; // i>len assign that element
R va[i];
}
 
V<T>& operator=(V<T>& a)
{u32 i;
T *p;
 
ele=0;
if(a.ele!=0)
{
l0: ele=1; G z; /* errore trovato e riportato */
}
if(a.len>size)
{p=(T*) realloc(va, a.len*S(T));
GG(p==0, l0); va=p; size=a.len;
}
FS(i,a) va[i]=a.va[i];
len=i;
z: R *this;
}
 
friend ostream& operator<<(ostream& ost, V<T>& m)
{u32 c;
if(m.ele) ost<<"{vector error}";
else FE(c,m){if(c==0) ost<<"{";
ost<<m[c]<< (c+1!= m.size? ", ": "}");
}
R ost;
}
 
u32 size;
u32 len;
T *va;
u32 ele; // element error
static T meno1; // element error1
static u32 e; // class error
};
template<class T> u32 V<T>::e=0;
template<class T> T V<T>::meno1=(T)-1;
 
 
f(V<i32>* a, V<i32>& o, i32 m, i32 b)
{i32 k,r=0,i; FE(i,o){r+=o[i]; (*a)[i]=r=(r<m?m:(r>b?b:r));}}
 
int main(void)
{i32 v[]={1, 4, 6};
V<i32> res(SA(v)), org(v,SA(v));
 
ooo<<"org="<<org<<"\n";
f(&res, org, 5, 10);
ooo<<"res="<<res<<"\n";
R 0;
}
 
------------------------------
 
in that example i think it is bugged
there is something there
one error i don't see...
 
for example if i change
f(V<i32>* a, V<i32>& o, i32 m, i32 b)
in
f(V<i32>* a, V<i32> o, i32 m, i32 b)
that goes in seg fault due to double free of address
org.va (for me)
 
because at start of f is not called contructor for "o" parameter
but it is called the distructor at end of f()
for free the address org.va

so i think i make one error capable generate error from code the
compiler
Geoff <geoff@invalid.invalid>: Oct 26 10:47AM -0700

On Mon, 26 Oct 2015 14:41:55 +0100, bartekltg <bartekltg@gmail.com>
wrote:
 
> > #define ooo cout
 
>WTF? ;-)
 
That should be std::cout. ;)
geoff <geoff@invalid.invalid>: Oct 26 11:04AM -0700

On 2015-10-26 17:03:21 +0000, Rosario19 said:
 
> On Mon, 26 Oct 2015 11:20:03 +0100, Rosario19 <Ros@invalid.invalid>
> wrote:
 
< utter garbage >
 
If you didn't write such absolute obfuscated shit that
you can't even parse yourself you would see the bugs in your code.
 
Nevertheless, here is your utter garbage, fixed.
 
#include <iostream>
#include <stdlib.h>
 
#define u8 unsigned char
#define i8 signed char
#define u16 unsigned short
#define i16 signed short
#define u32 unsigned
#define i32 int
#define f64 double
 
#define P printf
#define R return
#define F for
#define GG(a,b) if(a)goto b
#define G goto
#define S sizeof
#define SA(a) (sizeof(a)/(sizeof a[0]))
#define ooo std::cout
#define FE(a,b) for(a=0; a<b.len; ++a)
#define MM malloc
#define FF free
 
template<class T> class V{
public:
 
V()
{size=0; len =0; ele=0;
va =(T*)MM(10*S(T));
if(va==0){ele=1; e=1; R;}
size=10;
}
 
V(u32 s)
{size=0; len =0; ele=0;
va =(T*)MM(s*S(T));
if(va==0){ele=1; e=1; R;}
size=s;
}
 
V(T* s, u32 sz)
{u32 i;
size=0; len =0; ele=0;
va =(T*)MM(sz*S(T));
if(va==0){ele=1; e=1; R;}
F(i=0; i<sz; ++i)
va[i]=s[i];
len =sz;
size=sz;
}
 
V(T& o)
{u32 i;
size=0; len =0; ele=0;
va =(T*)MM(o.len*S(T));
if(va==0){ele=1; e=1; R;}
F(i=0; i<o.len; ++i)
va[i]=o.va[i];
len =o.len;
size=o.len;
}
 
~V(){FF(va);}
 
T& operator[](u32 i)
{T *p;
 
if(i>=size)
{if(i>=0xFFFFFE00)
R meno1;
p=(T*) realloc(va, (i+256)*S(T));
if(p==0) R meno1;
va=p; size=i+256;
}
if(i>=len) len=i+1; // i>len assign that element
R va[i];
}
 
V<T>& operator=(V<T>& a)
{u32 i;
T *p;
 
ele=0;
if(a.ele!=0)
{
l0: ele=1; G z; /* errore trovato e riportato */
}
if(a.len>size)
{p=(T*) realloc(va, a.len*S(T));
GG(p==0, l0); va=p; size=a.len;
}
FS(i,a); va[i]=a.va[i];
len=i;
z: R *this;
}
 
friend std::ostream& operator<<(std::ostream& ost, V<T>& m)
{u32 c;
if(m.ele) ost<<"{vector error}";
else FE(c,m){if(c==0) ost<<"{";
ost<<m[c]<< (c+1!= m.size? ", ": "}");
}
R ost;
}
 
u32 size;
u32 len;
T *va;
u32 ele; // element error
static T meno1; // element error1
static u32 e; // class error
};
template<class T> u32 V<T>::e=0;
template<class T> T V<T>::meno1=(T)-1;
 
 
i32 f(V<i32>* a, V<i32>& o, i32 m, i32 b)
{i32 r=0,i;FE(i,o){r+=o[i]; (*a)[i]=r=(r<m?m:(r>b?b:r));}R 0;}
 
int main(void)
{
i32 v[]={1, 4, 6};
V<i32> res(SA(v)), org(v,SA(v));
 
ooo<<"org="<<org<<"\n";
f(&res, org, 5, 10);
ooo<<"res="<<res<<"\n";
R 0;
}
 
Output:
 
org={1, 4, 6}
res={5, 9, 10}
Program ended with exit code: 0
Wouter van Ooijen <wouter@voti.nl>: Oct 26 07:13PM +0100

Op 26-Oct-15 om 5:42 PM schreef Scott Lurndal:
 
> In the vast majority of cases, yes. PDP-11 had a 16-bit
> int; but there haven't been many systems using 16-bit int
> since 1979.
 
8051
Mirochip PIC
Atmel AVR
MSP430
...
 
Wouter
scott@slp53.sl.home (Scott Lurndal): Oct 26 06:17PM


>> > #define ooo cout
 
>>WTF? ;-)
 
>That should be std::cout. ;)
 
Herr Ram would say "::std::cout".
Ian Collins <ian-news@hotmail.com>: Oct 27 07:23AM +1300

Rosario19 wrote:
 
> #define u32 unsigned
> #define i32 int
> #define R return
 
Idiot.
 
 
--
Ian Collins
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Oct 26 06:45PM +0100

On 10/26/2015 6:09 PM, Stefan Ram wrote:
> ^
 
> . Is it possible that this compiler does not consider
> classes with member initializers to be aggregates?
 
With MinGW g++ 5.1.0 I get the error you quote when using -std=c++11,
but, discounting warnings, it compiles with -std=c++14.
 
Visual C++ 2015 (version 19.00.23026 as reported by the compiler) does
not compile the code.
 
Upshot: this seems like a C++14 Microsoft-like feature (Microsoft-like:
something that some not very bright first-year student found useful in
one particular not very meaningful context, one would be forgiven for
calling it an idiot's device, and found a way to impose on everybody at
the cost of much else). Indeed the example you quote is there in the
C++14 standard, but not in the C++11 standard.
 
 
Cheers & hth.,
 
- Alf
ram@zedat.fu-berlin.de (Stefan Ram): Oct 26 05:04AM

>the "toupper(char(cz))" is yeilding 67, not C. What am I doing wrong?
 
67 /is/ 'C'! (Assuming ASCII)
 
#include <iostream>
#include <ostream>
#include <ios>
 
int main(){ ::std::cout << ::std::boolalpha <<( 67 == 'C')<< '\n'; }
 
You just need to cast the value to the appropriate type to
give a hint to the operator »<<« about how to output it.
 
>---
>This email has been checked ...
 
Oops, your computer seems to have been infected with a virus
that appends ads to your posts!
 
In Usenet, one can write anything into a signature, but what
I have quoted above /is not/ a signature!
ram@zedat.fu-berlin.de (Stefan Ram): Oct 26 05:09PM

I am using a compiler that seems to be GCC 4.9.2 with »-std=c++14«.
 
It reports an error on an example that was copied from 8.5.1p7:
 
int main()
{ struct S { int a; const char* b; int c; int d = b[a]; };
S ss = { 1, "asdf" }; }
 
3:22: error: could not convert '{1, "asdf"}' from '<brace-enclosed initializer list>' to 'main()::S'
S ss = { 1, "asdf" }; }
^
 
. Is it possible that this compiler does not consider
classes with member initializers to be aggregates?
 
I have this presumption because the following program
is accepted without an error message:
 
int main()
{ struct S { int a; const char* b; int c; int d; };
S ss = { 1, "asdf" }; }
 
.
Rosario19 <Ros@invalid.invalid>: Oct 26 10:51AM +0100

c++ stl vector--- how to make difficult what is easy
 
constructor: call call call call call never end
 
than
vector<int> b;
 
where is b.len the len of vector as u32
and
b.size the size of the vector as u32
[effective mem position already allocated]
 
do you know that in
for(;i<b.size(); ++i)
that call the function b.size() in the loop instead of
doing something more simple as i<b.size
or better i<b.len
?
Rosario19 <Ros@invalid.invalid>: Oct 26 11:55AM +0100

On Mon, 26 Oct 2015 10:51:12 +0100, Rosario19 <Ros@invalid.invalid>
wrote:
 
> and
> b.size the size of the vector as u32
> [effective mem position already allocated]
 
?
pheraps i understand...
 
they are not there because someone think programmer can change these
value and what can change these value is only class vectror
 
fear pure fear
 
and even if the programmer change that number?
it is supposed that for doing that operation know all the
consegueces...
 
 
 
Rosario19 <Ros@invalid.invalid>: Oct 26 12:00PM +0100

On Mon, 26 Oct 2015 11:55:01 +0100, Rosario19 wrote:
 
 
>they are not there because someone think programmer can change these
>value and what can change these value is only class vectror
 
>fear pure fear
 
in all my classes i use [pheraps not are too many and are small]
i never make the above error in all my life
at last believe that
and all type in my classes are public
and in every part of the code i can change them
[admit rarely chage them out the classes function]
 
bartekltg <bartekltg@gmail.com>: Oct 26 02:25PM +0100

On 26.10.2015 10:51, Rosario19 wrote:
 
> do you know that in
> for(;i<b.size(); ++i)
> that call the function b.size() in the loop instead of
 
No, it does not.
 
Look at the output assembler code. The size is copied to a register.
 
And even if it would not, the size is just a integer inside
the container, {return _size;} just like you described below.
 
> doing something more simple as i<b.size
> or better i<b.len
 
This is the beauty of c++. It work just like that (or even better),
but we still have the abstraction, all containers have similar
interface. With a decent optimalization level
size(){
return _size; }
is translated do inlined 'show _size', and in loop, it is optimized
further, so _size is copied to a register to eliminate memory access.
 
 
But is ti good you talk about this, beginners often are afraid
of this and write awful code to 'be faster'.
 
bast
bartekltg
Bo Persson <bop@gmb.dk>: Oct 26 05:09PM +0100

On 2015-10-26 10:51, Rosario19 wrote:
> doing something more simple as i<b.size
> or better i<b.len
> ?
 
What?!
 
b.size() is inlined and the result is i < b.m_size
 
 
 
Bo Persson
David Brown <david.brown@hesbynett.no>: Oct 26 10:38AM +0100

On 25/10/15 18:01, Jorgen Grahn wrote:
>> correct dependencies, so that they don't need rebuilt unnecessarily either.
 
> That's one solution I've encountered. It's sane, and works. I think
> the gcc manual more or less describes how to set it up?
 
There are common recipes for using gcc "-M" flags to generate dependency
information, and for using them together with sed to make Makefile
dependency files. I think the gnu make manual has examples.
 
However, the usual process generates dependency files looking like this:
 
# This is file main.d
main.o : main.c module1.h module2.h module3.h
 
Typically the file "main.d" is re-build whenever "main.c" changes. But
that would miss if "module2.h" was changed to also include "module4.h".
So you either have to re-build your dependency files manually on
occasion, or rebuild them on every change to any header, or (roughly
like Eclipse does) simply rebuild them for every build.
 
My Makefile has a somewhat more complex sed arrangement, and produces
files like this:
 
# This is file main.d
main.o main.d : main.c module1.h module2.h module3.h
 
This means dependency files get rebuilt as and when they are needed.
 
 
> it doesn't seem all that dangerous to require that, but then the
> project grows and more people show up ... and slowly it becomes a
> rather high hidden cost.
 
Agreed.
 
> to check the modification time on pretty much every file. I suspect
> many file systems are optimized for that kind of usage ... especially
> since tools like Git also rely heavily on that.
 
Again, you'll find Linux faster for this sort of thing than Windows.
The common Linux filesystems all handle such metadata checking very
quickly, and typically it is all from caches.
David Brown <david.brown@hesbynett.no>: Oct 26 11:02AM +0100

On 25/10/15 19:08, mark wrote:
 
> Windows >= 7 works perfectly fine, if you have enough RAM. There is very
> little disk activity after the first build. IME, there is no difference
> in rebuild speed between an SSD and a 'normal' hard drive.
 
Certainly it is better than with early Windows, and certainly more RAM
typically makes a bigger difference than moving to an SSD. Windows is
gradually catching up with the *nix world here.
 
(I am not anti-Windows, or at least not /entirely/ anti-Windows.
Windows works fine for many purposes, and can be better than *nix for
some things. My desk has a Windows machine and a Linux machine, and I
use both. But for dealing with lots of files, file metadata, parallel
access to files, multiple processes - *nix is a good deal more efficient
IME. Linux is a far better platform for software development, at least
for the type of development I do and the way I like to work.)
MikeCopeland <mrc2323@cox.net>: Oct 25 09:32PM -0700

The following code doesn't work (as I want it to). Specifically, it
produces "20367c", whereas I want it to yield "203Cc". For some reason,
the "toupper(char(cz))" is yeilding 67, not C. What am I doing wrong?
Please advise. TIA
 
ostringstream ossz;
int nx1 = 2;
char cz = char(nx1+'a');
ossz.str(""), ossz << "2" << setfill('0') << setw(2)
<< (nx1+1) << toupper(char(cz))
<< char(cz);
 
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
MikeCopeland <mrc2323@cox.net>: Oct 25 10:54PM -0700

In article <C-20151026060335@ram.dialup.fu-berlin.de>, ram@zedat.fu-
berlin.de says...
> >the "toupper(char(cz))" is yeilding 67, not C. What am I doing
wrong?
 
> 67 /is/ 'C'! (Assuming ASCII)
 
Indeed it is, but I want the output to be "C", not "67".
Is my cast operation wrong?
 
ostringstream ossz;
int nx1 = 2;
char cz = char(nx1+'a');
ossz.str(""), ossz << "2" << setfill('0') << setw(2)
<< (nx1+1) << toupper(char(cz))
<< char(cz);
 
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Robert Wessel <robertwessel2@yahoo.com>: Oct 26 01:38AM -0500

On Sun, 25 Oct 2015 22:54:59 -0700, MikeCopeland <mrc2323@cox.net>
wrote:
 
>ossz.str(""), ossz << "2" << setfill('0') << setw(2)
> << (nx1+1) << toupper(char(cz))
> << char(cz);
 
 
toupper() returns an int. Feeding an int to a stream will result in a
number being emitted. Cast the output of toupper().
Paavo Helde <myfirstname@osa.pri.ee>: Oct 26 01:43AM -0500

MikeCopeland <mrc2323@cox.net> wrote in news:MPG.30976e1acf2e5c9e9896b6
 
>> 67 /is/ 'C'! (Assuming ASCII)
 
> Indeed it is, but I want the output to be "C", not "67".
> Is my cast operation wrong?
 
Yes, cz is already a char, so there is no point to cast it to char again.
On the other hand, result of toupper() is an int and needs to be somehow
converted back to char. I.e. instead of
 
toupper(char(cz))
 
you need
 
char(toupper(cz)).
 
 
> ossz.str(""), ossz << "2" << setfill('0') << setw(2)
> << (nx1+1) << toupper(char(cz))
> << char(cz);
 
PS. Note that toupper() behavior is undefined for most negative input
values (and char is signed in common implementations), so if you are not
100% sure your texts are ASCII-only one should better check for that or
at least cast cz to unsigned char before passing to toupper.
cdalten@gmail.com: Oct 25 09:26PM -0700

???
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: