How to create a dynamically allocated array in Ada? - ada

As the questions says, I want to create a dynamically allocated array in Ada. Something like C++ std::vector ie., I don't want to store the length of the array in a separate variable like it is done here. As Ada supports generics, is it possible to create a std::vector like functionality in Ada?

The Standard Libraries includes Ada.Containers.Vectors (RM A.18.2)

Related

What is a tagged type in Ada?

Currently learning Ada and actually enjoying it, there is a something that bothers me: what's a tagged type? According to Programming in Ada 2012 by John Barnes, it indicates that the instantiated object carries a tag at run time.
I never heard of anything like this in C++ or C I think, so I'm a bit lost. What is it? When do I need it (apparently for having methods and inheritance?)?
It is simply a class. That's a way in Ada to declare the root of a class hierarchy. Another way is to use interfaces.
It is also, at the moment, the way to obtain dot notation for a type (but this will be generalized in Ada 2022).
See https://learn.adacore.com/courses/Ada_For_The_CPP_Java_Developer/chapters/08_Classes_and_Object_Oriented_Programming.html
So you rarely if ever directly manipulate the tag, just the same as vtables are behind the scenes providing the dispatching but you don't need to think about them in C++.
A notable difference with these languages is that T'Class can be used to refer to a whole family of derived types, and it must be used explicitly to achieve dynamic dispatch.
Tag in Ada is much like a Virtual Function Table Pointer in C++. That is, tagged type is the type having this one.
Virtual Function Table Pointer is allocated in the structure/class as soon as it have declared it's first virtual function. In Ada, you just have to declare it explicitly.
Both Ada's Tag and C++'s VFTPtr make a dynamic dispatch available.

Can I define a pointer in julia?

How do I define a pointer to a variable or list element in Julia? I have tried reading some resources but I am really confused about using a pointer in Julia.
You cannot have a pointer to a variable—unlike C/C++, Julia doesn't work like that: variables don't have memory locations as part of the language semantics; the compiler may store a variable in memory (or in a register), but that's none of your business 😁. Mutable objects, however, do generally live in memory and you can get a pointer to such an object using the pointer_from_objref function:
pointer_from_objref(x)
Get the memory address of a Julia object as a Ptr. The existence of
the resulting Ptr will not protect the object from garbage collection,
so you must ensure that the object remains referenced for the whole
time that the Ptr will be used.
This function may not be called on immutable objects, since they do
not have stable memory addresses.
See also: unsafe_pointer_to_objref.
Why the awful name? Because, really why are you taking pointers to objects? Probably don't do that. You can also get a pointer into an array using the pointer function:
pointer(array [, index])
Get the native address of an array or string, optionally at a given
location index.
This function is "unsafe". Be careful to ensure that a Julia reference
to array exists as long as this pointer will be used. The GC.#preserve
macro should be used to protect the array argument from garbage
collection within a given block of code.
Calling Ref(array[, index]) is generally preferable to this function
as it guarantees validity.
This is a somewhat more legitimate use case, especially for interop with C or Fortran, but be careful. The interaction between raw pointers and garbage collection is tricky and dangerous. If you're not doing interop then think hard about why you need pointers—you probably want to approach the problem differently.

An array- or vector-like type with values stored on disk in Julia

I am looking for an Array-like type with the following properties:
stores elements on disk
elements can have composite type
elements are read into memory, not the whole array
it is possible to write individual elements without writing the whole array
supports setindex!, getindex, push!, pop!, shift!, unshift! and maybe vcat
is reasonably efficient
So far I have found the following leads:
https://docs.julialang.org/en/latest/stdlib/SharedArrays/
http://juliadb.org
https://github.com/JuliaIO/JLD.jl
The first one seems promising, but it seems the type of the elements has to be isbits (meaning a simple number, some structs but not, e.g., an Array{Float64,1}). And it's not clear if the whole array contents are loaded into memory.
If it does not exist yet, I will of course try to construct it myself.
NCDatasets.jl addresses part of the requirements:
stores elements on disk: yes
elements can have composite type: no (although some support for composite type is in NetCDF4, but not yet in NCDatasets.jl). Currently you can have only Arrays of basic types and Arrays of Vectors (of basic types).
elements are read into memory, not the whole array: yes
it is possible to write individual elements without writing the whole array supports setindex!, getindex, push!, pop!, shift!, unshift! and maybe vcat: just setindex!, getindex
is reasonably efficient: the efficency is reasonable for me :-)
The project making it yourself sounds very interesting. I think it would server certainly a gap in the current ecosystem.
Some storage technologies that might be good to have a look at are:
HDF5 (for storage, cross-platform and cross-language)
JLD2 (successor of JLD) https://github.com/simonster/JLD2.jl
rasdaman (a "database" for arrays) http://www.rasdaman.org/
possibly also BSON http://bsonspec.org/
Maybe you can also reach out to the JuliaIO group.

How to efficiently expose many C++ data members of a single object to QML?

I have a C++ object pointer with many data members. I need them all available in QML. One obvious way would be to create a Q_PROPERTY for each member so they can be individually accessed via QML. However, if we are talking about dozens of data members, well that's a lot of lines of Q_PROPERTY, not to mention having to individually handle these in QML as separate properties as well (especially if dealing with "on changed" signals for each property).
I am wondering if it is possible to make a single Q_PROPERTY that would include all the data members I need. But what is not clear to me is the apparent mismatch between the types that QML supports and the types you can list in a Q_PROPERTY. For example, in QML, we have a basic string but its corresponding lising in a C++ Q_PROPERTY must be QString:
Q_PROPERTY(QString datastring READ showdata NOTIFY datastringChanged)
//in QML, `datastring` is just a plain string
Would there be more complex properties like lists or arrays that can be easily matched? QML has a list type and C++ has the QList but are these the same thing? Where can I find a listing of compatible types between C++ and QML?
On the other hand, having individual Q_PROPERTY for each data member could likely be better performance (my data is large and often changing) since the QML would not need to parse anything, perhaps.
Would there be more complex properties like lists or arrays that can
be easily matched? QML has a list type and C++ has the QList but are
these the same thing? Where can I find a listing of compatible types
between C++ and QML?
Have a look at the C++/JS data conversion help page. I think the list missed that QList<QObject*> is also possible.
On the other hand, having individual Q_PROPERTY for each data member
could likely be better performance (my data is large and often
changing) since the QML would not need to parse anything, perhaps.
Maybe, yes, depends on your performance needs. A C++ QList gets converted to a JavaScript list when accessed from QML/JS. That's a bit of conversion overhead indeed. Also, ff an item in the list changes, you need to emit the notify signal for the complete property, and every JS binding in which the list was used needs to be reevaluated, which will again be many list conversions. That could be better by having more fine-grained properties, but it really depends.
Btw, with Qt 5.1 there now is MEMBER, which makes writing Q_PROPERTYs a bit easier.

Linux Function Interception for OpenCL

I'm fairly new to C so be gentle.
I want to use the library interception method for Linux to replace calls to the OpenCL library with my own library. I understand that this can be done using LD_PRELOAD. So I can just re-implement the OpenCL functions as defined in the OpenCL header file within my own library which can then be linked against.
The problem is that this OpenCL header also contains some extern struct definitions, e.g.
typedef struct _cl_mem * cl_mem;
which are not defined within the OpenCL header. Is it possible these structs are defined within the OpenCL shared lib? If not, where might they be defined?
Cheers
Chris
That typedef declares a type pointing to a struct, the contents of which are undeclared. This means code using it can't do things like checking its size, copying the struct, or inspecting its contents - it simply has no idea what size it is.
This is a traditional technique in C to create an opaque, or private, type. You can declare the struct inside your OpenCL library, and the official header puts no restrictions on what that struct contains. It could even be empty, if all you need is an ID you can store in the pointer itself, though this is rarely done.
An example of the same technique used in the standard C library is the FILE type. It might be as simple as an integer file descriptor, or as complex as a struct containing the entire filesystem state; standard C code won't know. The particulars are known to the library only.
In short, you can declare that struct however you like - as long as you implement every function that handles that struct. The program that links to your library never handles the struct, only pointers to it.

Resources