Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 20:31

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Vector ?

Discussion on Vector ? within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
Terrat's Avatar
 
elite*gold: 130
Join Date: Apr 2012
Posts: 1,173
Received Thanks: 670
Vector ?

Hey,
wollte mal etwas Fragen (2 sachen).
Ich verende Vectoren(möchte sie verwenden) in einen TCP Server den ich im Moment schreibe.
Ich habe 2 Threads die die Vectors erstmal bearbeiten.
Code:
Code:
DWORD TCP_Server::Client_Accept_Thread()
{
	sockaddr_in Clientinfo;
	int Clientinfolen = sizeof(Clientinfo);
    ClientSockets.push_back(accept(ListenSocket,(struct sockaddr*)&Clientinfo,&Clientinfolen));		
	Client_Accept_Thread();//Infinit because we want that every time clients can join!
    return 1;
}
DWORD TCP_Server::CheckClientConnection()
{
	for(int CheckClientInt=0;CheckClientInt<ClientSockets.size();CheckClientInt++)
	{

	}
	Sleep(1000);
    return 1;
}
So also ich habe es jetzt so verstanden dass push_back immer das jewalige stück als neues Array item hinten ran hängt. Oder ?
Sprich ich denke jetzt:
push_back(1)
push_back(2)
vector [0]=1
vector [1]=2
Oder bin ich hier auf dem Holzpfad ?

2:
Wie kann ich ein Item eines Vectors so löschen das alle darüber nachrücken (möglichst ohne einen weiteren Vector zu benutzen)
Terrat is offline  
Old 10/17/2014, 22:08   #2

 
snow's Avatar
 
elite*gold: 724
Join Date: Mar 2011
Posts: 10,479
Received Thanks: 3,318
1) korrekt, std::vector<T>:ush_back hängt das Element hinten an
2) std::vector<T>::erase

Deine Client_Accept_Thread-Funktion wird langfristig einen Stack Overflow verursachen.
snow is offline  
Thanks
1 User
Old 10/17/2014, 22:11   #3


 
Jeoni's Avatar
 
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
Hallo,
ertsmal zu deinen Fragen:
1) Ja, das hast du richtig verstanden. push_back hängt das übergebene Element quasi einfach hinten an den vector ran.
2) Nutze dafür einfach die -Function. Ist in der Referenz sicher ausreichend gut beschrieben.

Generell müsstest du aber den Zugriff auf den vector aus verschiedenen Threads synchronisieren, da bspw. push_back das interne Array verschieben kann (wenn der intern angeforderte Speicher zu knapp wird), während der andere Thread noch versucht auf ein ggf. nicht mehr vorhandenes Element zuzugreifen.
Die Funktion TCP_Server::Client_Accept_Thread ist zudem eine endlos-rekursive Funktion. Sie ruft sich also ständig selbst auf und returnt damit auch nichts. Das sorgt dafür, dass durch die ganzen rekursiven Aufrufe irgendwann das Limit für den Stack erreicht ist und dir dein Programm abschmiert. Besser dort eine while(true)-Schleife oder noch besser gleich mit einer Abbruchbedingung, da das Programm sicherlich auch mal sauber beedet werden soll (hoffe ich doch wenigstens!?).
Hoffe, dass ich helfen konnte
Mit freundlichen Grüßen
Jeoni

/Edit: Snow war schneller
Jeoni is offline  
Thanks
1 User
Old 10/17/2014, 22:23   #4
 
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
Quote:
Originally Posted by Dreamsläps View Post
Wie kann ich ein Item eines Vectors so löschen das alle darüber nachrücken (möglichst ohne einen weiteren Vector zu benutzen)
Indem du std::deque oder std::queue verwendest. Dafür sind sie da.

Aber wie Jeoni schon anmerkte, reicht es nicht aus, ein Objekt zu haben, welches dann in unterschiedlichen Threads verwendet wird. Sofern es nicht unbedingt auf die Zeit ankommt, kannst du dann einfach std::mutex und std::lock_guard<std::mutex> verwenden.
Tasiro is offline  
Old 10/17/2014, 22:28   #5
 
Terrat's Avatar
 
elite*gold: 130
Join Date: Apr 2012
Posts: 1,173
Received Thanks: 670
Hmm aber kann man das den auch auf linux nutzen ? Bzw hat jemand nen tut wie ich zu vs 2012 nen linux compiler adde?

Ps warum entsteht eigentlich ein Stackoverflow?
Terrat is offline  
Old 10/17/2014, 23:48   #6
 
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
Quote:
Originally Posted by Dreamsläps View Post
Hmm aber kann man das den auch auf linux nutzen ?
Wenn du dich auf die vorherigen Antworten beziehst, dann lautet die Antwort "ja". Wenn du dich aber auf den Ausschnitt beziehst, den du zeigtest, dann lautet die Antwort "nein", bis du DWORD und Sleep definierst.

Quote:
Originally Posted by Dreamsläps View Post
Ps warum entsteht eigentlich ein Stackoverflow?
Weil sich TCP_Server::Client_Accept_Thread unbedingt selbst aufruft.
Tasiro is offline  
Old 10/17/2014, 23:55   #7
 
Terrat's Avatar
 
elite*gold: 130
Join Date: Apr 2012
Posts: 1,173
Received Thanks: 670
Hm habe es in ne while schleife gepackt programm crasht trotzdem nach ner weile hmm.
Code:
Problemsignatur:
  Problemereignisname:	APPCRASH
  Anwendungsname:	TCP SRV.exe
  Anwendungsversion:	0.0.0.0
  Anwendungszeitstempel:	54418ba8
  Fehlermodulname:	MSVCR110.dll
  Fehlermodulversion:	11.0.50727.1
  Fehlermodulzeitstempel:	5011aa2a
  Ausnahmecode:	40000015
  Ausnahmeoffset:	000a0a94
  Betriebsystemversion:	6.1.7601.2.1.0.768.3
  Gebietsschema-ID:	1031
  Zusatzinformation 1:	1468
  Zusatzinformation 2:	1468fb912db7ee8787f9a8bfd90cdc61
  Zusatzinformation 3:	72a6
  Zusatzinformation 4:	72a6ff73740b7655ce9ec0ee937a1d85
Debugge gerade warte das es wd crasht ...

Soo.
Code:
throw.cpp
+		&ThisException	0x042df234 {ExceptionCode=3765269347 ExceptionFlags=1 ExceptionRecord=0x00000000 {ExceptionCode=??? ...} ...}	EHExceptionRecord *
+		&ThisException.params	0x042df248 {magicNumber=429065504 pExceptionObject=0x042df270 pThrowInfo=0x5df83278 {msvcr110d.dll!__TI2?AVbad_alloc@std@@} {...} }	EHExceptionRecord::EHParameters *
+		ThisException	{ExceptionCode=3765269347 ExceptionFlags=1 ExceptionRecord=0x00000000 {ExceptionCode=??? ExceptionFlags=...} ...}	EHExceptionRecord
		ThisException.NumberParameters	3	unsigned long
Code:
 	KernelBase.dll!76c9b727()	Unknown
 	[Frames below may be incorrect and/or missing, no symbols loaded for KernelBase.dll]	
>	msvcr110d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 152	C++
 	msvcr110d.dll!operator new(unsigned int size) Line 63	C++
 	SAP Pro.exe!std::_Allocate<unsigned int>(unsigned int _Count, unsigned int * __formal) Line 28	C++
 	SAP Pro.exe!std::allocator<unsigned int>::allocate(unsigned int _Count) Line 591	C++
 	SAP Pro.exe!std::_Wrap_alloc<std::allocator<unsigned int> >::allocate(unsigned int _Count) Line 877	C++
 	SAP Pro.exe!std::vector<unsigned int,std::allocator<unsigned int> >::_Reallocate(unsigned int _Count) Line 1501	C++
 	SAP Pro.exe!std::vector<unsigned int,std::allocator<unsigned int> >::_Reserve(unsigned int _Count) Line 1532	C++
 	SAP Pro.exe!std::vector<unsigned int,std::allocator<unsigned int> >::push_back(unsigned int && _Val) Line 851	C++
 	SAP Pro.exe!TCP_Server::Client_Accept_Thread() Line 84	C++
 	SAP Pro.exe!StartClientAcceptThread(void * arg) Line 23	C++
 	kernel32.dll!753733ca()	Unknown
 	ntdll.dll!77689ed2()	Unknown
 	ntdll.dll!77689ea5()	Unknown
Code:
DWORD TCP_Server::Client_Accept_Thread()
{
	while(KeepClientAcceptThreadAllive)
	{
		sockaddr_in Clientinfo;
		int Clientinfolen = sizeof(Clientinfo);
		ClientSockets.push_back(accept(ListenSocket,(struct sockaddr*)&Clientinfo,&Clientinfolen));		
	}
	ClientAcceptThreadIsAlive=false;
    return 1;
}
Terrat is offline  
Reply


Similar Threads Similar Threads
[c++] help with sub-vector
05/22/2014 - C/C++ - 8 Replies
Well, i have a vector that stores objects of my class named 'Tools': vercotr<Tools>myvect that has lets say 5 objects saved in it... But now i want to make a method that makes a subvector from lets say 2nd (start) to 4th (end/finish) element in myvect and returns that newly created subvector. returnSubvect(2,4); that is returning: subvect Can anyone explain me how to do this? -Was strugling with this for some time now, but i cant get it to work.
[REQUEST] VECTOR
09/05/2013 - Facebook - 3 Replies
anyone know any hack gold / coins or stars for the next game facebook? yes? LINK: https://apps.facebook.com/vectorthegame/?fb_sourc e=canvasbookmark_recommended
vector hack
06/18/2013 - Facebook - 1 Replies
vector hack open charles open vector end any misson 'fb.vectorgames.ru/php/vl_api2.php' find it
Vector out of bounce
06/05/2013 - C/C++ - 7 Replies
try { return MapContainer.at(index) ; } catch (std::exception& e) { std::cout << "Element " << index << ": index exceeds vector dimensions." << std::endl; }
Mw2 Vector
05/12/2011 - Call of Duty - 9 Replies
Stimmt das wenn man Schaldämpfer raufschrauft dass sich die Vector nicht mehr so verzieht



All times are GMT +1. The time now is 20:32.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.