Access three dimensional array via pointer - c++

How do I access a three-dimensional array via pointers? At the moment I have this code, trying to get the same results. I need the access via pointers to use the values in the array in OpenCL.
for (l = 0; l < NumberOfLayers - 1; l++) {
for (i = 0; i < NeuronsPerLayer[l]; i++) {
for (j = 0; j < NeuronsPerLayer[l + 1] - bias[l + 1]; j++) {
cout<<Synweights[l][i][j]<<endl;
cout<<*(*Synweights[0]+l*NumberOfLayers + i * NeuronsPerLayer[l] + j)<<endl;
}
}
}
Synweights is declared as:
double ***Synweights
Synweights = (double ** *)malloc((NumberOfLayers - 1) * sizeof(double **));
for (l = 0; l < NumberOfLayers - 1; l++) {
Synweights[l] = (double **)malloc(NeuronsPerLayer[l] * sizeof(double *));
for (i = 0; i < NeuronsPerLayer[l]; i++) {
Synweights[l][i] = (double *)malloc((NeuronsPerLayer[l + 1] - bias[l + 1]) * sizeof(double));
}
}

It depends on the structure of your arrays.
Synweights is an array of arrays of arrays which means there are many memory blocks at different locations. If you have a pointer to such a structure you can do the very same with a pointer:
float p*** = new float**[sizeL];
for(int l=0; l<sizeL; ++l)
{
p[l] = new float*[sizeI];
for(int i=0; i<sizeI; ++i)
p[l][i] = new float[sizeJ];
}
...
p[l][i][j] = 0; // Access
In case you have just on block of memory which is interpreted as 3D array you need to do some arithmetic. This should be the case you need for CpenCL anyway.
float* p = new float[sizeL*sizeI*sizeJ];
...
p[(l*sizeI+i)*sizeJ+j] = 0; // Access
p + ((l*sizeI+i)*sizeJ+j); // Direct address/pointer calculation
This variant is faster due to cache performance and less address computations. You can store the computed index if you intend to access more than once.

A three dimensional array may be accessed in the following way;
T* myArray = new T[3][4][5];
T* aParticularT = myArray[1][2][2];
T* anArrayOfTs = myArray[1][2];
T* aTwoDimensionalArrayOfTs = myArray[1];
T* aThreeDimensionalArrayOfTs = myArray;
aTypeName aMemberOfT = aParticularT->memberName;

Related

How to return a 2D array in C++ using pointers, the error I get is "Cannot convert 'int (*)[size]' to 'int**'

I am currently building a median filter in C++. I have a decent amount of experience with other languages but C++ and its pointers confuse me. I am building a function which takes in a 2D array of RGB values of an image. The function may not be 100% yet but I just cannot get past returning the 2d array. My input parameters is the row major version of the image array and the filter size and the output is the pointer to the filtered 2D array. It has the following error when debugging >"Cannot convert 'int (*)[size]' to 'int"
Can you possibly walk me through this error and how to deal with it?
Also if you spot any other peculiarities please mention it, it would be greatly appreciated!
int** seq_medFilter(int image[][3], int filter)
{
int output[640 * 480][3];
int rows = 640;
int cols = 480;
int fil_arr_size = pow((2 * filter + 1), 2);
for (int i = 0; i<rows*cols; ++i)
{
int temp[fil_arr_size][3];
//edge cases excluded
int current_col = i / cols;
int current_row = i%cols;
if (current_col < filter || current_col > cols - filter - 1 || current_row < filter || current_row > rows - filter - 1)
{
for (int j = 0; j<3; j++) {
output[i][j] = image[i][j];
}
}
else
{
// just for a filter size of one now
int pos_x = i / cols - filter;
int pos_y = i%cols - filter;
for (int x = 0; x< fil_arr_size - 1; ++x)
{
for (int j = 0; j<3; j++) {
temp[x][j] = image[pos_x*cols + pos_y][j];
}
pos_x += 1;
if (pos_x == (2 * filter + 1))
{
pos_x = pos_x - (2 * filter + 1);
pos_y += 1;
}
}
int N = sizeof(temp) / sizeof(temp[0]);
sort(temp, temp + N);
for (int j = 0; j<3; j++) {
output[i][j] = temp[N / 2][j];
}
}
}
return output;
}
int main()
{
return 0;
}
The issue is that you cannot return a int output[][] as an int **. They are considered different types, but also, output is a local variable, and thus cannot be returned as a pointer without causing UB.
You could use a vector instead, like so:
std::vector<std::vector<int>> seq_medFilter(int image[][3], int filter)
{
std::vector<std::vector<int>> output( 640 * 480, std::vector<int>( 3 ) );
//...
If you insist on using pointers, then you can used unique_ptr/shared_ptr, or use new, though I would say that all three of these options are worse than just using a vector here.
You could also use an std::array
Example:
std::array<std::array<int, 3>, 640*480> seq_medFilter(int image[][3], int filter)
Then, where you declare output, you would change its type to
std::array<std::array<int, 3>, 640*480> output;
Note that the line:
int temp[fil_arr_size][3];
Is not valid in standard C++ (see here).
For completeness, using the pointer method, you would keep your function head the same, but then use:
int **output = new int*[640*480];
for ( size_t idx = 0; idx < 640*480; ++idx ) {
output[idx] = new int[3];
}
Again, I don't recommend this method.

Receive values from dynamic array

I recently asked question about how to work with element Edit1 dynamically, now I want to ask something about values, which I received from dynamical arrays. First I try to divide image into sectors:
const n=20;
unsigned short i, j, line_length, w = Image1->Width, h = Image1->Height, l = Left + Image1->Left, t = Top + Image1->Top;
unsigned short border = (Width-ClientWidth)/2, topborder = Height-ClientHeight-border;
Image1->Canvas->Pen->Color = clRed;
for (i = 0; i <= n; i++)
{
Image1->Canvas->MoveTo(0, 0);
line_length = w * tan(M_PI/2*i/n);
if (line_length <= h)
Image1->Canvas->LineTo(w, line_length);
else
{
line_length = h * tan(M_PI/2*(1-1.*i/n));
Image1->Canvas->LineTo(line_length, h);
}
}
Then I use regions to count black dots in each sector and I want to add values to element Memo:
HRGN region[n];
TPoint points[3];
points[0] = Point(l + border, t + topborder);
for (i = 0; i < n; i++)
{
for (j = 0; j <= 1; j++)
{
line_length = w * tan(M_PI/2*(i+j)/n);
if (line_length <= h)
points[j+1] = Point(l + border + w, t + topborder + line_length);
else
{
line_length = h * tan(M_PI/2*(1-1.*(i+j)/n));
points[j+1] = Point(l + border + line_length, t + topborder + h);
}
}
region[i] = CreatePolygonRgn(points, 3, ALTERNATE); // or WINDING ?? as u want
}
Byte k;
unsigned __int64 point_count[n] = {0}, points_count = 0;
for(j = 0; j < h; j++)
for (i = 0; i < w; i++)
if (Image1->Canvas->Pixels[i][j] == clBlack)
{
points_count++;
for (k = 0; k < n; k++)
if (PtInRegion(region[k], l + border + i, t + topborder + j))
point_count[k]++;
}
unsigned __int64 sum = 0;
for (i = 0; i < n; i++)
{
sum += point_count[i];
Memo1->Lines->Add(point_count[i]);
}
As i received an advice from one man, in order to allocate an array using a TEdit to specify the array's count I should use, for example DynamicArray:
#include <sysdyn.h>
DynamicArray<HRGN> region;
...
int n = Edit1-> Text.ToInt();
region.Length = n;
I have made the same changes to point_count array:
Byte k;
DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
unsigned __int64 /*point_count[n] = {0},*/ points_count = 0;
...
The problem is that I received different values if I do it dynamically or statically(n=20).
Statically:
Dynamically:
The problem is that I received different values if I do it dynamically or statically(n=20)
There is no difference whatsoever in accessing elements of a static array vs a dynamic array. Your problem has to be elsewhere.
For instance, your static code is initializing all of the array elements to 0, but your dynamic code is not doing that, so they will have random values before your loop then increments them.
Try this:
DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
for(int i = 0; i < n; ++i) {
point_count[i] = 0;
}
...
Alternatively:
DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
ZeroMemory(&point_count[0], sizeof(unsigned __int64) * n);
...
Also, using the Image1->Canvas->Pixels[][] property is very slow. Consider using the Image1->Picture->Bitmap->ScanLine[] property instead for faster access to the raw pixels.

Error "subscript of pointer to function type 'int *(int)'"

I am writing a code to find a cluster, I am using "cern root" to plot graphs,
the data is saved in ".root" file, but the code is written in c++. The data is saved as a 2D histogram. The logic of the code is once I find a bin with some signal in it, I find the neighbours around it (8 bins), then I tag the bin and increase the cluster size, and then do the same for the neighbour. I started by making a fiction to find the neighbour (the function returns an array with the x coordinate and another finds the y coordinate)
int* neighbour_function_i(int i){
int* neighbour_i = new int[8]; // Pointer to int, initialize to nothing.
neighbour_i[0] = {i-1}, neighbour_i[1] = {i}, neighbour_i[2] = {i+1}, neighbour_i[3] = {i-1}, neighbour_i[4] = {i+1}, neighbour_i[5] = {i-1}, neighbour_i[6] = {i}, neighbour_i[7] = {i+1};
return neighbour_i; //check if this works
}
the code that finds the cluster is as below
int* temp_neighbour_i = NULL;
int* temp_neightbour_j = NULL;
int uncheckedneighbours, total_neighbours;
int clsize = 0;
int temp_i,temp_j;
for(int i = 0; i < NPIXAX; i++){
for(int j = 0; j < NPIXAY; j++){
clsize = 0;
if(h->GetBinContent(i + 1, j + 1) - ped[i][j] > 0 && pedbf[i][j] == 0){//condition to find a cluster
pedbf[i][j] = 1; //Tag arry
clsize = 1;
uncheckedneighbours = 8;
total_neighbours = uncheckedneighbours;
int* neighbour_i = neighbour_function_i[i];//the error is here
int* neighbour_j = neighbour_function_j[j];//the error is here
while(uncheckedneighbours != 0){
for(int n = 0; n < total_neighbours; n++){
temp_i = neighbour_i[n];//Temp int for coordienate
temp_j = neighbour_j[n];//Temp int for coordinate
if(h->GetBinContent(temp_i, temp_j) - ped[temp_i][temp_j] > 0 && pedbf[temp_i][temp_j] == 0){//condition to find a cluster
pedbf[temp_i][temp_j] = 1;
int* new_neighbour_i = neighbour_function_i[temp_i];//the error is here
int* new_neighbour_j = neighbour_function_j[temp_j];//the error is here
uncheckedneighbours += 8;
total_neighbours += 8;
int* temp_neighbour_i = new int[clsize * 8];
int* temp_neighbour_j = new int[clsize * 8];
clsize++;
temp_neighbour_i[n] = neighbour_i[n];//moving data to chnage the size of neighbour/i array
temp_neighbour_j[n] = neighbour_j[n];//moving data to change the size of neighbour_j array
delete[] neighbour_i;//deallocate neighbour
delete[] neighbour_j;//deallocate neighbour
int *neighbour_i = new int[clsize * 8]; //re-allocate the size of neighbour with size = size(clsize *8)
int *neighbour_j = new int[clsize * 8]; //re-allocate the size of neighbour with size = size(clsize *8)
for(int x = 0; x < (clsize - 1) * 8; x++){ //neighbour = temp_neighbour + new_neighbour
neighbour_i[x] = temp_neighbour_i[x];
neighbour_j[x] = temp_neighbour_j[x];
}
for(int x = (clsize - 1)*8; x < clsize * 8; x++){
neighbour_i[x] = new_neighbour_i[x];
neighbour_j[x] = new_neighbour_j[x];
}
delete[]temp_neighbour_i; //dealocate temp and new
delete[]temp_neighbour_j; //dealocate temp and new
delete[]new_neighbour_i; //dealocate temp and new
delete[]new_neighbour_j; //dealocate temp and new
}
uncheckedneighbours--;
}
}
//if(clsize != 0){;//output to file cluseter size, i, j
//}
}
}
}
I am not sure why I am getting this error "subscript of pointer to function type 'int *(int)'"?
Maybe question should be closed as typo, but a function gets called like this:
int* neighbour_i = neighbour_function_i(i);
Not like this:
int* neighbour_i = neighbour_function_i[i];

How i delete float 2D array in c++, with QT

Hi I'm using QT to my work and i can't delete a 2D float array from memory.
I'm working with images, so I need delete arrays for not consume much memory.
I tried this way but doesn't work:
int n = test.cols; // number of colums image.
int m = test.rows; // number of lines image
float soma[test.cols][test.rows]; // create a array 2D for operations...
for(int i = 0 ; i < n + 2 ; ++i)
{
for(int j = 0 ; j < m + 2 ; ++j) delete[] soma[i][j] ;
delete[] soma[i];
}
delete[] soma;
In this particular case the array is either on the STACK or in the DATA section of memory, not the HEAP. Only memory in the HEAP that is allocated with new[] can be deleted by the delete[] operator.
Not this example creates a series of discontinuous rows all over memory.
If you allocated the memory like this.
float ** soma = new float* [test.rows];
// WARNING UNITIALIZED CONTENT
for(int i = 0 ; i < test.rows; ++i) soma[i] = new float[test.cols];
You could then delete the memory like this
for(int i = 0 ; i < test.rows; ++i) delete [] soma[i];
delete [] soma;
However it is often better to allocate a single continuous image (if the dimensions are not too big). You then use a second array to record row offsets as pointers.
// WARNING UNITIALIZED CONTENT
float * buffer = new float [ test.rows * test.cols ]
float ** soma = new float* [ test.rows ];
for(int i = 0 ; i < m; ++i) soma[i] = soma + i * test.cols;
Then delete it like this
delete [] soma;
delete [] buffer;
Or just use std::vector.
Thx, i use std::vector, i'm working with images, static allocation is not a good way, so i use std::vector, for this work, Thank you for your attention, , follow my code now:
vector <float> lines(test.rows);
vector<vector<float> > colums(test.cols,lines);
for(int i=0;i<colums.size(); i++) {
for (int j=0;j<colums[i].size(); j++){
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[0]/(float)(imagem.at<Vec3b>(j,i)[0] + (float)imagem.at<Vec3b>(j,i) [1] + (float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[0] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[1]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[1] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[2]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[2] = aux;
aux = 0;
}
}

C++: Time for filling an array is too long

We are writing a method (myFunc) that writes some data to the array. The array must be a field of the class (MyClass).
Example:
class MyClass {
public:
MyClass(int dimension);
~MyClass();
void myFunc();
protected:
float* _nodes;
};
MyClass::MyClass(int dimension){
_nodes = new float[dimension];
}
void MyClass::myFunc(){
for (int i = 0; i < _dimension; ++i)
_nodes[i] = (i % 2 == 0) ? 0 : 1;
}
The method myFunc is called near 10000 times and it takes near 9-10 seconds (with other methods).
But if we define myFunc as:
void MyClass::myFunc(){
float* test = new float[_dimension];
for (int i = 0; i < _dimension; ++i)
test[i] = (i % 2 == 0) ? 0 : 1;
}
our programm works much faster - it takes near 2-3 seconds (if it's calles near 10000 times).
Thanks in advance!
This may help (in either case)
for (int i = 0; i < _dimension; )
{
test[i++] = 0.0f;
test[i++] = 1.0f;
}
I'm assuming _dimension is even, but easy to fix if it is not.
If you want to speed up Debug-mode, maybe help the compiler, try
void MyClass::myFunc(){
float* const nodes = _nodes;
const int dimension = _dimension;
for (int i = 0; i < dimension; ++i)
nodes[i] = (i % 2 == 0) ? 0.0f : 1.0f;
}
Of course, in reality you should focus on using Release-mode for everything performance-related.
In your example code, you do not initialise _dimension in the constructor, but use it in MyFunc. So you might be filling millions of entries in the array even though you have only allocated a few thousand entries. In the example that works, you use the same dimension for creating and filling the array so you are probably initialising it correctly in that case..
Just make sure that _dimension is properly initialised.
This is faster on most machine.
void MyClass::myFunc(){
float* const nodes = _nodes;
const int dimension = _dimension;
if(dimension < 2){
if(dimension < 1)
return;
nodes[0] = 0.0f;
return;
}
nodes[0] = 0.0f;
nodes[1] = 1.0f;
for (int i = 2; ; i <<= 1){
if( (i << 1) < dimension ){
memcpy(nodes + i, nodes, i * sizeof(float));
}else{
memcpy(nodes + i, nodes, (dimension - i) * sizeof(float));
break;
}
}
}
Try this:
memset(test, 0, sizeof(float) * _dimension));
for (int i = 1; i < _dimension; i += 2)
{
test[i] = 1.0f;
}
You can also run this piece once and store the array at static location.
For each consecutive iteration you can address the stored data without any computation.