C++ smart pointer usage

Started by
21 comments, last by Misantes 9 years, 9 months ago

Thanks for the heads up. My initial implementation resulted in an fps drop, but it was a half hearted attempt and I hadn't fixed all the functions, so it could have been due to a number of reasons. I'll try out both methods, and see which gives better results.

Do you have a profiling tool you recommend? Profiling is embarrassingly a new concept to me. I'm on linux using code-blocks. Would valgrind be appropriate, or is that only for memory leaks?


Unfortunately I am unfamiliar with Linux development tools. On Windows I typically use XPerf and the Windows Performance Toolkit from Microsoft, which is a free download on MSDN.
Advertisement

Valgrind can be used for this (the callgrind and cachegrind tools in particular) (http://c.learncodethehardway.org/book/ex41.html)

However for Linux (and Windows) I really recommend Intel VTune. It is quite pricy but it is trivial to detect hotspots in your code.

If you want a cheap hacky alternative and you are using libraries that Emscripten C++ supports, I have recently found that compiling the game to Javascript and then running it in a browser allows me to profile it using Chrome's Javascript profiler which is strangely effective (It almost does a good job at locating out of bounds stack array access, something that ElectricFence, Otto's malloc hack and even Valgrind have difficulty with).

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

Thanks!

And, this question goes out to anyone who can answer it. I'm having trouble getting some of the newer c++ features to be recognized by Code Blocks. When using "make_unique" for example, I get an error saying it's not a member of the std library.

I've upgraded my gcc to 4.9, checked via gcc -v that's it's the used version, and tried various compiler settings such as -std=c++11, -std=c++1y,-std=c++14, -std=c++0x, etc. but I still can't get it to recognize make_unique (though, unique_ptr works fine). It's making it a little difficult to test some of these suggestions >.<

In theory gcc 4.9 should support it:

http://gcc.gnu.org/gcc-4.9/changes.html

Anyhow, thanks again all smile.png

Beginner here <- please take any opinions with grain of salt

Just tested it with -std=c++11 and it is working.
Did you mark it for the entire project?
Have you #included <memory>?

I get an error saying it's not part of the std library.

It would help if you posted the error itself.

Thanks for the reply smile.png

Yeah, it's marked for the entire project, and I've included <memory>, and again, the unique_ptrs are working. That's weird it's working for you(or it's weird that it's not working for me). Out of curiosity, are you using gcc? From what I understand, support for make_unique was just recently included in the latest release of gcc 4.9 (it took a little effort to even find a way to upgrade). I'm having a little difficulty finding too much information on using it with codeblocks as it's all relatively new.

the exact error is:


"error: 'make_unique' is not a member of 'std'"

I should also reiterate that I'm on linux, which may change how we're both compiling things.

Beginner here <- please take any opinions with grain of salt

I am also on linux and using GCC 4.9.
Are you using the automatic build system?
What about those "using namespace insertItHere;"?
Go through Project -> Build Options...
and check if you do have the flag marked as in here:
7Z1Dj3E.png

You can also check this by looking at your compiler output when you build your source. Something like this:
g++ -Weffc++ -std=c++11 -Wall -pg -g -DDEBUG -ITextureVault -Iinclude -c /home/dejaime/(...)

Yeah, it's the darndest thing. Mine seems to be set up exactly the same and the compiler outputs -std=c++11 as well. I'm making sure I'm setting to the whole program, not just release and debug (they're all ticked to -std=c++11).

I don't use any "using namespace" command, typically, and certainly not right now.

I'm a little stumped. I'm wondering if I'm installed correctly, but the "gcc -v" command in a terminal return gcc 4.9.1. I removed 4.8 through synaptic. Maybe I'll try reinstalling 4.9 and see if I have any luck.

I'm using code::blocks 13.12, if that makes a difference. I could try using one of the nightly builds, but I've been avoiding them since I haven't had any issues up until this point.

I'm definitely still open to suggestions though, if you have any other ideas smile.png

My build log looks like:


g++ -std=c++11 -Wextra -fexceptions -O2 -Wall -Iinclude -c /home/****/Documents/Copy/CodeBlocks/OPENGL/Main.cpp -o obj/Release/Main.o 

In case you're able to parse this better than I, here's the full output of gcc -v:

And, apologies for the terrible screenshot below, just open in another window. It shows the "enable common compiler warnings" currently, but disabling that doesn't seem to fix the issue.

Using built-in specs.

COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.1-1ubuntu2~14.04.3' --with-
+ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --
without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/
usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-
time=yes --enable-gnu-unique-object --disable-libmudflap --disable-vtable-verify --enable-plugin --
with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/
usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/
java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-
directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --
disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --
target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.1 (Ubuntu 4.9.1-1ubuntu2~14.04.3)

Beginner here <- please take any opinions with grain of salt

Well, that is weird. But you said you had 4.8 before and installed 4.9 manually?

You could check to see if codeblocks is really using the 4.9 version of g++.
There are two ways of doing that.
One would be to compile the code manually via console by copying the g++ command codeblocks uses.
A second possibility is to go through Settings-&gt;Compiler-&gt;Toolchain Executables(TAB) and click in the [...] button by the g++ command. That'll show you the folder where c::b is calling g++ from, you then go to that folder and call g++ --version in it...
&nbsp;
...or C++11 does't have make_unique. I have to admit I haven't really used unique_ptr from the std. Will have to look that up for you.

@edit
Yes, C++11 really doesn't have make_unique. I should have known that! haha
http://en.cppreference.com/w/cpp/memory/unique_ptr (lists make_unique as a C++14 feature)
http://isocpp.org/blog/2013/04/n3656-make-unique-revision-1 (paper that got approved into the C++14 draft)

Apparently, my make_unique comes from boost, when I kill the link to boost I get the error. I used to merge boost with my std namespace to mimic C++11 years ago, and this is still biting me to this day every time I reuse that code.

ah, doh!

I knew that it didn't (only after getting the error. But, that's why I upgraded gcc), but the 4.9 version of gcc was supposed to enable it. Though, must be for c++14 I think. Do you know of a way to get code blocks to use c++ 14?

Beginner here <- please take any opinions with grain of salt

It is probably -std=c++14 or -std=c++1y

But you'll have to add it in the [Other Options] tab and unmark the -std=c++11 .

This topic is closed to new replies.

Advertisement