Serving the Quantitative Finance Community

 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

pause function in C++

October 27th, 2002, 1:27 pm

Hi,Does anybody know how I can introduce a 'pause' while running my C++ program? Say the program is running normally, then when it gets to a certain point, I want it to pause until the user presses a key. Presing the key should resume the program execution.Thanks,Sam
Last edited by sam on October 26th, 2002, 10:00 pm, edited 1 time in total.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

pause function in C++

October 27th, 2002, 1:52 pm

if it's a console application, just putchar x;cin >> x;MJ
 
User avatar
Patrik
Posts: 1
Joined: April 15th, 2002, 9:18 am

pause function in C++

October 27th, 2002, 1:54 pm

This very much depend on what type of application you are writing. If it's a command-line application youhave control over the program flow and can just use a "getc(stdin);" to do a blocking read from thekeyboard.If it's a GUI application the program flow is decided upon by the gui-toolkits eventloop and the way towait for a key-press depends on which gui-toolkit you are using (win32, gtk, motif, fox, qt, etc..). I don'tknow any of these particularily well but it should be easy enough to find in the reference docs.
 
User avatar
Patrik
Posts: 1
Joined: April 15th, 2002, 9:18 am

pause function in C++

October 27th, 2002, 1:56 pm

hehe - mj beat me to it while I was writing. And using "cin >> x;" seems more natural, I read C-program..
 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

pause function in C++

October 27th, 2002, 3:03 pm

Thanks for the responses.yeah, I like mj's method! But here's another one for you guys:I have to create a LARGE array, which I am doing using a pointer.double* A;A = new double[n]; where n is a HUGE number, it can be more than 400 million. This is not running properly and I think it may be due to an issue with memory. Take n and multiply by 8 bytes (each double variable in the array needs 8 bytes for storage) and this gives the memory needed for storing the array. My question is, where is the free store? Is it the RAM or the hard drive? For instance, if I need to store 400 million double variables, this works out to 3.2 GB. My computer has 128 MB RAM, but 30 GB hard drive... so is this why the program keeps failing? Because I dont have enough RAM?On a separate note, if I wanted to assign values to the array usingS = ...then does i have to be of integer type? Can i assign the value S[3] by using S[a] with a defined to be a double with value 3? It seems to work ok, but I am wondering if there are any implications which I have not considered.Thanks for your help,Sam
 
User avatar
Patrik
Posts: 1
Joined: April 15th, 2002, 9:18 am

pause function in C++

October 27th, 2002, 3:59 pm

creating a static array of size 400 million seems, well, silly to me. Is this really the natural way to solve the problem?Do you really need all those numbers in memory at the same time? Can't you do it itteratively in chunks of 100 000 ofsomething similair?When allocating memory dynamically with new the limit should first be set by how much memory per process is allowed on your system.I only know unix where you can set things like maximum memory a (user) process can allocate (in order to avoid a runnawayprogram stealing the entire system making it unrespondible). Secondly the limit should be defined by the total amount ofmemory available to the virtual memory system - RAM+swap space. I don't know windows very well, but I do know itdoesn't use a special swap-partition so how big swap space can get I don't know how one can determine - I guess thereis a limit set somewhere.I don't see what you mean in the beginning of the index question. But generally it's ok to index with a floating type,but you really should cast it to a proper integer (by means of *_cast<to_type>(var) family of functions). This will use theinteger part of the float as an index - the implications are hard to tell if you don't know the possible values of the float.
Last edited by Patrik on October 26th, 2002, 10:00 pm, edited 1 time in total.
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

pause function in C++

October 27th, 2002, 4:03 pm

Well the computer can use virtual memory i.e. the hard drive. I must admit I've never tried to assign 3 gb to a single array. As arrays have to be contiguous in memory this could be problematic, however. You can check how much virtual memory you have in windows by looking at the system settings. Can I suggest trying to use thestl container deque instead as it doesn't require contiguous memory?i.e. #include <deque>using namespace std;deque<double> A(n);where n is the size. If you are using NT, I suggest using hte task manager to watch memory usage. Re the other question, the compiler is presumably implicitly converting the double to an int or a long. If its converting to an int, this could be problematic as the number might be too small. i.e. it could be taking the remainder modulo the biggest int. I 'd advise an explicit cast to an unsigned long. MJ
 
User avatar
mj
Posts: 12
Joined: December 20th, 2001, 12:32 pm

pause function in C++

October 27th, 2002, 4:04 pm

ok patrik was faster this time
 
User avatar
Patrik
Posts: 1
Joined: April 15th, 2002, 9:18 am

pause function in C++

October 27th, 2002, 4:13 pm

mj,You and I must be equally bored answering questions this fast I'm more or less forcedto sit in an office repairing other peoples misstakes - been sitting here since 21 pm lastnight and will probably not leave the office until a client visit at lunch time tommorow :/I'm just watching machines doing huge compiles and fixing things when they blow up -that's my reason for answering questions (and for not sending you (mj) comments onthe book - I forgot my copy when going out of town 2 days ago..)
 
User avatar
jens
Posts: 0
Joined: July 14th, 2002, 3:00 am

pause function in C++

October 27th, 2002, 6:05 pm

Sam,keeping so much data in memory seems to be the wrong way to attack whatever problem you have.Depending on your application, I would suggest to use a DB and/or some "divide & conquer" strategies?Cheers,Jens.
 
User avatar
sam
Topic Author
Posts: 2
Joined: December 5th, 2001, 12:04 pm

pause function in C++

October 27th, 2002, 7:03 pm

There is another way to solve the problem, without having to use so much memory but I was just curious about doing it all in one shot...This 'casting' sounds lexactly like the thing I need, but I havent been able to find much about it. how do you index with the integer part of a double variable? Patrik can you elaborate on the command a bit more?Sam
 
User avatar
Patrik
Posts: 1
Joined: April 15th, 2002, 9:18 am

pause function in C++

October 27th, 2002, 7:18 pm

something like:---startdouble a = 3.14;double array[10];array[static_cast<unsigned long>(a)] = 5.67;---end
 
User avatar
jens
Posts: 0
Joined: July 14th, 2002, 3:00 am

pause function in C++

October 27th, 2002, 10:58 pm

If you really want to index with doubles, I'd cast to size_t like this: array[(size_t)a] = 5.67;Using size_t as the type for your index variable is much more preferable!!Jens.
 
User avatar
rags
Posts: 0
Joined: September 24th, 2002, 4:53 am

pause function in C++

October 28th, 2002, 4:48 am

system("PAUSE")
 
User avatar
mholm

pause function in C++

October 28th, 2002, 12:19 pm

I would like to know what crazy type of calculation you are doing which requires 3GB of memory. This type of memory usage will kill windows. You can set windows to use a maximum combination of RAM and virtual memory ;(really swap space but like everything else M$ has to change the name), to be 4GB for a 32-bit processor. Windows does not put a limit on how much memory a process uses like unix/linux does. An old hack which people would do to kill a windows operationg system was to write programs which would just consume memory, spawn threads, fork processes, i.e. eat up critical resoures. This still works in windows.By default windows sets your virtual memory to be something similiar to 500MB in WinNT and 2000. 98 will manage it for you so you shouldn't have to modify anything. You can change this in the system settings. If you only have 128MB like you said then 99% of this will be kept on disk and your program will be very very slow because it will be constantly swapping data in and out of memory. I would recommend using a database or something but if you must store it in memory then go buy some more. Memory is cheap.
Last edited by mholm on October 27th, 2002, 11:00 pm, edited 1 time in total.