   No enough memory, why?
#include "stdafx.h"#include <string>#include <vector>#include <Windows.h>using namespace std;DWORD WINAPI ThreadProc(void *){    while (true) {   vector<string> StringArry;   for(int i = 0; i < 500; i ++)   {  string str = "Oh my!!!Why can't I write such code?";  str.reserve();  StringArry.push_back(str);   }    }    return 0;}int _tmain(int argc, _TCHAR* argv[]){    for(int i = 0; i < 5; i ++)    {   DWORD dwThreadID = 0;   CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadID);    }    WaitForSingleObject(GetCurrentProcess(), INFINITE);    return 0;}Hi, I wrote this snippet program.I always got a debug break that NO-ENOUGH-MEMORY....I don't know, where is the correct way I should go, any comment will be appreciated.   
  There should be a parameter here:str.reserve(XXX);   ;  I don't think it has anything to do with this problem.However, keep it up!!   ;  [EDIT: Oops, just had another look at your code, ignore my first comment (below) about using up all available memory. Hmm, looks like your code should really work... Could be some thread weirdness I don't quite understand.][IGNORE THIS]I'm not sure if this is meant to be a joke or not...Ok, assuming it's not a joke, the code you've written repeatedly allocates memory (this happens when you call StringArry.push_back(str) in your ThreadProc), and it never stops - until you've used all the memory that's available to your program. Note that the while(true) loop repeats the code it encloses for ever and ever into all eternity. Except that it doesn't in this case, because your program runs out of memory :PNow, if you want help with fixing your code, it might help if you told us what you are actually trying to achieve with it.[/IGNORE THIS][Edited by - Deniz on December 8, 2005 4:16:42 AM];  No, the variables are all within the scope of the while loop, so at the end of each iteration all memory should be deallocated.  I suspect the problem is a non-threadsafe implementation of the SC++L.  Try linking to the multithreaded runtime library.Enigma   ;  EDIT 2: yeah ignore this aswellQuote:Deniz is right. While on first glance it looks like your only allocating memory for 500 strings (in the for loop), that for loop is being run in the while loop over and over again (well until true == true returns false anyway).EDIT: new posts before i finished!Quote:No, the variables are all within the scope of the while loop, so at the end of each iteration all memory should be deallocated. I suspect the problem is a non-threadsafe implementation of the SC++L. Try linking to the multithreaded runtime library.Enigmaare variables that are declared in the scope of a while loop destroyed after each iteration? I thought the were only destroyed when the loop exits. hmmmm.;Quote:Original post by greksterDeniz is right. While on first glance it looks like your only allocating memory for 500 strings (in the for loop), that for lopp is being run in the while loop over and over again (well until true == true returns false anyway).But then, once you glace at it a third time, you will see that what Enigma said is actually correct.EDIT: New edits before I finished ;)Quote:Original post by greksterare variables that are declared in the scope of a while loop destroyed after each iteration? I thought the were only destroyed when the loop exits. hmmmm.Variables go out of scope when the end of the compound statement (the "}") they're declared in is reached.   ;  Yeah ive just been trying it out, oh well guess you can ignore me aswell then!   ;  Hi,Enigma, you are right. I tried to link the Multiple Thread Library, it's ok this time. Oh, I should have noticed this at the begging, thanks for all you guys.   ;  That's not how you're supposed to use reserve.Call it once, before the for loop and pass it the appropriate integral parameter.   