C++ conversion from array to pointer - c++

I am bit struggling with kinda easy task. I need to convert array to pointer.
GLfloat serieLine[8][80];
GLfloat *points = &serieLine[0][0];
this gives me just first value, but how can I get all values in the array?

If you want pointer to an array, you can do it like this:
GLfloat (*points)[80] = serieLine;
points will point to the first row of serieLine. If you increment points, it will point to the next row.

Increment the pointer, and it'll point at the next value in the array (so once you've incremented it 8*80 times you'll have seen all of the values)

Related

Moving pointer to vector of vectors to specific position

I have a maze vector of vector of ints and a pointer line to that vector declared as follows. I also have a pointer to line that identifies each element in particular.
std::vector<std::vector<int>> maze;
auto * line = new std::vector<std::vector<int>>(maze); //pointer to maze to hold line position
auto * column = line; // pointer to element in line
maze is a vector of vectors of int that holds numbers. I'm supposed to follow a route starting from 1 and going to the next highest number(+1) until i find an exit or a dead end inside the labyrinth.
It is unclear to me how the pointer line works. My understanding is that it will hold the address of the first element in the first vector of maze. By writing line+1, the pointer will hold the address of the first element of the second vector etc. The compiler lets me write column = &line[1] and i assume column will point to the first element of line[1].
However, i didn't find a way to have line point to a specific vector in maze. I have tried the following and all cause errors:
line = &maze[i]; // make line point to i-th vector in maze
line = &(*maze.begin()); // as suggested in a stackoverflow topic, converting iterator to pointer
*line = maze[i];
The only way i didn't get an error was by writing line->begin = maze.begin(), but i want to move line to any position in maze.
What is the conceptual difference that doesn't allow me to assign line the same way i do column?
Thanks.
It is unclear to me how the pointer line works.
This line
auto * line = new std::vector<std::vector<int>>(maze);
creates a fresh new instance of std::vector<std::vector<int>>, copying the contents of maze into that new instance, and then stores a pointer to the new instance in line. line is not a pointer to maze! That would be this instead:
auto * line = &maze;
By writing line+1, the pointer will hold the address of the first element of the second vector etc.
No. line is not a pointer to an array, or to an element of an array, so you cannot increment it (well, to be precise, it can be considered as an array with one element, and so incrementing the pointer once is fine, but anything beyond that is not).
The only way i didn't get an error was by writing line->begin = maze.begin()
I'll skip the rest, because you are on the wrong track, and I have to admit that I do not completely understand what the second snippet is supposed to do.
However, i didn't find a way to have line point to a specific vector in maze
It is unclear why you want a pointer in the first place. An element of maze you get via
auto x = maze[i][j];
a reference via
auto& r = maze[i][j];
and a pointer via
auto* p = &maze[i][j];
It is not perfectly clear what you are trying to accomplish. What is clear is that your attempt with using line is flawed, because it is not a pointer to maze.
PS: If your intention was to use pointer arithmetic to navigate through the maze, then there is bad news: It won't work. At least not as easy as you hope for. A std::vector<T> stores its elements in contiguous memory. However, that memory is not inside the vector object. Hence, the Ts in a std::vector<std::vector<T>> are stored in contiguous blocks, but they are not contiguous as a whole. The outer vector holds a contiguous block of vector<T>s, and each inner vector<T> holds a separate contiguous block of ints. Collectively, the individual blocks of ints are scattered around memory.

Calculate number of elements in an array based on pointers to the first and last elements

Suppose there exists an array, A, such that its elements are of struct Element and I am told that the struct Element is packed with no padding.
If I am given pointers to the first and last element in A, can I determine the number of elements in A based on the address of the pointers and amount of memory an Element takes up? Or does the structure of an array in memory not work like that.
My thought is if the pointers I'm given are Element* start and Element* finish...
number of elements = (finish - start) / sizeof(Element)
Is this logical thinking?
If you have:
Element* start; // first element
Element* finish; // last element
Then:
numElements = finish - start + 1;
If finish is like an end in STL, you do not have the +1.
Because of pointer arithmetic, you do not have to divide by sizeof(Element)
With regard to considering whether there might be padding at the structure end, as Billy indicated, sizeof already contains that, as will pointer arithmetic. from the C++14 final draft:
N3797/5.3.3/2 [ sizeof ]
When applied to a class, the result is the number of bytes in an
object of that class including any padding required for placing objects of that type in an array.
When you use pointer arithmetic, you can say that the "unit" is the size of one element of the type pointed to.
I.e. if you have Element* start pointing to the 0-th element of an array, start + 1 will point to the 1-st element of that array.
So, when you use finish - start, you already get the number of elements between them, and there is no need to divide by sizeof(Element).

Misunderstanding of C++ array structure

I'm new to C++ and I learned with different tutorials, in one of them I found an example of code:
I have pointed by numbers of lines, that I completely do not understand;
Does this array in array or something like that?
I can understand the second call, but what is the first doing? There is already
"coordinates[blocks[num]]", aren't there? Why need again blocks(i) ?
How do you make this part of the code easier? Did struct with this arrays
don't make easier getting value from arrays?
Thanks in advance!
// Global vars
Struct Rect {
float left;
}
Rectangle *coordinates;
int *blocks;
coordinates = new Rect[25];
blocks = new int[25];
// in method storing values
const int currentBlock = 0; //var in cycle
coordinates[currentBlock].left = column;
blocks[currentBlock] = currentBlock;
//get element method
const Rect& classA::Coords(int num) const
{
return coordinates[blocks[num]]; //(2)
}
//and calling this method like
Coords(blocks[i]); //(3)
Coords(i); //(3)
// (4)
No, not really. Lots of people will think of them as arrays and even describe them as arrays, but they're actually not. coordinates and blocks are both pointers. They just store a single address of a Rect and an int respectively.
However, when you do coordinates = new Rect[25];, for example, you are allocating an array of 25 Rects and setting the pointer coordinates to point at the first element in that array. So, while coordinates itself is a pointer, it's pointing at the first element in an array.
You can index coordinates and blocks like you would an array. For example, coordinates[3] will access the 4th element of the array of Rects you allocated. The reason why this behaves the same as arrays is because it actually is the same. When you have an actual array arr, for example, and you do arr[4], the array first gets converted to a pointer to its first element and then the indexing occurs.
No, this is not an array of arrays. What it is doing is looking up a value in one array (blocks[num]) and using that to index the next array (coordinates[blocks[num]]). So one array is storing indices into the other array.
I'll ignore that this won't compile, but in both cases you are passing an int to the Coords function. The first case looks incorrect, but might not be. It is taking the value at blocks[i], passing that to the function then using that value to index blocks to get another value, then using that other value to index coordinates. In the second case, you are just passing i, which is being used to index blocks to give you a value with which you index coordinates.
That's a broad question that I don't think I can answer without knowing exactly what you want to simplify and without seeing some real valid code.

Access variable Element of Array in llvm

I want to get the Value of an array at an variable index. The Index is computed by the program and not known at parse time. So it is stored in an Value and converted to an Int like this:
Value *IndexV = Index->Codegen();
Value *IntV = Builder.CreateFPToUI( IndexV, Type::getInt32Ty( getGlobalContext() ) );
If I know the index, I can use:
Value *VV = Builder.CreateExtractValue( Builder.CreateLoad( V ), 0 );
This gives me the first element of the array. And works correctly. But how can I use IntV as the index? CreateExtractValue only takes an ArrayRef and there is no way of casting the IntV to an ArrayRef, or am I wrong? How would one do such a thing?
Thanks!
First of all, whenever an ArrayRef is expected, you can always pass just a single item, as there's an implicit conversion between any T and ArrayRef<T>.
Specifically here, though, extractvalue requires constant indices, and cannot accept general values, which is why it wants unsigned values. If you want to access an element in an unknown index in an array, use a getelementptr instruction instead: invoke it on the address of the array with indices 0 and IntV, and you should get a pointer to the array at location IntV.

can not count array elements after passing as argument

I am having issues counting the array elements after passing it into an arguement
void GXDX::LoadMesh(GXVector vertices[], UINT indices[] = NULL)
{
D3D10_BUFFER_DESC bufferDesc;
UINT numVerts = sizeof(vertices)/sizeof(GXVector);
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(GXVector) * numVerts;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufferDesc.MiscFlags = 0;
bufferDesc.CPUAccessFlags = 0;
code..............
}
In the following line above
UINT numVerts = sizeof(vertices)/sizeof(GXVector);
I am trying to count the number of elements in the array varible vertices. But I am not getting an accurate count, if not one at all.
When I did a step through, I notice that I am not able to see all the values in the array, only the first value of the array.
So I am not sure if I am passing the array correctly as an arguement. In another application, I did the same thing and i was able to see all the values in the step through. I pass the array like this.
GXVector vertices[] = { {D3DXVECTOR3(0.5f, 0.5f, 0.5f)},
{D3DXVECTOR3(0.5f, -0.5f, 0.5f)},
{D3DXVECTOR3(-0.5f, -0.5f, 0.5f)},
{D3DXVECTOR3(-0.5f, -0.5f, 0.5f)},
{D3DXVECTOR3(-0.5f, 0.5f, 0.5f)},
{D3DXVECTOR3(0.5f, 0.5f, 0.5f)},
};
UINT indices[] = {0,1,2,3,4};
GXRenderManager::Device()->LoadMesh(vertices, indices);
So in a nutshell, Am I passing the array right as an arguement, what am I doing wrong where I can not get the correct element count of the array.
Yes, you are passing the array correctly; however, in C (and C++), arrays don't contain their size. So you need to pass the size of the array as a separate parameter. Arrays effectively decay into pointers when passed as a parameter into a function, so sizeof(vertices) will give you the size of the pointer type, not the size of the array. You can walk through the array though, either with pointer arithmetic or indexing - but you need to know its exact size, otherwise you can get out of bounds.
This is one main reason why in C++ std::vector is recommended to use instead of raw arrays.
You are passing the array correctly. Array parameters simply do not remember their lengths. Instead, they're passed as though they were mere pointers, so your sizeof expression is getting the size of a pointer, not the size of the entire array that the pointer points at.
If the function needs to know the lengths of the arrays, then you need to pass the lengths as additional parameters.
C arrays (which is what you're passing) don't pass the size (length) along unless you explicitly specify the array length in the function declaration. The typical way to solve this in C is to pass the array length into the function as well as the array.
In C++ much better is to use a std::vector and pass it around. It already knows its own size and the problem vanishes compeltely.
The sizeof(vertices) operation that you are doing is not a runtime operation, it is actually resolved by the compiler. So, as long as the declaration of the array is in scope, you will get a correct result.
That is not so in a function, because you could be passing arrays from several other points in the code, hence the incorrect result. (And hence the confusion for the debugger as well).
Arrays decay into pointers when you pass them like that. What kind of sense did you think that UINT indices[] = NULL made?
You can pass the length around with the array, like you would have in C, or you could use some more intelligent construct like a vector or boost::array.
The C sizeof operator is evaluated at compile-time, not run-time. As written, the compiler does not have enough information to determine the size of the array.
Instead, change your function prototype. Pass in a pointer to the array as well as the length of the array.