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

Author Topic: sf::Vector2f limit  (Read 3294 times)

0 Members and 1 Guest are viewing this topic.

sergiones

  • Guest
sf::Vector2f limit
« on: April 13, 2013, 08:18:12 pm »

Hi !

I'm having trouble knowing why my program keeps sending me Segmentation Fault when I set a vector type

sf::Vector2f[number_of_vectors] when number_of_vectors exceeds 1.100.000.

I was thinking about making a massive particle simulation with sf::VertexArray points1( sf::Points, number_of_vectors) and worked fine until I tried to create veleocity vectors with sf::Vectors vel[number_of_vectors].

Can anyone guess why VertexArray manages to exceed easily 300.000.000  but the vector2f not even 1.100.000 ?

Thanks I'm quite newbie in SFML  :P


I'm using SFML 2.0 by the way...

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: sf::Vector2f limit
« Reply #1 on: April 13, 2013, 08:32:52 pm »
There are no limits for an array size, virtually.

You re probably getting an exception of std::bad_alloc because you re allocating a huge array and you dont have enough contiguous memory free. Try again with less programs open, or check if you have enough RAM, or simply allocate smaller arrays.. you can also divide the particles among multiple smaller arrays instead of a big one.. Try and give us some feedback! :D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector2f limit
« Reply #2 on: April 13, 2013, 08:35:37 pm »
This has nothing to do with SFML.

I'm assuming you're not newing that array. Vertex array uses std::vector which uses dynamic memory from heap which is unlimited* and stack space(for things like arrays of static size, local variables, function calls) is very limited(around couple of MBs, system, compiler, settings etc. dependant, might seem like much but 1024^2 ints or floats is 1 MB) and not mandated by standard. Bjarne Stroustrup recommends not assuming anything about how much you're given there and use handles to heap when you need large amounts of space.


*Ram is physically limited, but running out of that is quite hardcore and system might start swapping to fake even more. Also running out of heap space is recoverable from(you can start catching the bad_allocs that get thrown and start freeing), while running out of stack space is not(I think) because your program insta-dies with seg fault or stack corruption(or something similar sounding :P) because no more autos or calls can be made at all.
« Last Edit: April 13, 2013, 08:42:10 pm by FRex »
Back to C++ gamedev with SFML in May 2023

sergiones

  • Guest
Re: sf::Vector2f limit
« Reply #3 on: April 13, 2013, 09:28:58 pm »
Thanks you the replys!

Well I think the RAM is not the guilty this time. I have 8 GB, and in the system monitor the program "only" used 70 MB for 2.000.000 vector points.

Also, I tried changing the vector for a struct. Worked ! ... but only loading. Now when it process the data I get the same Segmentation Fault.

It seems like I going to have to split the vectors in different  chunks ... any ideas ?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector2f limit
« Reply #4 on: April 13, 2013, 09:38:27 pm »
Are you using stack space or not?
Back to C++ gamedev with SFML in May 2023

sergiones

  • Guest
Re: sf::Vector2f limit
« Reply #5 on: April 13, 2013, 09:51:10 pm »
Well I was willing to use Vector2f from SFML which is dynamic but my intention is to keep the same amount of vectors  and only modify their values so I didn't care much.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector2f limit
« Reply #6 on: April 13, 2013, 09:56:13 pm »
You're(probably) running out of stack space, so use std::vector<sf::Vector2f> and see if it helps.
Back to C++ gamedev with SFML in May 2023

sergiones

  • Guest
Re: sf::Vector2f limit
« Reply #7 on: April 13, 2013, 10:13:32 pm »
Sorry it doesn't.

Yells:

Segmentation fault (core dumped)

(program exited with code: 139)

with    std::vector<sf::Vector2f> vel[2000000];

I googled code: 139 and i got

http://stackoverflow.com/questions/15600296/counter-exit-code-139-when-running-but-gdb-make-it-through

but nothing more ... it doesn't make sense, the rest of the program runs smoothly at 40 FPS and this only goddamn  thing crashes all up!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Vector2f limit
« Reply #8 on: April 13, 2013, 10:19:10 pm »
 :o
NO:
std::vector<sf::Vector2f> vel[2000000];
YES:
std::vector<sf::Vector2f> vel(2000000);
//use vel just like an array: vel[whatever].x=0; etc.
Back to C++ gamedev with SFML in May 2023

sergiones

  • Guest
Re: sf::Vector2f limit
« Reply #9 on: April 13, 2013, 10:30:13 pm »
... good,good what a facepalm I've made.

Now works perfectly.

Thanks a lot FRex. Sorry for the stupid conversation (LOL)