I found a crazy mistake in my code.
I had written the following line:
GLfloat* points = new GLfloat(1024);
rather than
GLfloat* points = new GLfloat[1024];
I only just noticed it. My code has compiled and run a few times before I noticed the error. I realize that this is by fluke, but my question is what does the line I originally had do?
I notice that it looks kind of like the creation of a class using a pointer to allocated memory. Does it create a single GLfloat on the heap with the initial value of 1024.0? If this is true, why is it valid syntax? (GLfloat is not a class, is it?)
GLfloat is an OpenGL alias for float (i.e., typedef float GLfloat;). Consequently the code:
GLfloat* points = new GLfloat(1024);
Is equivalent to:
float* points = new float(1024);
Which allocates a floating point number and initializes it to 1024.0 and assigns its address to pointer points.
Yes, you are creating a single GLFloat on the heap initialized to 1024.0. You can initialize primitives with the same syntax as classes. e.g.
int i(10);
Would create an int on the stack initialized to 10.
what does the line I originally had do?
GLfloat* points = new GLfloat(1024);
Let us try to replace GLfloat with int, you will see that if GLFloat is a type similar to int or float, then you will have the following:
int * points = new int(1024);
The above statement means that you are creating a pointer to an int with initial value being 1024. So in your case, it means creating a pointer points to a variable with type being GLfloat and initial value as 1024.
It is equivalent to write the following in a condensed version:
int * points = new int;
*points = 1024;
See here for more explanation.
Related
Im struggling to understand something with this code:
int **p_p_tictactoe;
p_p_tictactoe = new int*[ 3 ];
for ( int i = 0; i < 3; i++ )
{
p_p_tictactoe[ i ] = new int[ 3 ];
}
on the line: p_p_tictactoe[i] = new int [3]; The square brackets dereference p_p_tictactoe once so that we are looking at the value at the address stored by p_p_ticatactoe which is another address as it is a pointer to a pointer. Then this address is assigned to a new int but shouldn't this be assigned to the value at the address rather than the address itself like this: *p_p_tictactoe[i] = new int[3]
p_p_tictactoe is an int **.
Therefore, p_p_tictactoe[i] must be an int *.
Therefore, *p_p_tictactoe[i] must be a single, lonely, int.
Assigning the result of new, which would be an int * here, to an int will not have any useful results.
Another helpful way of looking at this: *p_p_tictactoe[i] is equivalent to p_p_tictactoe[i][0]. That, obviously, is an int, a single cell, and stuffing a pointer into it, won't work.
No.
*p_p_tictactoe[i] (also spelt p_p_tictactoe[i][0]) will be one of the ints in the array that you're creating on that line.
As an aside, this is a very inefficient memory layout. Unless your array is likely to change dimensions, or is jagged, prefer a single block of ints with 2D indexing faked on top.
So I'm currently reading and learning a code from the internet (related to artificial neural network) and I found a part of the code that I don't understand why it works.
double* inputNeurons;
double* hiddenNeurons;
double* outputNeurons;
This is how it was declared. Then in this next code, it was changed and used as an array?
inputNeurons = new( double[in + 1] );
for ( int i=0; i < in; i++ ) inputNeurons[i] = 0;
inputNeurons[in] = -1; // 'in' is declared in the function as an int
So, I want to understand why and how it works. Did it become an array of "doubles"? If so, in what way can I also use this? Can this be used for struct or even class?
Every array can be treated as a pointer. But that does not mean every pointer is an array. Do not mix this up!
Assuming we have an array int test[..], the variable name also represents the address where the array is stored in the memory. So you could write
int * p = test;
At that moment my pointer p "becomes" an array, where "becomes" means 'points to an array'. Your example is similar - the only difference is that the memory is allocated dynamically (heap) and not on the stack (as in my example).
So how are the elements accessed?
Let's say, we want to get the first element (index 0).
We could say
int i = test[0];
or we could say
int i = *p;
Now we want to get the element at index 1:
int i = test[1];
Or - by using pointer arithmetics we could write
int i = *(p + 1);
In C++ (and C) pointers support indexing operator [] which basically adjusts the value of the pointer by the amount specified times the size of the type pointed.
So basically
inputNeurons[5] = 0;
is equivalent to
*(inputNeurons+5) = 0
Now this doesn't give you any guarantee about the fact that inputNeurons points to an address which is correctly allocated to store at least 6 double values but syntactically it is correct and well defined.
You are just adjusting an address to point to the i-th element of a given type starting from the specified address.
This means that
double x;
double* px = &x;
px[5] = 0;
Is syntactically correct although it is wrong, since px+5 is an address which points to memory which has not been reserved correctly to hold that value.
The pointer of type double (double* inputNeurons;) just gets assigned to point to the beginning of an dynamically allocated array (new( double[in + 1])) of the same type. It does not become an array.
You can do this with any other pointer and regular array of the same type. As a matter of fact an array is a pointer to specific address (to its beginning, i.e. to the element with index: 0).
When you increment the pointer by + 1, that one means 1 * type_size (i.e 1 * size_of_double)
In your case: inputNeurons points to the address of the first element of the array. If you dereference it: *inputNeurons, you will get the value stored at that address (if inputNeurons was an array, it would be equivalent to: inputNeurons[0] ). To access the next element just increment by one (*inputNeurons + 1).
I'm never used C++ before, and I used OpenCV background subtraction MOG (Mixture of Gaussian) function in Python and I need to understand how the program works, the OpenCV program line 123 there's command bgmodel.create( 1, frameSize.height*frameSize.width*nmixtures*(2 + 2*nchannels), CV_32F );.. I found the .create function is to allocate new data from here with I assumed the parameter inside was (int ndims, const int* sizes, int type), my question is what the * mean, is it multiplication or pointer?
The asterisk (*) has three use cases:
The multiply operator: 2 * 3
A pointer type declaration: int* p;
Dereferencing a pointer (making it a reference to the value the
pointer is pointing to): *p = 6;
Example:
int i;
int* p = &i; // Sorry, introducing another confusion, taking the address of i
*p = 2 * 3;
// Now *p and i have the value 6
In the question, only multiplications are involved
If we look at your example:
bgmodel.create( 1, frameSize.height*frameSize.width*nmixtures*(2 + 2*nchannels), CV_32F );
We want to know if the * is a pointer dereference or is it multiplication. The form a a pointer dereference is
*pointer_type;
Which doesn't work here as every * is preceded by another variable. If we look at the syntax for multiplication we have:
some_type * some_type;
Now that matches what we have with variables on each side of the *
Now lets say you want to dereference a pointer and multiply it with something. To do that we will wrap the dereferencing in parentheses
some_type * (*pointer_type);
create a dynamic array
int testSize = 10;
float *spec = new float[testSize];
how do you point to spec which points to the dynamic arary?
I tried this but It didn't work.
float **specPtr;
*specPtr = &spec[0];
float **specPtr;
specPtr = &spec;
spec is a pointer,so spePtr is the pointer that point to spec
Your problem is that by putting the dereference operator operator, you're assigning what the pointer specPtr points to, not what it actually is.
Try this instead
float **specPtr;
specPtr = &spec; // Note the omission of the '*'
Recently, after being very tired, I wrote the following code:
GLfloat* array = new GLfloat(x * y * z);
Which, of course should have been:
GLfloat* array = new GLfloat[x * y * z];
(Note the square brackets as opposed to the parenthesis.)
As far as I know, the first form is not valid, but g++ compiled it. Sure, it spat out a completely incomprehensible segfault, but it compiled.
Why?
GLfloat* array = new GLfloat(x * y * z);
Creates a pointer called array to an object of type GLfloat with a value of x * y * z.
Well, the result of new T() is a T*, so new GLFloat would return a GLFloat*. As long as x*y*z is valid to pass to the GLFloat constructor, it's valid code.
It's the same sort of thing as:
int * p = new int(42);
Well, the first expression is a pointer to a GLfloat with value (xyz), which is perfectly legal.