Thread: can't find what's making main return a non-zero value

  1. #1
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86

    can't find what's making main return a non-zero value

    Hi Folks,

    Can anyone tell me why this console program errors out *after* it has correctly performed both of the show() functions? The program compiles without error or warning, which is why I don't understand what the problem is. I've isolated the problem enough to know it's somewhere in the set() function. This program was my solution to the following textbook exercise:

    // Following is a program skeleton. Complete it by providing the
    // described functions and prototypes. Note that there should be
    // two show() functions, each using default arguments. Use const
    // arguments when appropriate. Note that set() should use new to
    // allocate sufficient space to hold the designated string. The
    // techniques used here are similar to those used in designing
    // and implementing classes. (You might have to alter the header
    // file names and delete the using-directive, depending upon your
    // compiler.)

    And here is the skeleton, plus my code for the three functions (including prototypes):

    Code:
    #include <iostream>
    using namespace std;
    
    #include <cstring>  // for strlen(), strcpy()
    struct stringy
    {
       char * str;            // points to a string
       int ct;                  // length of string (not counting '\0')
    };
    
    /// These are my prototypes for the three functions, two of them overloaded
    void set(stringy & x, char * y);
    void show(const stringy & a, int b = 1);
    void show(const char * c, int d = 1);
    
    int main()
    {
       stringy beany;
    
       char testing[] = "Reality isn't what it used to be.";
    
       set(beany, testing);   // first argument is a reference!
    
       show(beany);            // prints member string once
       show(beany, 2);        // prints member string twice
       testing[0] = 'D';
       testing[1] = 'u';
       show(testing);          // prints testing string once
       show(testing, 3);      // prints testing string thrice
       show("Done!");
    
       return 0;
    }
    
    
    void set(stringy & x, char * y)  // 'x' is reference variable for stringy type "beany"
    {
       x.ct = strlen(y);                   // sets ct member of beany (length of testing string)
    
       char * space = new char;     // allocates space to hold copy of testing
    
       space = x.str;                     // sets str member of beany to point to the new block
    
       strcpy(space, y);                // copies testing to new block
    
       delete space;                     // releases memory allocated for space
    }
    
    void show(const stringy & a, int b)
    {
       for (int i = 0; i < b; i++)
          cout << a.str << "\n";
    }
    
    void show(const char * c, int d)
    {
       for (int i = 0; i < d; i++)
          cout << c << "\n";
    }
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    char * space = new char;     // allocates space to hold copy of testing
    You shouldn't lie to yourself in the comments -- this allocates one char, which is nowhere near long enough to hold a copy of testing. You then throw away your allocated memory by reassigning the value of space, meaning you lose your pointer to the allocated memory, meaning you delete something that isn't yours.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Of course this would all be avoided if you stopped using C code in your C++ program. What is preventing you from using std::string here?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by tabstop View Post
    Code:
    char * space = new char;     // allocates space to hold copy of testing
    You shouldn't lie to yourself in the comments -- this allocates one char, which is nowhere near long enough to hold a copy of testing. You then throw away your allocated memory by reassigning the value of space, meaning you lose your pointer to the allocated memory, meaning you delete something that isn't yours.
    While I'm scrambling to figure out how I screwed this up, could you tell me if the following code would be a better way to allocate the proper space:
    Code:
       char * space = new char[x.ct];
    I'm not sure if creating an array of char is the proper answer, that's why I'm asking.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It isn't the proper answer, use std::string. You are programming for C++ correct?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by AndrewHunter View Post
    Of course this would all be avoided if you stopped using C code in your C++ program. What is preventing you from using std::string here?
    I'm not sure if that's because I haven't gotten to the chapter on strings yet (that's 16, and I'm on 8), or if my textbook is too old (pub. 2002). The skeleton the book provided contained the #include <cstring> line.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I would suggest you get a new book, or look at some tutorials that actually teach C++ and then apply those concepts to correct the problems in your book as you go through it. CBoards C++ Tutorials are a good starting point.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by tabstop View Post
    Code:
    char * space = new char;     // allocates space to hold copy of testing
    You shouldn't lie to yourself in the comments -- this allocates one char, which is nowhere near long enough to hold a copy of testing. You then throw away your allocated memory by reassigning the value of space, meaning you lose your pointer to the allocated memory, meaning you delete something that isn't yours.
    tabstop, I'm not trying to argue with you, I'm just trying to understand: why does beany.str print out the string that originally came from testing, if the allocated space isn't large enough, and also if I've lost the pointer before the string is (or isn't!) copied? If the show() functions for beany would fail, then I'd probably have less problems trying to figure out what's wrong. I get that Andrew is trying to steer me in the right direction by using string functions that are native to C++ instead of C, but as I said in my other reply, I'm not to the string class chapter yet (for that matter, I'm not even up to the initial class chapter yet (10).
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  9. #9
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by AndrewHunter View Post
    I would suggest you get a new book, or look at some tutorials that actually teach C++ and then apply those concepts to correct the problems in your book as you go through it. CBoards C++ Tutorials are a good starting point.
    Thanks for your input.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by spongefreddie View Post
    While I'm scrambling to figure out how I screwed this up, could you tell me if the following code would be a better way to allocate the proper space:
    Code:
       char * space = new char[x.ct];
    I'm not sure if creating an array of char is the proper answer, that's why I'm asking.
    For your current code you would need to use your structure member str, and alloc space for it using new to create a char array size strlen(y)+1. The plus 1 is so you can append the '\0' char at the end.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Quote Originally Posted by AndrewHunter View Post
    For your current code you would need to use your structure member str, and alloc space for it using new to create a char array size strlen(y)+1. The plus 1 is so you can append the '\0' char at the end.
    My modus operandi is to keep re-reading the chapter and testing new ideas with the compiler until I finally get it right. Then I always document the struggle so that I never forget it. I have to hit the hay now, but I will attack this anew tomorrow afternoon.

    Thank you for your assistance. I realize you guys have to deal with a lot of dunderheads, and it probably gets old.
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by spongefreddie View Post
    tabstop, I'm not trying to argue with you, I'm just trying to understand: why does beany.str print out the string that originally came from testing, if the allocated space isn't large enough, and also if I've lost the pointer before the string is (or isn't!) copied? If the show() functions for beany would fail, then I'd probably have less problems trying to figure out what's wrong. I get that Andrew is trying to steer me in the right direction by using string functions that are native to C++ instead of C, but as I said in my other reply, I'm not to the string class chapter yet (for that matter, I'm not even up to the initial class chapter yet (10).
    Because you make your variable that used to point at your extra memory point at your char array inside your object (instead of the other way around) -- and how that works even a little bit is quite simply massive luck, since that char* in your object could be pointing at Tanzania for all anybody knows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main( ) without return
    By adnilsah017 in forum C Programming
    Replies: 4
    Last Post: 06-05-2011, 08:17 PM
  2. return value from main()
    By megastar in forum C Programming
    Replies: 18
    Last Post: 06-28-2007, 07:27 PM
  3. Why does main need to return a value?
    By jimboob in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 10:48 PM
  4. return from main
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-11-2002, 01:29 PM
  5. return from main
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-11-2002, 10:04 AM