Pages: 1 2 3 »
  Print  
Author Topic: C++ short delay when using CIN + file access discussion  (Read 5425 times)
Offline (Unknown gender) Darkstar2
Posted on: July 23, 2014, 08:56:25 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 ?

Code: (C++) [Select]
#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
Offline (Male) edsquare
Reply #1 Posted on: July 23, 2014, 09:49:50 PM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
Don't know why it happens to you, I tried it and I see no delay  ???

Code: [Select]
#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
Offline (Male) Josh @ Dreamland
Reply #2 Posted on: July 23, 2014, 10:00:50 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
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
Offline (Male) edsquare
Reply #3 Posted on: July 23, 2014, 10:02:51 PM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
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
Offline (Male) Josh @ Dreamland
Reply #4 Posted on: July 23, 2014, 10:13:19 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
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
Offline (Male) edsquare
Reply #5 Posted on: July 23, 2014, 10:21:12 PM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
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
Offline (Unknown gender) Darkstar2
Reply #6 Posted on: July 23, 2014, 10:37:04 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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. :D

Logged
Offline (Male) edsquare
Reply #7 Posted on: July 23, 2014, 10:43:19 PM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
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. :D

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  :o
« 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
Offline (Unknown gender) Darkstar2
Reply #8 Posted on: July 23, 2014, 11:30:03 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
I am not going to open another topic,  but I have a slightly O/T question :D

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

Code: (C++) [Select]
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 :P
BTW, I'm assuming that once the process ends the memory allocated along with it is freed, but just want to make sure :D

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 :D
« Last Edit: July 23, 2014, 11:52:13 PM by Darkstar2 » Logged
Offline (Male) edsquare
Reply #9 Posted on: July 23, 2014, 11:50:37 PM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
I am not going to open another topic,  but I have a slightly O/T question :D

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

Code: (C++) [Select]
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 :P

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 :D

Memory allocation... No idea how that works... Do you eat it with a fork?  :D

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
Offline (Unknown gender) Darkstar2
Reply #10 Posted on: July 23, 2014, 11:56:51 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
I am not going to open another topic,  but I have a slightly O/T question :D

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

Code: (C++) [Select]
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 :P

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 :D

Memory allocation... No idea how that works... Do you eat it with a fork?  :D

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 :D Memory allocation is not that hard, basically they teach you that in kindergarten nowadays :D  so you mashed my code, I forgive you :P BTW that would not compile :P

Tomorrow I will try the CIN on an XP machine. :D

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
Offline (Male) edsquare
Reply #11 Posted on: July 24, 2014, 12:04:52 AM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
Quote
and eds don't worry about it, I was wondering why my code was modified was afraid maybe I switched to a parallel universe :D

Memory allocation... No idea how that works... Do you eat it with a fork?  :D

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 :D Memory allocation is not that hard, basically they teach you that in kindergarten nowadays :D  so you mashed my code, I forgive you :P BTW that would not compile :P

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.  :o

Tomorrow I will try the CIN on an XP machine. :D

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
Offline (Unknown gender) Darkstar2
Reply #12 Posted on: July 24, 2014, 12:52:14 AM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 :D

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, :P

Logged
Offline (Male) edsquare
Reply #13 Posted on: July 24, 2014, 01:04:52 AM

Member
Location: The throne of ringworld
Joined: Apr 2014
Posts: 402

View Profile
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 :D

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  ;D (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, :P

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
Offline (Unknown gender) Darkstar2
Reply #14 Posted on: July 24, 2014, 01:17:06 AM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 :P  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!

Quote
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

Code: (C++) [Select]
int x = 100;
int array[x] ; //... etc, it needs a constant. so that won't work....

the new allows this.........

Here is an example,

Code: (C++) [Select]
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 :P

I'm going to be using this in my new dynamic resource engine :D


Logged
Pages: 1 2 3 »
  Print