Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new operator with parentheses "(", ")" instead of brackets "[", "]"

Tags:

c++

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?)

like image 798
FreelanceConsultant Avatar asked Aug 22 '14 13:08

FreelanceConsultant


People also ask

What do brackets mean instead of parentheses?

Parentheses are used to enclose numbers, words, phrases, sentences, letters, symbols, and other items while brackets are used to enclose information that is inserted into a quote as well as parenthetical materials within the parentheses.

How to Overload parenthesis operator in c++?

Write a class that holds a string. Overload operator() to return the substring that starts at the index of the first parameter. The length of the substring should be defined by the second parameter. Hint: You can use std::string::substr to get a substring of a std::string.

What do extra parentheses mean?

In general, in programming languages, "extra" parentheses implies that they are not changing the syntactical parsing order or meaning.

How to use parentheses in JavaScript?

Parenthesis are used in an arrow function to return an object. Parenthesis are used to group multiline of codes on JavaScript return statement so to prevent semicolon inserted automatically in the wrong place.


3 Answers

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.

like image 119
taocp Avatar answered Oct 21 '22 22:10

taocp


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.

like image 45
101010 Avatar answered Oct 21 '22 21:10

101010


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.

like image 23
Ferruccio Avatar answered Oct 21 '22 22:10

Ferruccio