I notice this has caused confusion for several people, but after reading a couple of posts on here and the cplusplus tutorial my brain is still scrambled.
Suppose I have the following variables in a header file -
int numberOfLinePoints;
D3DXVECTOR3* line; //confused as to what it is
Then in the implementation C++ file I initialize them as follows -
//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints]; //array of pointers?
What does my line variable now represent?
As far as I can tell from reading links on stackoverflow it should represent an array of pointers. I then read the following however...
(1) Pointers for Beginners
...where (A) arrays of pointers, and (B) pointers to arrays, are both discussed. This left me confused once again as they both seem to work similarly.
The fact that I define my pointers in a seperate location to where I allocate (correct?) them seems to be where my confusion stems from. Am I correct that this is an array of pointers to D3DXVECTOR3 objects?
To finish - if variable line holds information about one line segment, how would I create an array of line segments? I currently have the following -
//HEADER FILE
int numberOfLineSegments;
D3DXVECTOR3** lineCollection; //array of pointers - each of which
//points to an array of pointers?
//CPP FILE
numberOfLineSegments = 8; //constructor
for(i = 0; i < numberOfLineSegments; i++) //initialization
{ //and allocation CORRECT?
lineCollection[i] = new D3DXVECTOR*[numPoints]; //of memory for Y/N
} //lineCollection
VOID createLineSegments(startPoint, endPoint) //could return array instead
{
//pseudo to generate one line segment
while != numberOfLinePoints
line[sentinel++] = interpolate(startPoint, endPoint, time_T)
//pseudo to generate array of line segments
while != numberOfLines
lineCollection[sentinel++] = line
}
Any help is much appreciated.
Your first example:
int numberOfLinePoints;
D3DXVECTOR3* line; //confused as to what it is
Declares a simple pointer to a D3DXVECTOR3. A pointer can be initialized in two ways. First:
line = new D3DXVECTOR3;
This creates a single D3DXVECTOR3 and makes line point to that object. Second:
line = new D3DXVECTOR3[numberOfLinePoints];
This creates an array of D3DXVECTOR3s and makes line point to the first element of that array. You can then use pointer arithmetics to access other elements in that array.
If you declare you pointer as double pointer:
D3DXVECTOR3** line;
You simply create another level of indirection.
int numberOfLinePoints;
D3DXVECTOR3* line; //confused as to what it is
//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints]; //array of pointers?
line is an array of D3DXVECTOR3. It would be an array of pointers if D3DVECTOR3 is itself a pointer, however. Since I don't know the C++ D3D headers very well, I'm not sure.
D3DXVECTOR3** lineCollection;
Is an array of pointers, each pointer likely being a pointer to a line (that is, an array of D3DXVECTOR3).
You have two options. Memorywise, the best would be to set each entry in lineCollection to just point to the corresponding line. This is safe if you either know the lines aren't going to change (and aren't going to be freed), or if they do change you want the changes to be reflected immedaitely inside your collection.
The other option would be to create a new array for each entry in lineCollection, and copy the points from each line into this new array.
There is no correct answer, it depends on the functionality you want.
(Attempting to answer the first part of the question as succinctly as possible without introducing other issues.)
C++ (and C) uses pointers to a single item in an array as a handle for the full array. However, some pointers don't point to items in an array! You have to make the distinction between points-to-single-item and points-to-item-in-array yourself.
int length = 8;
D3DXVECTOR3* line = new D3DXVECTOR3[length];
The new[] operator returns a pointer to the first item in the array it allocates, and this assigns that value to line. Notice that because pointers don't make the distinction of single-item vs. item-in-array:
you have to store the length separately
you have to be careful you use correct indices with pointers ("line" above)
you are better off using a "real" container type, such as:
std::deque, std::vector, etc.
std::tr1::array (aka boost::array)
(The last bullet point doesn't mean you never use pointers, you just don't use them when these containers are more appropriate.)
D3DXVECTOR3 line; // Line is a D3DXVECTOR3
D3DXVECTOR3 * line; // Line is EITHER a pointer to D3DXVECTOR3 OR an
// array of D3DXVECTOR3
D3DXVECTOR3 ** line; // Line is an array of pointers to D3DXVECTOR3 OR
// an array of array of D3DXVECTOR3
This is because an array is no specific structure in memory. It is just a bunch of D3DXVECTOR3 in a row. So pointing to the first element, and you get access to all of the others.
So, having
D3DXVECTOR3** lineCollection; // An array of pointers ORĀ an array of array!
new D3DXVECTOR[numPoints]; // A pointer to an array of D3DXVECTOR
lineCollection[i] // A pointer to an array
You initialize it by:
lineCollection[i] = new D3DXVECTOR[numPoints]; // No extra *
Yet: try to use the STL (like std::vector) instead of ugly C/Java style arrays. If you can, avoid declaring on the heap (using 'new'), but rather declaring on the stack:
D3DXVECTOR a, b, c; // a, b, and c ARE D3DXVECTOR, not pointers
std::vector<D3DXVECTOR> lines;
lines.push_back(a);
lines.push_back(b);
lines.push_back(c);
// equivalently: (you can push_back without temporaries)
std::vector<D3DXVECTOR> lines;
lines.push_back(D3DXVECTOR());
lines.push_back(D3DXVECTOR());
lines.push_back(D3DXVECTOR());
This will avoid manual memory management; it's more readable. You might not be able to always use that comfort (the way your code is organized). And if someone says something about performances, for now, don't worry. First get something working without segfaults nor memory leaks.
line = new D3DXVECTOR3[numPoints];
line holds the memory address of the first element of the array of D3DXVECTOR3.
I.e. line is a pointer to the first element of the array.
This article should clarify it.
Just look at this simple example :
case 1:
int *p = new int [N];
Here p is pointer to array of N integers and p stores starting address of the array.
case 2:
int **p = new int *[N]; //p is a pointer to pointers holding integers used for 2D array.
for(int i=0 ; i<N ; i++)
{
p[i] = new int [N]; // each element is pointer to array of integers.
}
It is applicable to all kinds of user defined types.
Related
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.
The problem that I have is to create a specific matrix.
I have to use an array called for example ptr with x pointers. Each pointer in this array should point to a new array (in this case, an int array; each array is a new line in the matrix then).
All x arrays should be created with new; in the end, it should be possible to access the matrix with ptr[a][b] easily.
After a lot of trying and failing, I hope that someone can help me out.
Thank you in advance!
Since this is obviously homework, let me give you a better answer for your sake to go alongside the accepted one.
std::vector<std::vector<int>> matrix(10, std::vector<int>(10));
// ^ ^ ^
// Column count ______| |________________|
// |
// |___ Each column is
// initialized with
// a vector of size 10.
That's a 10x10 matrix. Since we're using vectors, the sizes are dynamic. For statically sized arrays, you can use std::array if you want. Also, here's the reference for std::vector.
If the number of pointers in the array is known, you could simply use a raw array of pointers to int:
int* my_array[10]; // 10 int*
Then you should allocate memory individually for each pointer in the array using usually a for loop:
for(int i=0; i<10; i++){
// each int* in the array will point to an area equivalent to 10 * sizeof(int)
my_array[i] = new int[10];
}
On the other hand, if you don't know the size of the array, then you need a pointer to pointers:
int** ptr_to_ptr = new int*[10];
Note that I am allocating space for 10 int* and not int.
Remember to deallocate the memory allocated above also for the internal pointers, once you don't need that memory anymore.
I'm hitting an odd segmentation fault that is happening somewhere and I was wondering whether it could be due to the way I allocated the matrix array of pointers.
It's declared as such in the .h file:
int **matrix;
But when I pass it, I am using it in this way int *matrix[], in order to
access individual rows with matrix[i] (this made a lot of my tasks simpler).
So, when I am allocating the matrix, should I have done:
matrix = new int * [vertices];
for (int i = 0; i < vertices; i++)
matrix[i] = new int[vertices];
Or for the third line, should I use the -> operator:
matrix[i]-> new int[vertices]; // Or something like this.
And what is the difference between the two?
The first option you suggested is completely fine in this case. But the second one with the -> operator is not even a valid syntax. The first line from the first suggestion actually creates an array of int* of size vertices:
matrix = new int * [vertices];
Each of the elements of this array is initialised with some garbage values. You can make sure it is initialised with zero by using braces like this: new int * [vertices](). But in either case accessing any of the pointers by matrix[i]-> would be meaningless. In fact the arrow operator -> is a dereference operator that is used exclusively with pointers to objects that have members. Hope that helps.
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.
I'll cut to the chase to save you all some boredom of surplus reading:
I've tried to comb the internet in search of tutorials over dynamical arrays of objects/classes where they explain how pointers are implemented here.
This in particular: TheClass **foo[10]; or something like that, I don't understand what two stars/asterisks are good for and how they are used.
And this whole thing in general.
I do know how dynamical arrays are declared,how regular pointers are used,how to make classes,how to make dynamic arrays of classes.
But all this combined got me confused.
So my questions are:
What does this do and how does it work?
Can you recommend a site where you know examples/tutorials of this can be found?
Does this have a specific name rather than "dynamic object arrays with double pointers"
or whatnot?
If no tutorial comes to mind, I'd appreciate it if you could make a very, very brief example.
Like for instance
int *something;
int somethingElse = 10;
something = &somethingElse; /*Now when you change "somethingElse","something"
will also change to the same number*/
A little super-short example and explanation like that would be greatly appreciated. =)
The simplest, more or less usefull, example using pointers to pointers would be a two dimensional array. So for example
//Create a pointer to (an array of) pointers to (an array of) ints
int** array2D;
//Create a array of pointers to (an array of) ints and assign it to the pointer
array2D = new int*[10];
//Assign each of the pointers to a new array of 10 ints
for(size_t i =0; i<10;i++) {
array2D[i] = new int[10];
}
//Now you have an 2 dimensional array of ints that can be access by
array2D[1][3] = 15;
int test = array2D[1][3];
I hope this explains a bit what pointers to pointers are and how they work.
A pointer is, well, a pointer. It points at something. Period. If you understand that much, then you should be able to understand what a pointer to a pointer is. It is just a pointer whose value is the memory address of another variable that is itself a pointer to something else. That's all. Every time you add a * to the mix, it is just another level of pointer indirection. For example:
int i;
int* p_i = &i; // p_i is a pointer to an int and points at i
int** pp_i = &p_i; // pp_i is a pointer to an int* and points at p_i
int*** ppp_i = &pp_i; // ppp_i is a pointer to an int** and points at pp_i
Now apply that concept to TheClass **foo[10]; (which is actually TheClass** foo[10]; from the compiler's perspective). It is declaring an array named foo that contains 10 TheClass** pointer-to-pointer elements. Each TheClass* might be a pointer to a specific TheClass object, or it might be a dynamic array of TheClass elements (no way to know without more context), then each TheClass** is a pointer to a particular TheClass* pointer.
Well i see you aim for the complete answer, i'll give u a brief example on that.
If you define an array of pointers to pointers, like in your "class foo**[10]" example, let's say:
int numX = 100;
int numY = 1000;
Node **some[10];
some[0] = new Node*[numX];
some[0][0] = new Node[numY];
Then what it does mean is:
You have 10 Node** in your 1st level. So you have 10 pointers to type Node**, but they aren't pointing anywhere useful yet.
This is just 10 adjacent memory locations for storing pointers. In this case it is irrelevant what they are pointing to, mainly it is just 10 fields containing space for a pointer.
Then take the first of these 10 "spaces" and assign the address of an array of 100 pointers to type Node*
some[0] = new Node*[numX]; //numX = 100
This is done and evaluated during runtime, so u can use a variable value given by user input or some application logic, to define dimensions of arrays aka memory-fields.
Now you have 1 of 10 pointers pointing to 100 pointers to type Node*, but still pointing to a black hole.
In the last step create 1000 objects of type Node and attach their addresses to the first of your 100 pointers.
some[0][0] = new Node[numY]; //numY = 1000
In the above example this means, only [0][0][0] to [0][0][999] are objects, the 1000 you created with:
This way you can build multi-dimensional arrays with the specified type. What makes the whole thing work, is that you instantiate what you need in the last dimension (3) and create pointers to uniquely index every field created from [0][0][0] to [9][99][999].
some[0][1]; // memory violation not defined
some[0][0]; // good -> points to a *Node
some[0][0][0]; // good -> actually points to Node (data)
some[1][0][0]; // memory violation not defined
As far as i know, most often you use a one-dimensional array and some tiny math, to simulate two-dimensional arrays. Like saying element [x][y] = [x+y*width];
However you want to use your memory, in the end it all boils down to some memory-adresses and their content.
TheClass** foo[10];
This line of code tells the compiler to a make an array called foo of 10 elements of type pointer to (pointer to Theclass ).
In general when you want to figure out a type that involve multiple astrisks, ampersands. Read Left to Right. so we can break out the code above to something like this:
( (Theclass) * ) * foo[10]
^ ^ ^ ^ ^
5 4 3 2 1
#1 an array of 10 elements named #2 foo #3 of type pointer #4 to pointer #5 to Theclass