Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Vector problem (probably)  (Read 4244 times)

0 Members and 1 Guest are viewing this topic.

sfora14

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
sf::Vector problem (probably)
« on: October 08, 2012, 09:19:42 pm »
Hello all, im not sure if that problem is connected with SFML but it appears when im using one of SFML classes so maybe ill try to ask here :) First some code:

sf::Vector2f *gridpoints = new sf::Vector2f[15];
sf::Vector2f *bfn = new sf::Vector2f[15];
for (i=0; i<16; i++)
{
 bfn[i].x=sf::Randomizer::Random(-1.0f,1.0f);
 bfn[i].y=sf::Randomizer::Random(-1.0f,1.0f);
}

when i execute this code im getting error:
glibc detected *** malloc(): memory corruption

im using ubuntu precise pangolin but under windows program crashes also but without any error message, i cant see anything wrong in above code and im fairly sure that that fragment of code is a reason for application to crash. Any help will be really appreciated :)

Kuba

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector problem (probably)
« Reply #1 on: October 08, 2012, 09:22:40 pm »
for (i=0; i<16; i++)//change 16 to 15 here
Might not be it but last iteration of the loop accesses the element indexed as 15th.. and there are only elements from 0 to 14.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Vector problem (probably)
« Reply #2 on: October 08, 2012, 09:37:30 pm »
Well you don't provide enough information to tell you want the problem actually is, then again it's way easier for you to find out. Just run the application with the debugger and extract the call stack then find out what goes wrong.

As for the code snipped there are a few things you should not do:
  • You're using SFML 1.6, which is quite outdated (2-3 years old) and has some ugly bugs. Use SFML 2!
  • You use a for loop without declaring the variable within the loop, this should only be done if there's really a reason. Also what FRex said; use: for(unsigned int i = 0; i < 15; ++i)
  • Use std::vector instead of pure arrays, it's safer, specially if you don't really know what you're doing...
  • There's no reason to allocate an array with constant size on the heap, don't use the new keyword unless you know what you're doing and you're aware that you'll have to call delete[] later on. With std::vector you wouldn't have that issue at all. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sfora14

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: sf::Vector problem (probably)
« Reply #3 on: October 08, 2012, 09:42:34 pm »
for (i=0; i<16; i++)//change 16 to 15 here
Might not be it but last iteration of the loop accesses the element indexed as 15th.. and there are only elements from 0 to 14.

Thank You very much. that was it, i cant say how stupid i feel now,  but application works now :) at least works somehow :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Vector problem (probably)
« Reply #4 on: October 17, 2012, 02:30:42 pm »
This wouldn't have happened with std::vector instead of dynamic arrays ;)

I totally agree with eXpl0it3r. We just had a discussion where I explained in details why you should use RAII. Even if the examples in that thread refer to smart pointers, the same thoughts apply to STL containers.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector problem (probably)
« Reply #5 on: October 18, 2012, 09:00:32 pm »
You're kind of late nexus. ;D
And it'd happen but assertion in [] would/should be quite clear what happened. But I wish everyone on earth had your c++ attitude and favor c++ over C.
Back to C++ gamedev with SFML in May 2023