Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » [SOLVED] Vector Bug
[SOLVED] Vector Bug [message #52565] Tue, 22 October 2019 13:47 Go to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Hello community,

let's assume this code :
#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	Vector<String> vector1;
	
	String& r = vector1.Create("hello");
	Cout() << r << EOL;
	
	String& r2 = vector1.Create("hello2");
	Cout() << r2 << EOL;
	
	r = "test";//Crashing here
	Cout() << r << EOL; 
}


my code crash at r ="test" statement.
Is it a bug ?!

this kind of thing works :
Array<String> vector1;
	
	String& r = vector1.Create<String>("hello");
	Cout() << r << EOL;
	
	String& r2 = vector1.Create<String>("hello2");
	Cout() << r2 << EOL;
	
	r = "test";
	Cout() << r << EOL;


Thanks in advance
Best regard.

[Updated on: Tue, 22 October 2019 14:52]

Report message to a moderator

Re: Vector Bug [message #52567 is a reply to message #52565] Tue, 22 October 2019 14:06 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Vector, when it reallocates memory, it invalidates references.

https://www.ultimatepp.org/srcdoc$Core$Caveats_en-us.html

I don't think there is a workaround for this and you are not supposed to long term store references gotten from vector, just store them for a short amount of processing, until the next operation on Vector.

I believe if you need to store references the correct way is to use Array.
Re: Vector Bug [message #52571 is a reply to message #52567] Tue, 22 October 2019 14:52 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
cbpporter wrote on Tue, 22 October 2019 14:06
Vector, when it reallocates memory, it invalidates references.

https://www.ultimatepp.org/srcdoc$Core$Caveats_en-us.html

I don't think there is a workaround for this and you are not supposed to long term store references gotten from vector, just store them for a short amount of processing, until the next operation on Vector.

I believe if you need to store references the correct way is to use Array.




Ahhhhhh ! seems logique thanks
Previous Topic: S_type and ValueMap
Next Topic: How to serialize a new data type
Goto Forum:
  


Current Time: Fri Apr 19 15:44:49 CEST 2024

Total time taken to generate the page: 0.02201 seconds