Thread: system("pause") crashing my app

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    system("pause") crashing my app

    Hello all,

    I recently was doing an assignment for my c class and I ran into a small problem.

    I haven't started much on the rest of the program, and I am currently decoding the command line arguments

    It seems whenever I use system("pause"); at the end of my program (before return 0;) it crash and I have no idea why.

    It has to do with the concat's I'm using. Commenting out those stops the crashes.

    Here is my code (I commented most of it out):
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdbool.h>	// I like my booleans
    #include <string.h>
    #include "lc3.h"
    
    int main(int argc, char* argv[])
    {
    
    	int CLA = 2; 	// override found command line arguments
    			// (setting this as 8 or 0 also crashes the program)
    
    	if((CLA & 2) == 2) //if -b
    	{
    		if(strstr(argv[argc-1],".bin")==null)  // if .bin is not in the filename
    		{
    			strcat(argv[argc-1],".bin");  // concat .bin to the filename
    		}
    	}
    	
    	if((CLA & 8) == 8) //if -m
    	{
    		if(strstr(argv[argc-1],".mco")==null)  // if .mco is not in the filename
    		{
    			strcat(argv[argc-1],".mco");  // concat .mco to the filename
    		}
    	}
    	
    	if((CLA & 10) == 0) //if not -b or -m
    	{
    		if(strstr(argv[argc-1],".hex")==null)  // if .hex is not in the filename
    		{
    			strcat(argv[argc-1],".hex");  // concat .hex to the filename
    		}
    	}
    	
    	printf("\n\n&#37;s",argv[argc-1]);
    	
    	printf("\n\n");
    	system("pause");  // if I remove this, my program ends succesfully
    	return 0;
    }
    any idea why its not working?

    Thanks,
    Kairos
    Last edited by KairosDrasis; 12-09-2008 at 05:20 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure it's crashing before then - perhaps because you are writing over the end of the argv[] strings - since there is absolutely no guarantee that argv[] has any space beyond the actual content, you can't use strcat() on argv[].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    ok. That was my problem. When I created a new string with enough space it didn't crash.

    I still find it odd that the only time it crashed was when I used system("pause"); Well C is funny like that I guess...

    Thanks for the help,
    Kairos

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KairosDrasis View Post
    I still find it odd that the only time it crashed was when I used system("pause"); Well C is funny like that I guess...
    Yes, it's funny that way - that's probably the first time you acutally used the memory that you had overwritten, hence it crashes then, not before - without slowing things down dramatically, there is really no way to catch this sort of badness.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    You should get rid of the habbit to use system("pause") for pausing the program's input. You could use _getch (or it's macro without an underline) or if you program in C++ cin.get().
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    7
    Quote Originally Posted by hauzer View Post
    You should get rid of the habbit to use system("pause") for pausing the program's input. You could use _getch (or it's macro without an underline) or if you program in C++ cin.get().
    yeah... but its so easy to put system("pause") XD. I'm programming in C and I only use it when I'm using Dev-C++ because the window disappears in a flash. I think I heard that its not compatible with anything but windows, but just doing homework for class it don't really matter. We normally don't need to put a pause but I do it because I refuse to use vim.

    Thanks again to all.

    How do I mark this topic as answered/completed?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't really need to do that...
    If your question has been answered, you just need to avoid replying in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 05:56 PM
  3. Need help migrating console app to windows app
    By DelphiGuy in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 07:05 PM
  4. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM
  5. How do I make my Linux app standalone?
    By Joda in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 04:53 AM