Char array difficulty

This is a discussion on Char array difficulty within the C++ Programming forums, part of the General Programming Boards category; I'm unable to enforce char array bounds for some reason. Take a look at the following code (iostream was giving ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    69

    Char array difficulty

    I'm unable to enforce char array bounds for some reason. Take a look at the following code (iostream was giving me a hard time so I used the C headers)

    Code:
    #include <stdio.h>
    #include <string.h>
    
    class OOBException {};
    
    class Test
    {
        public:
            char *strtest;
    };
    
    int main()
    {
        Test *test = new Test();
        test->strtest = new char[14];
        try
        {
            gets(test->strtest);
            printf("%s", test->strtest);
        }
        catch (OOBException e)
        {
            printf("exception raised\n");
        }
    
        return 0;
    }
    It compiles okay, but I'm trying to crash it with an exception when I enter more than 13 characters. It simply executes the try clause with no problems no matter what. If I'm inputting more than 13 characters, why doesn't it crash since it goes beyond array boundaries?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    And what bizarre magic do you expect to cause gets() to throw an OOBException if it goes out of bounds?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Welcome to the wonderful world of C and undefined behavior that you've now experienced in C++.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    69
    Quote Originally Posted by brewbuck View Post
    And what bizarre magic do you expect to cause gets() to throw an OOBException if it goes out of bounds?
    Hmm... alright then. I take it C++ just writes the extra characters to the memory beyond the array boundaries? This, of course, overwrites anything occupying the memory, but just asking.

    Sorry, I've had to take a Java prereq course and now my mind is practically poisoned by it.
    Last edited by Chris87; 06-07-2009 at 08:25 PM.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Chris87 View Post
    Hmm... alright then. I take it C++ just writes the extra characters to the memory beyond the array boundaries? This, of course, overwrites anything occupying the memory, but just asking.
    Yes.

    Sorry, I've had to take a Java prereq course and now my mind is practically poisoned by it.
    Even in Java, why would you expect some custom exception you've defined to get thrown?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    69
    One thing doesn't make sense though. If it writes to the array boundaries as well as extra characters beyond it, how come printf() echoes the entire input rather than just the array boundary? Shouldn't the extra characters be a memory leak until termination?

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Chris87 View Post
    One thing doesn't make sense though. If it writes to the array boundaries as well as extra characters beyond it, how come printf() echoes the entire input rather than just the array boundary? Shouldn't the extra characters be a memory leak until termination?
    No. In C, a string's length is not directly specified, but rather implied by the presence of a terminating '\0' character. So printf() will continue printing characters until it sees this "end of string" marker, which was placed there by the previous (overflowing) operation.

    You've got to expunge this idea of "boundaries" from your thinking. There are no boundaries. There are only places where if you touch it, it explodes. But you don't know this until you touch it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    69
    silly me..

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by Chris87 View Post
    silly me..
    Well, I won't lie about it -- C and C++ are an awful lot more "unsafe" than Java in this regard.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    69
    Quote Originally Posted by brewbuck View Post
    Well, I won't lie about it -- C and C++ are an awful lot more "unsafe" than Java in this regard.
    Well, I know that. I'm aware of how disasterous C is, but I'm also aware of how powerful it is as well. Java is cooshy-cooshy and isn't suitable for anything but web applications and small programs. If you've tried the game Runescape, you'll see why...

    I got frustrated with Java even, because I find pointers a necessity in my work, as well as bitwise operations. Plus C headers provide a means to skim over the public contents of an implementation, for humans and for the compiler. Java packs the declaration and definition together and it gets very annoying to read. Not to mention I don't want everything in classes. Yes, I want to use classes but not all the time.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Blaming a language on one program isn't the best idea. You have to focus on knowing the strengths and weaknesses of the language in question, along with understanding the paradigms that the language writers intended for you to use.

    Why would you want a C-style pointer in Java? Are you sure you haven't gone against the intended paradigm, losing out on the strengths of Java?

    It seems you lack the ability to discipline yourself to write code the way you're supposed to for the given language. You want pointers in Java, so you switch to C++. You can't figure out C++, so you mix a bunch of C code.

    Learn the languages for how they are supposed to be used. Things will be much easier for you.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    69
    Quote Originally Posted by MacGyver View Post
    Blaming a language on one program isn't the best idea. You have to focus on knowing the strengths and weaknesses of the language in question, along with understanding the paradigms that the language writers intended for you to use.

    Why would you want a C-style pointer in Java? Are you sure you haven't gone against the intended paradigm, losing out on the strengths of Java?

    It seems you lack the ability to discipline yourself to write code the way you're supposed to for the given language. You want pointers in Java, so you switch to C++. You can't figure out C++, so you mix a bunch of C code.

    Learn the languages for how they are supposed to be used. Things will be much easier for you.
    Well, I did say Java was good for web/small applications, but as far as I know, it's just that. Though, in my case, I'd feel a bit uncomfortable using it with it's safeguards at the cost of low-level functionality.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM

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