Darkstar2
|
|
Posted on: July 23, 2014, 08:56:25 pm |
|
|
Joined: Jan 2014
Posts: 1238
|
This question is for C++ users, I noticed when using cin in my projects there is a noticeable pause of 1 second before the cursor appears.
Example, when making a program that displays a menu followed by a cin, when running the menu would display instantly, but there is a delay before the cursor appears, it's as if the cin takes time to "start". Why is this ?
#include <iostream>
using namespace std;
int x = 0;
int main() { cout << "-Options-" << endl; cout << "1. Pack with encryption" << endl; cout << "2. Pack without encryption" << endl; cin >> x; return 0; }
Yes even on the release compile, same thing. I mean 1 second ? WTH! When I coded BASIC and used INPUT it was instant.
|
|
« Last Edit: July 25, 2014, 01:55:10 pm by Darkstar2 »
|
Logged
|
|
|
|
edsquare
|
|
Reply #1 Posted on: July 23, 2014, 09:49:50 pm |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
Don't know why it happens to you, I tried it and I see no delay #include <stdio.h> #include <string.h> #include <iostream>
using namespace std;
int main()
{
int x = 0;
{ cout << "-Options-" << endl; cout << "1. Pack with encryption" << endl; cout << "2. Pack without encryption" << endl; cin >> x; return 0; } getchar();
return 0;
}
|
|
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Josh @ Dreamland
|
|
Reply #2 Posted on: July 23, 2014, 10:00:50 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
He's on Windows. The console is GOING to be terribly slow.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
edsquare
|
|
Reply #3 Posted on: July 23, 2014, 10:02:51 pm |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
He's on Windows. The console is GOING to be terribly slow.
I compiled it as an exe and executed it under wine, shouldn't that be just as slow? EDIT: Just noticed I placed the variable in a different place, changed it so it would be where Darkstar2 put it but still no delay.
|
|
« Last Edit: July 23, 2014, 10:07:41 pm by edsquare »
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Josh @ Dreamland
|
|
Reply #4 Posted on: July 23, 2014, 10:13:19 pm |
|
|
Prince of all Goldfish
Location: Pittsburgh, PA, USA Joined: Feb 2008
Posts: 2950
|
Write a custom terminal emulator that, upon printing a character, deletes the entire content of the terminal by filling it with spaces, then re-prints everything, including the new character. That might get it to be as slow. Probably not worth the effort, though.
Disclaimer: Not implying that's what Windows does. I can't conceive of why the Windows command prompt is that damn slow. Especially since it takes so many disgusting shortcuts.
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
edsquare
|
|
Reply #5 Posted on: July 23, 2014, 10:21:12 pm |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
Write a custom terminal emulator that, upon printing a character, deletes the entire content of the terminal by filling it with spaces, then re-prints everything, including the new character. That might get it to be as slow. Probably not worth the effort, though.
Disclaimer: Not implying that's what Windows does. I can't conceive of why the Windows command prompt is that damn slow. Especially since it takes so many disgusting shortcuts.
Damn I'm glad I started using Linux before trying to relearn to program.
|
|
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Darkstar2
|
|
Reply #6 Posted on: July 23, 2014, 10:37:04 pm |
|
|
Joined: Jan 2014
Posts: 1238
|
Why did you change my code there ? Why 2 returns ? The example I gave is fine. Could it have to do with the fact I am running Windows 7 64bit ? I've made tons of console apps in my days, in QB, and the input prompt was instant. Shocked that it takes 1 second ! LOL. BTW this was just a question out of curiosity. Majority of my upcoming projects won't use CIN, but will take commands directly from command line. Though it's kinda odd, I also remember working with batch files back in the days and it was instant too.... I have a PC with Windows XP on it I will go try running it there, if CIN prompt is instant (no delay) then it's a windows7 or windows7 64bit thing.
|
|
|
Logged
|
|
|
|
edsquare
|
|
Reply #7 Posted on: July 23, 2014, 10:43:19 pm |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
Why did you change my code there ? Why 2 returns ? The example I gave is fine.
Could it have to do with the fact I am running Windows 7 64bit ?
I've made tons of console apps in my days, in QB, and the input prompt was instant. Shocked that it takes 1 second ! LOL.
BTW this was just a question out of curiosity. Majority of my upcoming projects won't use CIN, but will take commands directly from command line. Though it's kinda odd, I also remember working with batch files back in the days and it was instant too.... I have a PC with Windows XP on it I will go try running it there, if CIN prompt is instant (no delay) then it's a windows7 or windows7 64bit thing.
Didn't changed it on purpose, just copied the output/input part over a small program I already had (a homework of my son), so that's why the two returns, I didn't notice it And since I only copied your output and input and left everything else as it was, that's also why I wait for the return key to be pressed. I also don't remember GWBASIC nor QBASIC being slow on windows, but I don't remember if it was XP or windows98
|
|
« Last Edit: July 23, 2014, 10:45:55 pm by edsquare »
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Darkstar2
|
|
Reply #8 Posted on: July 23, 2014, 11:30:03 pm |
|
|
Joined: Jan 2014
Posts: 1238
|
I am not going to open another topic, but I have a slightly O/T question it concerns dynamic memory allocation, what happens if you use NEW but don't use DELETE and you exit your program. Will the OS automatically free any memory allocated by your CPP, or will the memory not be freed. Let's say for example char* resMem = new char[5000];
What happens if I let the program exit or forcibly exit and don't use delete ? Will 5000 bytes of RAM be lost in space unless I restart windows or use a RAM freeing tool or will the OS automatically free it upon exit ? Again this is a question out of curiosity, as I know better than to not free RAM I allocate BTW, I'm assuming that once the process ends the memory allocated along with it is freed, but just want to make sure 2) (yes it's a 2 part question). What's the command so I find out HOW MUCH RAM was allocated ? In the example above I know that 5000 bytes were allocated as char = 1 byte, right ? for an int it would have been 4 * 5000, etc. so how do I find out, in the example above, how much RAM was allocated. Thanks and eds don't worry about it, I was wondering why my code was modified was afraid maybe I switched to a parallel universe
|
|
« Last Edit: July 23, 2014, 11:52:13 pm by Darkstar2 »
|
Logged
|
|
|
|
edsquare
|
|
Reply #9 Posted on: July 23, 2014, 11:50:37 pm |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
I am not going to open another topic, but I have a slightly O/T question
it concerns dynamic memory allocation,
what happens if you use NEW but don't use DELETE and you exit your program. Will the OS automatically free any memory allocated by your CPP, or will the memory not be freed.
Let's say for example
char* resMem = new char[5000];
What happens if I let the program exit or forcibly exit and don't use delete ? Will 5000 bytes of RAM be lost in space unless I restart windows or use a RAM freeing tool or will the OS automatically free it upon exit ?
Again this is a question out of curiosity, as I know better than to not free RAM I allocate
2) (yes it's a 2 part question). What's the command so I find out HOW MUCH RAM was allocated ? In the example above I know that 5000 bytes were allocated as char = 1 byte, right ? for an int it would have been 4 * 5000, etc. so how do I find out, in the example above, how much RAM was allocated.
Thanks
and eds don't worry about it, I was wondering why my code was modified was afraid maybe I switched to a parallel universe
Memory allocation... No idea how that works... Do you eat it with a fork? No problem Ds2, copy paste was the easiest way and I did'nt check for consistency with your code, and I should have, it is good practice to do so.
|
|
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Darkstar2
|
|
Reply #10 Posted on: July 23, 2014, 11:56:51 pm |
|
|
Joined: Jan 2014
Posts: 1238
|
I am not going to open another topic, but I have a slightly O/T question
it concerns dynamic memory allocation,
what happens if you use NEW but don't use DELETE and you exit your program. Will the OS automatically free any memory allocated by your CPP, or will the memory not be freed.
Let's say for example
char* resMem = new char[5000];
What happens if I let the program exit or forcibly exit and don't use delete ? Will 5000 bytes of RAM be lost in space unless I restart windows or use a RAM freeing tool or will the OS automatically free it upon exit ?
Again this is a question out of curiosity, as I know better than to not free RAM I allocate
2) (yes it's a 2 part question). What's the command so I find out HOW MUCH RAM was allocated ? In the example above I know that 5000 bytes were allocated as char = 1 byte, right ? for an int it would have been 4 * 5000, etc. so how do I find out, in the example above, how much RAM was allocated.
Thanks
and eds don't worry about it, I was wondering why my code was modified was afraid maybe I switched to a parallel universe
Memory allocation... No idea how that works... Do you eat it with a fork?
No problem Ds2, copy paste was the easiest way and I did'nt check for consistency with your code, and I should have, it is good practice to do so.
You can eat it with your hands if you wish Memory allocation is not that hard, basically they teach you that in kindergarten nowadays so you mashed my code, I forgive you BTW that would not compile Tomorrow I will try the CIN on an XP machine. As far as memory I'm assuming memory does get freed by the OS, and that the delete would be avoiding any memory leak/hogs within the program itself / process running, unnecessarily.
|
|
|
Logged
|
|
|
|
edsquare
|
|
Reply #11 Posted on: July 24, 2014, 12:04:52 am |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
and eds don't worry about it, I was wondering why my code was modified was afraid maybe I switched to a parallel universe
Memory allocation... No idea how that works... Do you eat it with a fork?
No problem Ds2, copy paste was the easiest way and I did'nt check for consistency with your code, and I should have, it is good practice to do so.
You can eat it with your hands if you wish Memory allocation is not that hard, basically they teach you that in kindergarten nowadays so you mashed my code, I forgive you BTW that would not compile BTW you mean my code would not compile? but it did compile without a hitch! and it ran without a hitch! It didn't on your end? Then windows is a worse pile of crap than even I thought it was. Tomorrow I will try the CIN on an XP machine.
As far as memory I'm assuming memory does get freed by the OS, and that the delete would be avoiding any memory leak/hogs within the program itself / process running, unnecessarily.
What I've read so far as to memory allocation says you need to free it yourself, except no languages with garbage recollection like Java, Python and C#, not sure if it's so though.
|
|
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Darkstar2
|
|
Reply #12 Posted on: July 24, 2014, 12:52:14 am |
|
|
Joined: Jan 2014
Posts: 1238
|
did not try compiling it, but assuming it won't compile because of the 2 returns, but again I can be wrong on that one.....Been a windows user forever and never had any problem. Done film, video, audio visual production, mutlimedia both hobby and professionally and never had issue. but again this is not a linux vs. windows debate really let's leave that for another topic As to what your read, yes you have to free memory yourself to avoid memory leak within your app, memory not freed could be potential memory for other process/applications running, and you avoid the risk of stability/crashing or running out of memory, that is what they probably mean by that......as some would for example allocate new memory in a function but exit function without freeing it (creating a leak) etc. I'll be using memory allocation (more likely dynamic allocation) for some of the stuff I have planned with my resource packer / reader and some of the stuff I will add to ENIGMA soon, hopefully if I have some time,
|
|
|
Logged
|
|
|
|
edsquare
|
|
Reply #13 Posted on: July 24, 2014, 01:04:52 am |
|
|
Location: The throne of ringworld Joined: Apr 2014
Posts: 402
|
did not try compiling it, but assuming it won't compile because of the 2 returns, but again I can be wrong on that one.....Been a windows user forever and never had any problem. Done film, video, audio visual production, mutlimedia both hobby and professionally and never had issue. but again this is not a linux vs. windows debate really let's leave that for another topic Not being an expert on C++ and since Lazarus helps you a lot with memory management, not sure of what I'm about to say but here it goes: You can have as many returns as you like, your functions need to return a value and your procedures return an int to check if they worked correctly (at least on pascal they do), but even on C++ you can have more than one return if you need it, to check that something did x or y you can then pass the integer for error management, I think. I don't know how to explain it, hope you can understand it, because I managed to not understand what I just wrote. About the Windows vs Linux, just name the hour the place and I get to choose the weapons (Beer and steaks okey with you?) As to what your read, yes you have to free memory yourself to avoid memory leak within your app, memory not freed could be potential memory for other process/applications running, and you avoid the risk of stability/crashing or running out of memory, that is what they probably mean by that......as some would for example allocate new memory in a function but exit function without freeing it (creating a leak) etc.
I'll be using memory allocation (more likely dynamic allocation) for some of the stuff I have planned with my resource packer / reader and some of the stuff I will add to ENIGMA soon, hopefully if I have some time,
Dynamic allocation on C++? Is that possible?
|
|
« Last Edit: July 24, 2014, 01:06:39 am by edsquare »
|
Logged
|
A child of five would understand this. Send someone to fetch a child of five. Groucho Marx
|
|
|
Darkstar2
|
|
Reply #14 Posted on: July 24, 2014, 01:17:06 am |
|
|
Joined: Jan 2014
Posts: 1238
|
worked correctly (at least on pascal they do), but even on C++ you can have more than one return if you need it, to check that something did x or y you can then pass the integer for error management, I think.
Gotcha. and BTW I learned Pascal in school But I have not used Pascal a lot actually, so I forgot lots, I still remember readln and writeln lol. Right now i'm sticking to C++, EDL, GML for a while should keep me busy and out of trouble lol! Dynamic allocation on C++? Is that possible?
Let's say you want to declare an array but you don't know in advance the size, you can't do this int x = 100; int array[x] ; //... etc, it needs a constant. so that won't work....
the new allows this......... Here is an example, int i = 200; char* p; p = new int[i];
Another example is you want to read a file and store it in memory, you would get the file size first, store it in a variable and then declare the pointer and allocate RAM based on the file size (dynamic allocation). There are many uses for that, such as creating data base reading into memory, reading files, using arrays of variable or unknown sizes, etc. Hope I have this right I'm going to be using this in my new dynamic resource engine
|
|
|
Logged
|
|
|
|
|