Segmentation fault on vectors.

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

67 Replies - 1955 Views - Last Post: 20 August 2019 - 03:48 PM Rate Topic: -----

#16 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 06 July 2019 - 04:45 AM

I've been thinking about it and could the problem be that the vector copies the object(it's not stored as a pointer) and that somehow creates a new vector inside the object which is empty thus trying to access something inside of it that doesn't exist causes the crash?
Was This Post Helpful? 0
  • +
  • -

#17 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,935
  • Joined: 08-August 08

Re: Segmentation fault on vectors.

Posted 06 July 2019 - 07:02 AM

You mean like if you passed a copy of a vector instead of passing a reference to a vector?
Was This Post Helpful? 0
  • +
  • -

#18 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 06 July 2019 - 08:42 AM

I'm not sure what you mean. Are saying passing a copy OF the vector, or TO the vector?
Was This Post Helpful? 0
  • +
  • -

#19 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,935
  • Joined: 08-August 08

Re: Segmentation fault on vectors.

Posted 06 July 2019 - 01:02 PM

I'm saying that when I see multiple lines like FreeDraw::FreeDraw(sf::Vector2f mousePos, float radius, int r, int b, int g) I get suspicious. Passing a copy of a vector doesn't seem like a good idea, and when you're complaining about seg faults, that's the first thing I'd look at.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: Segmentation fault on vectors.

Posted 06 July 2019 - 02:08 PM

The Vector2f there is not a C++ vector. It is simply a tuple that holds x and y coordinates. Things like those are typically defined as simple structs or classes. They typically do not involve any dynamic memory allocation. They are meant to be as lightweight as possible because most 2D and 3D programs would then have arrays or C++ vectors of these objects.
Was This Post Helpful? 0
  • +
  • -

#21 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 04:41 AM

Not completely sure, but I think this code reflects what is happening in mine. Refuses to output anything to the terminal.
#include <iostream>
#include <vector>

class Test
{
	public:
		std::vector<int> doSmth()
		{
			return numbers;
		}
	private:
		std::vector<int> numbers;
};

int main()
{
	std::vector<Test> tests;

	for(int i = 0; i < 10; ++i)
	{
		Test newtest;

		for(int a = 0; a < 10; ++a)
		{
			newtest.doSmth().push_back(a);
		}

		tests.push_back(newtest);
	}

	for(int i = 0; i < 10; ++i)
	{
		for(int a = 0; a < tests[i].doSmth().size(); ++a)
		{
			std::cout<<tests[i].doSmth()[a]<<std::endl;
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg   User is offline

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 05:07 AM

What exactly do you think this line is doing? newtest.doSmth().push_back(a);

Hint: It's probably not doing what you think, or anything useful for that matter.


Jim
Was This Post Helpful? 0
  • +
  • -

#23 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 05:40 AM

Well I would assume that doSmth gives me the "numbers" vector into which the current value of a is being put in.
Was This Post Helpful? 0
  • +
  • -

#24 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 05:49 AM

Step through the code, or add debugging statements. Notice that the instance of (C++) vector within the class is not the same instance being returned by doSmth().
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg   User is offline

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 05:56 AM

Quote

Well I would assume that doSmth gives me the "numbers" vector into which the current value of a is being put in.

Well you're assuming incorrectly. What you get is a copy of the empty vector of the class, not the actual vector.

Why are you trying to return that vector in the first place?

Why not create a class member function to "add" the information into the vector?

And another member function to "print" the vector, and another member function to return a single element of that vector, etc.

By the way I also recommend you use your debugger to single step through the code to help you visualize the problem.

Jim
Was This Post Helpful? 1
  • +
  • -

#26 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,935
  • Joined: 08-August 08

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 06:03 AM

To illustrate what others are saying:
#include <iostream>
#include <vector>

class Test
{
public:
	std::vector<int> doSmth()
	{
		return numbers;
	}
	void pushBack(int n) {
		numbers.push_back(n);
	}
private:
	std::vector<int> numbers;
};

int main()
{
	std::vector<Test> tests;

	for(int i = 0; i < 10; ++i)
	{
		Test newtest;

		for(int a = 0; a < 10; ++a)
		{
			newtest.pushBack(a);
		}

		tests.push_back(newtest);
	}

	for(int i = 0; i < 10; ++i)
	{
		for(int a = 0; a < tests[i].doSmth().size(); ++a)
		{
			std::cout<<tests[i].doSmth()[a]<<std::endl; // This works because the copy of the numbers vector has the data. It would be better/more efficient to do something like tests[i].doSmth(a) where doSmth returns an element of the vector.
		}
		std::cout << "\n";
	}
	return 0;
}



Was This Post Helpful? 1
  • +
  • -

#27 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 10:33 AM

Wouldn't just returning it by reference do the same thing?
Was This Post Helpful? 0
  • +
  • -

#28 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • Posts: 23,975
  • Joined: 05-May 12

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 11:02 AM

It takes a while for new practioners of object oriented programming to heed the "never share your objects privates". It takes getting burned three or more times before one really learns just not to do it.

I said "three or more", because usually the first time something bad happens, it is a case of losing track of object ownership. "They weren't supposed to free that object under me." or "The memory leak is due to someone else is supposed to free those objects." This is the type of problem that CTphpnwb was referring to when started seeing vectors being passed around.

The second time something bad happens it is usually because somebody managed to get access to an object that was supposed to stay private within a subsystem or component. It is just passed off as "Well, I wasn't expecting my design to evolve that would allow someone else to accidentally mess with my object's internal state. I'll be more careful next time to explicitly warn my co-workers not to pass around my object to untrusted callers. We really need to pass internal objects by reference because of speed requirements". Often that "speed requirement" is actually premature optimization without doing any kind of profiling to prove or disprove that decision.

Giving somebody direct access to your objects internals is like giving Mt. Gox your Bitcoin private keys or the Amazon delivery guy keys to your house. You are trusting that they won't do something bad.

Also take time to read The Paperboy, The Wallet, and The Law of Demeter.
Was This Post Helpful? 0
  • +
  • -

#29 Xrey274   User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 294
  • Joined: 10-December 16

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 11:48 AM

I've taken your advice, but sadly the thing refuses to compile. I don't mean I get an error - I mean that in code blocks when I try to compile it just compiles forever and then code blocks crashes. When I do it manually(through the terminal) it show that the thing has compiled succesfully, but the program executable works like the old code, not the new one.

#include <iostream>
#include <vector>

class Test
{
	public:
		std::vector<int>& doSmth()
		{
			return numbers;
		}

		void addNumber(int number)
		{
			numbers.push_back(number);
		}

		void displayNumber(int index)
		{
			std::cout<<numbers[index]<<std::endl;
		}

		int getSize()
		{
			return numbers.size();
		}
	private:
		std::vector<int> numbers;
};

int main()
{
	std::vector<Test> tests;

	for(int i = 0; i < 10; ++i)
	{
		Test newtest;

		for(int a = 0; a < 5; ++a)
		{
			newtest.addNumber(a);
		}

		tests.push_back(newtest);
	}

	for(int i = 0; i < 10; ++i)
	{
		for(int a = 0; a < tests[i].getSize(); ++a)
		{
			tests[i].displayNumber(a);
		}
	}
}



Was This Post Helpful? 0
  • +
  • -

#30 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,935
  • Joined: 08-August 08

Re: Segmentation fault on vectors.

Posted 07 July 2019 - 01:42 PM

That compiled and ran for me using Xcode.
I saw a couple of possible issues that another compiler might balk at, so try this version:
#include <iostream>
#include <vector>

class Test
{
public:
	void addNumber(int number)
	{
		numbers.push_back(number);
	}

	void displayNumber(int index)
	{
		std::cout<<numbers[index]<<std::endl;
	}

	size_t getSize()
	{
		return numbers.size();
	}
private:
	std::vector<int> numbers;
};

int main()
{
	std::vector<Test> tests;

	for(int i = 0; i < 10; ++i)
	{
		Test newtest;

		for(int a = 0; a < 5; ++a)
		{
			newtest.addNumber(a);
		}

		tests.push_back(newtest);
	}

	for(int i = 0; i < 10; ++i)
	{
		for(int a = 0; a < tests[i].getSize(); ++a)
		{
			tests[i].displayNumber(a);
		}
	}
}


This post has been edited by CTphpnwb: 07 July 2019 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »