What is the best way to duplicate PB's DIM AT in C/C++ ?
...
Hi Patrice,
Arrays can be mapped onto any space using a pointer variable and some casting. You just need to find a suitable encapsulation that matches your syntax requirements.
// ARRAY OVERLAY
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* m=(int*) malloc(1024); //heapspace
float*va=(float*) m; //map float array to m
va[1]=10.0; //test
printf("%f\n",va[1]);
free(m);
}
Thank you Charles, that's simple indeed.
By the way moving my librairies to C/C++ i can see all kind of benefits, including easy use of the latest technology, and get rid of having to translate all headers before starting to write anything.
I am amazed by the size of the resulting 64-bit DLL when everything is based on the flat API.
...
Patrice,
I fear you may be missing some very useful features of the C11 spec by using VS 2010. Even VS 2012 is still missing a lot.
MinGW just released version 4.8 today which includes all but 2.
The range based for loops , and the auto type inference feature as well as direct assignments to containers are very useful.
I have fallen quite heavily for vectors and intend to put together a bc9 tutorial on thier uses as well as examples of the other containers.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
// this is a C11 addition
vector<wstring> s ={ L"one", L"two", L"three", L"four", L"five"};
cout<<"s contains"<<endl;
for(auto it=s.begin();it!=s.end();++it)
{
wcout<<" "<<*it;
}
cout<<endl;
cout<<"in reverse"<<endl;
for(auto rit=s.rbegin();rit!=s.rend();++rit)
{
wcout<<" "<<*rit;
}
cout<<endl;
cout<<"range-based for loop"<<endl;
for ( auto it : s)
{
wcout<<" "<<it;
}
cout<<endl;
// sort it
std::sort(s.begin(),s.end());
// display it
for ( auto it : s)
{
wcout<<" "<<it;
}
cout<<endl;
return 0;
}
output:
s contains
one two three four five
in reverse
five four three two one
range-based for loop
one two three four five
five four one three two
James
My first target will be Visual Studio programmers :)
...
Thats a much more interesting market :-)
But you knew it before!
Quote from: James C. Fuller on March 23, 2013, 04:06:06 PM
Patrice,
I fear you may be missing some very useful features of the C11 spec by using VS 2010.
While handy, you pay a price for convenience. If he wants an extra 100-200K added to his DLL, then pulling in <iostream> and friends is certainly the way to go about it.