Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 13 Jan 2007
Offline Last Active Today, 04:43 AM

#5214043 null a vector of pointers in a parameter?

Posted by Ameise on 02 March 2015 - 04:17 PM

If an empty container would work, you can now do this:

foo( {} );


Which will send an empty array down.

#5213428 Best way of ensuring semantic type consistency?

Posted by Ameise on 27 February 2015 - 06:49 PM

I use the user-defined literal approach (I refer to them as Concrete Types), but with more changes. Mainly, I have dimensional units - that is, 'time' would have the units be 0 for distance, 0 for mass, 1 for time.


You'd see something like this as the declaration:


template <typename T, uint dimLength, uint dimMass, uint dimTime, ...etc...> class concrete_type...

I also have more significant work involved within the concrete_type class to enforce type safety and make it more difficult to accidentally assign values that don't make sense.


The nice thing about it is that:


time val1 = 1_s;
length val2 = 1_m;

auto val3 = val2 / val1; // decltype(val3) == speed.

float val4 = val2 / val1; // error
time val5 = val2 / vall; // error
float val6 = val3; // error
float val6 = val3.raw(); // OK

#5196172 Default font texts from windows

Posted by Ameise on 03 December 2014 - 08:12 PM

I'm a fan of the Liberation Fonts.

#5168965 Funniest line of code ever ?

Posted by Ameise on 24 July 2014 - 02:10 PM


When I was trying to teach somebody to code they wanted to use 'or' to randomly pick between two numbers
value = 1 or 2
Its understandable how a non programmer would expect this to work, so I didn't think he was dumb for doing it, but I still found it funny.

I remember trying to do the same thing myself when I was learning c++.



You should be able to do something like that with variadic templates, though... make a proper templated class, and a function helper to simplify the syntax, and you should be able to do akin to:

if (multi(foo, bar, 4) == some_var) ...

#5129144 fractal result by accident

Posted by Ameise on 05 February 2014 - 03:24 PM



Clearly an aliasing effect and nothing fractal about it.


If you say so ;\ (yawn)



Why do you bother asking if you're just going to reject the answers you get if they don't match your predetermined presumptions? I concur that it just looks like a Moiré pattern.

#5128463 Why would devs be opposed to the PS4`s x86 architecture?

Posted by Ameise on 03 February 2014 - 11:12 AM

X360's x86 architecture could perform 77 GFLOPS, the PS3 could perform 230 GFLOPS.

Just to nitpick, but the XBox 360 didn't have an x86 processor - it was a PowerPC just like the PS3 but with a different internal architecture. Perhaps you meant that the design of the Xenon CPU is more similar to a PowerPC version of common x86 chips than it is to the distributed design of the Core CPU?

#5080275 Better FPS with high-poly terrain

Posted by Ameise on 24 July 2013 - 05:51 PM

In other words, you implemented a O(log n) solution a la spatial subdivision?

#5079031 Generating Terrain on the GPU vs on the CPU

Posted by Ameise on 19 July 2013 - 04:14 PM

Graphical applications usually are bottlenecked by pixel shader, while vertex shader and cpu are being idle most of the time. People usually move computations from cpu to vertex shader to save cpu/memory resources for other cpu specific tasks like AI, physics.

In your case, you have moved some calculation from cpu which was idle most of the time to vertex shader which was also idle most of the time. The performance of your application was determined by pixel shader which was not changed and overall performance of your application was not changed, too. But you should be able to see difference in loads on cpu and vertex shader using some performance profiling tool like Nvidia PerfHud.


Most modern GPUs have unified shader models, so there are no dedicated vertex or fragment units. They are shared.

#5079021 Uniform Location = -1 even though Uniform is Active

Posted by Ameise on 19 July 2013 - 03:21 PM

I'd point out again that it is likely this line:


float shadowFactor      = (curSunSpaceDepth < sunSpaceDepth) ? 1.0f : 1.0f;

Since both sides of the ternary are 1.0f, the compiler is likely compiling away the conditional, and since those are the only places that those variables are referenced, it is recursively compiling out those two uniforms. Most likely, if he changes one of the 1.0f's to 0.0f, he will stop getting -1.

Not to say he shouldn't correct his code otherwise, but that's probably not why this is happening.

#5079008 Uniform Location = -1 even though Uniform is Active

Posted by Ameise on 19 July 2013 - 02:35 PM

Outside of that, I'd suggest the issue has to do with this line:


float shadowFactor      = (curSunSpaceDepth < sunSpaceDepth) ? 1.0f : 1.0f;

#5050065 This error has me perplexed.

Posted by Ameise on 04 April 2013 - 01:49 PM

Also, see if there's some sort of memory debugger that you can use. One of the first programs I turn to for things like this is Valgrind, which would give you a heads-up once various memory errors occur.


"VS2012 express edition"


Valgrind does not run on Windows.

#4958562 Memory Allocate new

Posted by Ameise on 12 July 2012 - 03:30 PM

Why were you downvoted??? you were spot on in everything you mentioned.

oh and yeah i completely forgot about 2[var] syntax. I never use it since its confusing.

What do you mean overloading new? I'm not sure i've come accross that use of new

Also monkeyboi other things about memory management:

  • When using new, as stated before, it goes to the heap. If you allocate too many items, you will run out of memory unless you deallocate them. When you continually allocate memory but never free it, this is called a Memory Leak
  • To deallocate, you use the delete key word.
    say I put SomeClass* x = new SomeClass(); to deallocate it just use delete x; (keep in mind this calls the destructor for SomeClass and frees the memory that x was pointing to, allowing later allocations to use that memory)
    but if you allocate an array you have to do something else; in the case of char* ca = new char[3]; you will have to use delete [] ca;

Overloading new... I don't recommend what I'm doing here (since it's stupid) but it's a quick example:
[source lang="cpp"]static char sBuffer[512];void * operator new (size_t sz){ return (void *)sBuffer;}[/source]
That won't return the pointer from the heap, but rather will return sBuffer.

In regards to placement new:
[source lang="cpp"]char buffer[512];new (buffer) Object;[/source]

That also will not use the heap, but will construct the object within buffer.

For placement new, do not ever call delete. Call the destructor manually. It will crash/cause a black hole as it is not a valid pointer to data on the heap.

[source lang="cpp"]char buffer[512];new (buffer) Object;((Object *)buffer)->~Object();[/source]

Although technically beyond the scope of mid-level C++, he should also have familiarity with the C allocators (malloc, calloc, realloc, ...) and deallocator (free).

They function relatively the same as new (with a few caveats) except that they do not call constructors/destructors. The pointers may also not be interchanged between malloc and delete, for instance.

alloca, if it's present, will allocate the memory on the stack (by decrementing the stack pointer, generally). I don't usually recommend it as it's not safe - a failure for alloca to allocate results in a stack overflow.

#4958558 Memory Allocate new

Posted by Ameise on 12 July 2012 - 03:08 PM

I'm wondering why I was downvoted without any comments explaining what was wrong with what I wrote Posted Image

I first came accross std::string before i learned about char arrays. This was way back in HS in intro programming. But i feel you are right he needs to understand pointers, allocation, and buffer limitations.

Key thinks that monkeyboi needs to know about C-style strings (aka char arrays):

  • There must be a zerobyte/null byte inside that determines the end of the string. '\0' is the ASCII symbol for it, but 0 is also fine. When using strlen it counts the number of characters up until it find this zerobyte. This is why he had odd answers since the char[] was undefined upon initialization. When you put char[4] tc = "123"; you are really putting '1', '2', '3', '\0', filling it up; this extra step of putting the zerobyte at the end is just a feature of c++ (and C i believe)
  • Even if it has a zerobyte, never access/overwrite an element outside of its bounds (dont ever use tc[-2] or tc[n], or any larger number, only 0->n-1 ).
  • Keep in mind the compiler will not warn you if you go out-of-bounds, since it doesn't really know the size of the array, only in the case of declaration and putting something too big will it ever tell you something (char [4] tc = "1234", was too big since '\0' is a fifth additional byte added on)
  • When you use new, it goes to the heap in the CPU memory, which is a special place for dynamically allocated memory, or run-time(?) allocation.
  • Always remember that char[] is still a pointer, as are all arrays. tc itself is the pointer to the first element of the array, as in the case above *tc would be '1'; *(tc+1) is '2', *(tc+2) is '3' and *(tc+3) is '\0'. When you use tc[0] or tc[2], it is just a "shorter" way of doing *(tc+2).
  • char* and char[] are in many ways equivalent, just char[] is explicity announcing its array property, while char* could just be a pointer to a single char or possibly pointing to the beginning of a char array

Regarding 4, only if he uses the standard non-placement new operator. If it is overloaded, everything's up in the air. Placement new just uses whatever memory you're pointing to.

Regarding 5, 2[tc] is also '3' Posted Image I absolutely hate that syntax, though.

#4958545 Memory Allocate new

Posted by Ameise on 12 July 2012 - 02:32 PM

any time you use the "new" or malloc (C-style) allocation methods, it goes onto the heap, contrast to putting it on the stack (any local variables for example, like int f =3)

So indeed your array IS on the heap, since you used new. What I think Ameise was getting at is that since it is on the heap there is less chance of encountering important data, such as things like returning addresses (the place to go back to after a function call for example) which are stored on the stack (if you overwrite a return address it may very well cause a page fault and crash the program)

Edit: the point is that you do not want to go out of bounds of any array or invalid memory. Even if you overwrite harmless data, you risk the opportunity to contaminate either your own program executing, the operating system, or other programs. This is why std::string is much better alternative to C-style strings (char[]) because you don't directly deal with pointers. Also it has its own size() function, as well a multitude of manipulating functions. All in all std::string is more flexible, useful, and easier.

My other point still stands in that regards, though, in that he shouldn't be using std::string until he understands CStrings. In regards to the standard lib, I don't usually recommend using things until you have an understanding of how they work.

#4958486 Memory Allocate new

Posted by Ameise on 12 July 2012 - 11:41 AM

If you need space for strings, consider using std::string instead.

Knowing and using std::* classes is not a replacement for actually understanding how the language works, and in particular how memory allocation works, particularly with a relatively low-level language such as C++. When he has a good understanding of the fundamentals, a basic understanding of the std::* classes and knowledge that he should use them should come along with it.

To the OP:

The reason that strlen is not returning the size of your array is simply due to the fact that that is not the purpose of strlen.

strlen returns the length of a C String, that is, a null-terminated character array.

You are allocationg 4 bytes of memory. However, that memory is coming out of the heap, and it is likely that there is "valid" memory after it (valid in that it won't crash, but undefined as per C++).

Since it is not initialized, it just has random junk. You are seeing strlen return 16. This is why:


rnd is any random non-zero value.

strlen has no knowledge of your array or its size, and only has knowledge of the pointer and that it must seek the null terminator. The fact that you have consistently gotten 16 is coincidental.

It could very well return this:


In which case, strlen would return 3.

The reason it could CRASH is that it could have allocated your memory at the edge of a page allocation, for instance. When you go outside the buffer, you are now reading unmapped memory, which is a page fault. There are other reasons too, simply put - don't read outside of memory you are aware of.