Overflow and bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ramsin
    New Member
    • Jul 2011
    • 15

    Overflow and bytes

    Sizeof(a) must be 4 bytes, but why does the overflow occur when I enter at least over 32 characters?!
    Code:
    // HHHe...
    #include <iostream>
    using namespace std;
    
    int main() {
    	char a[4];
    	string str;
    	
    	cin >> str;
    	cin.ignore();
    	
    	int i;
    	for(i=0; i<(str.length()); i++) {
    		a[i] = *(str.c_str()+i);
    	}
    	
    	/* char * p = &(a[7])+i;
    	cout << *p << endl; */
    }
    Thanks for help...
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    yes your char array variable is 4 bytes. it mean it only can hold 3 character and the 4th character will be used null terminating character. you can also use array a cleverly to hold just 4 byts character.

    and why do you think that array will hold 32 character? one byte can hold only one character. One byte mean 8 bit (depending on processor) but its never 8 character
    Last edited by johny10151981; Aug 14 '11, 12:25 PM. Reason: More info

    Comment

    • Ramsin
      New Member
      • Jul 2011
      • 15

      #3
      Code:
      (gdb) run
      Starting program: /home/ramsin/Desktop/a.out 
      AAAABBBBCCCCDDDD
      
      Program exited normally.
      (gdb) r
      Starting program: /home/ramsin/Desktop/a.out 
      AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD 
      
      Program exited normally.
      (gdb) r
      Starting program: /home/ramsin/Desktop/a.out 
      AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDDF
      
      Program received signal SIGSEGV, Segmentation fault.
      0x00272c49 in __libc_start_main () from /lib/libc.so.6

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Ramsin,

        What is happening is that you are blindly writing on the stack, and for whatever reason have 30-something bytes that you can destroy before you reach a critical value.

        The error is because you have corrupted a stack framing pointer or a return address; thus, when the execution leaves your main() function, the CPU tries to access memory that is not in your assigned memory space. Thus the segmentation fault.

        The reason you get 32 characters is system and complier dependant. On some systems you might only get five characters before having troubles.

        So yes, you can easily exceed the bounds of variables using C/C++. In fact, some programs intentionally use the programmer's knowledge of memory lay-out in order to function correctly. However, if you aren't writing high-performance binary communication protocols, you probably don't need to do such things. Just say "No!".

        I hope this helps you a little.

        Cheers,
        Oralloy
        Last edited by Oralloy; Aug 15 '11, 03:50 PM. Reason: Corrected a typo.

        Comment

        • Ramsin
          New Member
          • Jul 2011
          • 15

          #5
          I thought it was also system/compiler dependant, but I was unsure. Anyway, I must learn about such things, because it's necessary to clearly understand how memory works.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            Ramsin,

            Memory schemes depend on what level you have to know it at. There is a huge difference between using new/delete and what the operating system has to do to allocate virtual memory.

            At the risk of confusing you - understand what it is you need to know about memory.

            If you are doing application programming, that's one thing.

            If you are doing binary network communication, that's another.

            If you are writing an operating system, that is yet another.

            Stay current with your coursework and you should be fine.

            When you write you programs, never assume that you can go outside the boundaries of the memory allocated to your program. Once you do, you immediately enter the realm of system and compiler specific behaviour.

            Good Luck!
            Oralloy

            Comment

            Working...