Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sat Jun 16, 2018 11:40 pm

jahboater,

Interesting.

I have done some sniffing around for what is in the various new C++ standards contain and can find no evidence that it includes variable length arrays.

It's impossible to determine from the actual standards document which is nearly 2000 pages long and completely unreadable: http://www.open-std.org/jtc1/sc22/wg21/ ... /n4659.pdf

Articles about C++17 don't mention VLAs:
https://stackoverflow.com/questions/380 ... res-in-c17
https://blog.smartbear.com/development/ ... ould-care/
https://medium.com/@afinlay/new-languag ... 953ff64c65

More formal change documents don't mention VLA's http://open-std.org/JTC1/SC22/WG21/docs ... 636r1.html

I had thought I read some place that GCC and clang supported VLAs as an extension. But now I find only references to them supporting VLA's for pre C99 versions of C, not C++.

If anyone can locate some definitive information on this it would be great. Perhaps we cannot find it because it does not exist!

Edit: Fixed link to C++ standard.
Last edited by Heater on Sun Jun 17, 2018 9:37 am, edited 1 time in total.

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 1:30 am

Heater wrote:
Sat Jun 16, 2018 11:40 pm
It's impossible to determine from the actual standards document which is nearly 2000 pages long and completely unreadable: http://www.open-std.org/jtc1/sc22/wg21/ ... /n4685.pdf
Thats part of a draft.
The actual standard has now finally been published, its available here:-
https://webstore.ansi.org/FindStandards ... 017%7cnull
sadly you have to pay for it, and it is twice the price of the C17 standard :(
At 1618 pages long, you get quite a lot for your money.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 2:35 am

I ain't paying for the final version.

Sorry, yes, I should not have casually said "actual standards". Presumably the drafts are plenty close enough to the finished product. After all GCC, Clang MS, etc, have been implementing those features from proposals and drafts for ages already.

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 6:54 am

Please could you could fix the link, currently it points to a small part of the doct about ranges?

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 9:38 am

Opps. Sorry. I meant this C++ draft standard:

http://www.open-std.org/jtc1/sc22/wg21/ ... /n4659.pdf

Also fixed in the above post.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 9:58 am

Hmm...looking at the draft. If we restrict ourselves to the actual language definition and ignore all that useless library junk we only have 446 pages to deal with. Much nicer. Sounds like I have some bedtime reading ahead...

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 12:27 pm

Heater wrote:
Sun Jun 17, 2018 9:58 am
Hmm...looking at the draft. If we restrict ourselves to the actual language definition and ignore all that useless library junk we only have 446 pages to deal with. Much nicer. Sounds like I have some bedtime reading ahead...
There are now several books on C++17 but none of them cover the entire language.
When Mr Stroustrups book is updated it may do.

So I believe the ISO standard is the only source that is both complete and obviously definitive.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 4:50 pm

I guess that is the end of the line of my looking into variable sized arrays in C++.

I am not going to live long enough to comprehend the full complexity that the C++ monster has become.

In fact, I don't believe that even if you were born today you would live long enough to ever understand the fractal complexity of C++ in your lifetime.

Years ago, I thought that given a language like C++ the best thing to do with it is use it's low level efficiency to create some more reasonable language that you can actually do some useful work in easily. I guess that is what happened with the creation of Java, C#, Python, Javascript etc.

Today I only drop to C/C++ when I feel the need for speed. Or I'm working on a memory constrained embedded system.

Except... what I'd like is for some bright spark to create some class / template arrangement in C++ such that any variable I define with it behaves like a Javascript variable. Then I can have variable length and dynamically sized arrays, easily runtime extensible objects, add numeric strings to ints and floats, automatic garbage collection, closures, lambdas, and all that good clarity and flexibility of a dynamically typed language like Javascript in C++ itself. But with the added bonus that when I feel the need for speed I still have all the low level facilities of C available, in the same source in the same language.

Oh and whilst we are at it, said class / template arrangement would allow for event driven programming in C++ as simply as JS, allowing one to juggle multiple tasks at the same time without all the inefficiency and complexity of threads and mutual exclusion etc.

Ok, rant over.

ejolson
Posts: 3078
Joined: Tue Mar 18, 2014 11:47 am

Re: Dynamic sized arrays

Sun Jun 17, 2018 6:24 pm

Heater wrote:
Sun Jun 17, 2018 4:50 pm
I guess that is what happened with the creation of Java, C#, Python, Javascript etc.
I think Python allows you to link to C libraries and subroutines relatively easily; with the others it seems not so easy. For many people, working with a project written in multiple languages is often more difficult than just one, even if that one language happens to be C.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Sun Jun 17, 2018 7:11 pm

That's not quite what I was getting at.

What I meant was that those languages, Python, Java, C#, JS, etc, are themselves created using C/C++. Their interpreters, compilers (to byte code) run time systems and dynamic optimizers etc.

I'm very sure that Java compilers and run times are not written in Java. Similarly for all the others. Somebody correct me if I'm wrong in some case or other.

The ease with which code written in these languages can interoperate with C or C++, in the same executable, is a whole other can of worms.

I have no idea about Python, but Lua makes it dead easy, Java seemed easy enough with its Java Native Interface. Javascript under node.js is possible but but perhaps not so simple.

ejolson
Posts: 3078
Joined: Tue Mar 18, 2014 11:47 am

Re: Dynamic sized arrays

Sun Jun 17, 2018 9:44 pm

Heater wrote:
Sun Jun 17, 2018 7:11 pm
Java seemed easy enough with its Java Native Interface.
My understanding of JNI is that strings and arrays are pass by value. This makes it somewhat useless when working with arrays that are large.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 1:19 am

It's a long time since I played with JNI so I don't recall. That does sound pretty hopeless.

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 8:46 am

Heater wrote:
Sun Jun 17, 2018 4:50 pm
Except... what I'd like is for some bright spark to create some class / template arrangement in C++ such that any variable I define with it behaves like a Javascript variable. Then I can have variable length and dynamically sized arrays, easily runtime extensible objects, add numeric strings to ints and floats, automatic garbage collection, closures, lambdas, and all that good clarity and flexibility of a dynamically typed language like Javascript in C++ itself. But with the added bonus that when I feel the need for speed I still have all the low level facilities of C available, in the same source in the same language.
That might be a good project to do yourself.
You would learn some more of "modern" C++ as a by product.
When you do the research, you might find that some of it is already in C++17 ...
Lambda's obviously, all the countless container classes and stuff in the STL, adding strings to scalar's (trivial).
Closures (I think) could be done with lambda's (see captures). Event driven stuff with callbacks.
GC might be hard though, unless you just implement it for your new class only perhaps.

User avatar
PeterO
Posts: 4732
Joined: Sun Jul 22, 2012 4:14 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 9:08 am

http://www.theregister.co.uk/2018/06/18 ... plus_plus/

"What's all the C Plus Fuss? Bjarne Stroustrup warns of dangerous future plans for his C++
Language creator calls proposals 'insanity'"

Clearly even BS realises C++ is out of control !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 9:30 am

Thanks for posting that, very interesting.
If you read it all, he is not that negative.
But I saw this amusing spoof in the comments :-
http://www-users.cs.york.ac.uk/susan/joke/cpp.htm
:)

User avatar
PeterO
Posts: 4732
Joined: Sun Jul 22, 2012 4:14 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 9:46 am

jahboater wrote:
Mon Jun 18, 2018 9:30 am
Thanks for posting that, very interesting.
If you read it all, he is not that negative.
But I saw this amusing spoof in the comments :-
http://www-users.cs.york.ac.uk/susan/joke/cpp.htm
:)
That's so ...... believable :-)

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 10:00 am

Heater wrote:
Sun Jun 17, 2018 4:50 pm
But with the added bonus that when I feel the need for speed I still have all the low level facilities of C available, in the same source in the same language.
This morning I saw some code produced by the C compiler:-

Code: Select all

   uint64_t n, base;
    if( n > UINT64_MAX / base )
    {
      handle overflow 
    }
    n *= base;
Looks like an expensive pre-check for wrap doesn't it.
But the compiler produced just two instructions (a multiply and a conditional jump) to handle it all except the overflow error handler. No costly division. No large literal.
Somehow it deduced the intention of the C code - or is it a common idiom that it recognized?
I do like C for sheer speed!

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 11:03 am

Such optimizations, surprising as the are, are common in modern compilers. I'm sure you will find that if you compile that as C++ it will produce the same instructions.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 12:16 pm

jahboater,
That might be a good project to do yourself.
It's a thought that crosses my mind now and then. Usually when I'm having to maintain some of the C++ programs we use in house. And again recently when I had to create a whole new application in C++. Grrr...
When you do the research, you might find that some of it is already in C++17 ...
Actually the thought first started to crossed my mind a couple of years back when I found such things as "lambdas", "captures", "auto", etc were turning up in new C++ standards. "Ah", I thought, "they are trying to bring the power and sophistication of Javascript to C++". But in the hideous, clunky and complex way C++ does these things.

Adding strings to scalars is trivial as you say. I guess with the definition of some variant class and enough operator overloading and such most of the dynamic nature of JS variables could be achieved.

Can't we get the garbage collection using shared_ptr or something?

jahboater
Posts: 4463
Joined: Wed Feb 04, 2015 6:38 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 12:24 pm

Heater wrote:
Mon Jun 18, 2018 12:16 pm
Can't we get the garbage collection using shared_ptr or something?
Wow, I didn't know about that. Well spotted.
Looks like it was designed for the job!

http://en.cppreference.com/w/cpp/memory/shared_ptr

This en.cppreference.com site is a useful resource by the way.

Heater
Posts: 12708
Joined: Tue Jul 17, 2012 3:02 pm

Re: Dynamic sized arrays

Mon Jun 18, 2018 12:31 pm

There is also unique_ptr which ensures an object only has one owner at a time and is garbage collected when the last owner forgets about it. Very useful.

Return to “C/C++”