11

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • Constructor call or value initialization when it comes to built-in types. – Rapptz Aug 22 '14 at 13:55
  • Value initialisation: http://codepad.org/KDPyKSMG – Dave Aug 22 '14 at 13:57
  • It's valid because why shouldn't it be? `new` isn't only for classes. – Lightness Races in Orbit Aug 22 '14 at 15:08
  • 4
    That's what you get for using `new[]`... :) – Christian Hackl Aug 22 '14 at 17:52
  • @ChristianHackl What's the alternative? Malloc? – FreelanceConsultant Aug 22 '14 at 19:33
  • 3
    @user3728501: `std::vector`. – Christian Hackl Aug 23 '14 at 09:45
  • @ChristianHackl often not suitable for embedded applications – FreelanceConsultant Aug 29 '14 at 17:26
  • 3
    @user3728501: If `std::vector` is not suitable for whatever environment, then `new[]` (or `new`) and `malloc` are not suitable there, either. The default allocator of the standard containers uses the heap just like `new` and `malloc` (but likely in a better way than any self-written container mechanism based on manual calls to `new` or `malloc` would). If you need a replacement for static arrays, use `std::array` or `boost::array`. – Christian Hackl Aug 29 '14 at 19:31
  • @ChristianHackl The standard library may not be fully implemented on the system. Dynamic memory may also not be implemented, however it is probably more likely that a method of dynamic allocation is available than std::vector. – FreelanceConsultant Aug 29 '14 at 19:53
  • @user3728501: I thought that such systems were becoming more and more a thing of the past. Anyway, some interesting past SO discussions about this topic: http://stackoverflow.com/questions/2226252/embedded-c-to-use-stl-or-not / http://stackoverflow.com/questions/3853382/stl-in-embedded-enviornment – Christian Hackl Aug 29 '14 at 20:30
  • @ChristianHackl At home they are becoming a thing of the past, as e.g. RasbPi can be more powerful than a laptop was not so long ago. But you're forgetting infrastructure and enterprise applications that use older hardware by necessity for various reasons, also low-power stuff. – Lightness Races in Orbit Sep 12 '18 at 09:53

3 Answers3

8

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.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
8

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.

101010
  • 41,839
  • 11
  • 94
  • 168
  • Why using literal double `1024.0` instead of literal float `1024.0f` (whereas the original is literal int `1024`) ? – Jarod42 Aug 22 '14 at 14:32
  • @Jarod42 In `float(1024)` literal `int` is implicitly convert to `1024.0f`. I'm using `1024.0` to signify the value that is going to be assigned to the allocated `float`, is this confusing? – 101010 Aug 22 '14 at 14:39
  • Not confusing, just strange to introduce an other type which also implicitly convert to `1024.f`. But it is just a detail. – Jarod42 Aug 22 '14 at 15:09
4

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.

taocp
  • 23,276
  • 10
  • 49
  • 62