my program crash?

This is a discussion on my program crash? within the C++ Programming forums, part of the General Programming Boards category; can someone tell me why this program crash, if it does on your computer? Code: #include <windows.h> #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    my program crash?

    can someone tell me why this program crash, if it does on your computer?

    Code:
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    string readtxtvar(string filetxt,string vartxt)
    { 
     // read a strin/var in a txt file par1:"the name of the txt file ek. c:\\lol.txt". par2:the variable that shall be returned
    
    }
    string writetxtvar(char* filetxt,string vartxt,string writetxt)
    {
     // write a strin/var in a txt file par1:"the name of the txt file ek. c:\\lol.txt". par2:the variable that shall be returned writed
     // par3: the string that shall be sended to the var/string
     
      ofstream myfile;
      myfile.open (filetxt);
      myfile << vartxt << "=" << writetxt << ";\n";
      myfile.close();
     
     //return numbersecond;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    main()
    {
    //ShellExecute(NULL, "open", "C:\\VrKiller-system\\villersystem.exe", NULL, NULL, SW_SHOWNORMAL);
    
    writetxtvar("c:\\lol.txt","bomb","hahaha");
    
    getchar();
    getchar();
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Run it through a debugger
    cyberfish@cyberfish-desktop:/tmp$ g++ -g a.cpp
    a.cpp: In function ‘int main()’:
    a.cpp:31: warning: deprecated conversion from string constant to ‘char*’
    cyberfish@cyberfish-desktop:/tmp$ gdb a.out
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law. Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu"...
    (gdb) r
    Starting program: /tmp/a.out
    *** glibc detected *** /tmp/a.out: munmap_chunk(): invalid pointer: 0x00007fe7092dc268 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x7fe70884d938]
    /usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x3d)[0x7fe70908938d]
    /tmp/a.out[0x400e6c]
    /lib/libc.so.6(__libc_start_main+0xe6)[0x7fe7087f2466]
    /tmp/a.out[0x400bc9]
    ======= Memory map: ========
    00400000-00402000 r-xp 00000000 08:02 2130025 /tmp/a.out
    00601000-00602000 r--p 00001000 08:02 2130025 /tmp/a.out
    00602000-00603000 rw-p 00002000 08:02 2130025 /tmp/a.out
    01ef2000-01f13000 rw-p 01ef2000 00:00 0 [heap]
    7fe7087d4000-7fe70893d000 r-xp 00000000 08:02 8773670 /lib/libc-2.8.90.so
    7fe70893d000-7fe708b3c000 ---p 00169000 08:02 8773670 /lib/libc-2.8.90.so
    7fe708b3c000-7fe708b40000 r--p 00168000 08:02 8773670 /lib/libc-2.8.90.so
    7fe708b40000-7fe708b41000 rw-p 0016c000 08:02 8773670 /lib/libc-2.8.90.so
    7fe708b41000-7fe708b46000 rw-p 7fe708b41000 00:00 0
    7fe708b46000-7fe708b5c000 r-xp 00000000 08:02 8773693 /lib/libgcc_s.so.1
    7fe708b5c000-7fe708d5c000 ---p 00016000 08:02 8773693 /lib/libgcc_s.so.1
    7fe708d5c000-7fe708d5d000 r--p 00016000 08:02 8773693 /lib/libgcc_s.so.1
    7fe708d5d000-7fe708d5e000 rw-p 00017000 08:02 8773693 /lib/libgcc_s.so.1
    7fe708d5e000-7fe708de2000 r-xp 00000000 08:02 8773704 /lib/libm-2.8.90.so
    7fe708de2000-7fe708fe1000 ---p 00084000 08:02 8773704 /lib/libm-2.8.90.so
    7fe708fe1000-7fe708fe2000 r--p 00083000 08:02 8773704 /lib/libm-2.8.90.so
    7fe708fe2000-7fe708fe3000 rw-p 00084000 08:02 8773704 /lib/libm-2.8.90.so
    7fe708fe3000-7fe7090d4000 r-xp 00000000 08:02 7630262 /usr/lib/libstdc++.so.6.0.10
    7fe7090d4000-7fe7092d4000 ---p 000f1000 08:02 7630262 /usr/lib/libstdc++.so.6.0.10
    7fe7092d4000-7fe7092db000 r--p 000f1000 08:02 7630262 /usr/lib/libstdc++.so.6.0.10
    7fe7092db000-7fe7092dd000 rw-p 000f8000 08:02 7630262 /usr/lib/libstdc++.so.6.0.10
    7fe7092dd000-7fe7092f0000 rw-p 7fe7092dd000 00:00 0
    7fe7092f0000-7fe70930f000 r-xp 00000000 08:02 8773650 /lib/ld-2.8.90.so
    7fe7094ee000-7fe7094f1000 rw-p 7fe7094ee000 00:00 0
    7fe70950b000-7fe70950e000 rw-p 7fe70950b000 00:00 0
    7fe70950e000-7fe70950f000 r--p 0001e000 08:02 8773650 /lib/ld-2.8.90.so
    7fe70950f000-7fe709510000 rw-p 0001f000 08:02 8773650 /lib/ld-2.8.90.so
    7fff114fb000-7fff11510000 rw-p 7ffffffea000 00:00 0 [stack]
    7fff115ff000-7fff11600000 r-xp 7fff115ff000 00:00 0 [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

    Program received signal SIGABRT, Aborted.
    0x00007fe708806fd5 in raise () from /lib/libc.so.6
    (gdb) bt
    #0 0x00007fe708806fd5 in raise () from /lib/libc.so.6
    #1 0x00007fe708808b43 in abort () from /lib/libc.so.6
    #2 0x00007fe708847fa8 in ?? () from /lib/libc.so.6
    #3 0x00007fe70884d938 in ?? () from /lib/libc.so.6
    #4 0x00007fe70908938d in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string () from /usr/lib/libstdc++.so.6
    #5 0x0000000000400e6c in main () at a.cpp:31
    (gdb)
    (I removed windows.h for it to compile on Linux)

    it crashed on line 31.

    *edit*
    writetxtvar promises it's going to return a string, but doesn't return anything. That might be why?

    The compiler should warn you about this if you have warnings on (and you should).

    For example, from GCC
    Code:
    cyberfish@cyberfish-desktop:/tmp$ g++ -Wall a.cpp
    a.cpp:27: warning: ISO C++ forbids declaration of ‘main’ with no type
    a.cpp: In function ‘int main()’:
    a.cpp:31: warning: deprecated conversion from string constant to ‘char*’
    a.cpp: In function ‘std::string readtxtvar(std::string, std::string)’:
    a.cpp:13: warning: control reaches end of non-void function
    All 3 points are valid concerns. The third one is probably the cause of your crash.
    Last edited by cyberfish; 02-10-2009 at 06:46 PM.

  3. #3
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    What compiler do you use? This is invalid C++ code.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    sorry for the late post, but does this mean i should get a converter that "converts" char* to a string and THEN retur it?

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vrkiller View Post
    sorry for the late post, but does this mean i should get a converter that "converts" char* to a string and THEN retur it?
    An std::string() is a converter, simply create a new string with the char* as a constructor argument and then pass the string to the writetxtvar function.
    What's more important is that you return something from the writetxtvar() function! You can't declare it to return a string and then not do so.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    so?

    What do you say that i can't return a string in any of them "void();"string();" and "int();"?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    QuantumPete is saying you MUST return a string if the prototype's return type is "string".
    Similarly, you MUST return an int if the prototype's return type is "int".
    Not returning anything from such functions is undefined.

    And you still haven't mentioned what out-of-date compiler you use.
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vrkiller View Post
    What do you say that i can't return a string in any of them "void();"string();" and "int();"?
    No, what he's saying is that IF you declare a function that is supposed to return a string, then you SHOULD return a string. If you do not want to return a value at all, then set the return type to void, which indicates that you do not wish to have this function return anything - to me, this makes sense with your current code.

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

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Wink thx

    Thank you, problem solved.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    FWIW, you should also #include the string header:
    Code:
    #include <string>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21