Page 1 of 1

Array buffer overflow (and underflow) ... arrays with size sz, have last valid index sz - 1 Rate Topic: -----

#1 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Posted 23 May 2010 - 02:53 PM

Recently at DIC, a student had a programming problem that possibly could have been resolved quite quickly, if the student had obtained an firm crasp on this single fact ...

that for arrays with size sz

the last VALID index is ... sz - 1


The following code,

first illustrates the correct code

then ... the problematic code ...

with some simple debugging tactics added.

// showCmdLinePars.cpp

// an example of (desired) usage: showCmdLinePars.exe 1 2 3

// this code illustrates a commnon coding problem:  array/buffer OVERflow
// (array/buffer UNDERflow, not illustrated here, also causes a similar problem)

// and ... also illustrates some simple debugging ...


#include <iostream>
using namespace std;

#define showSteps 1

// Note: the 'back-slash' at the end of each line below ... (in this macro)
// indicates to the compiler that the code is continued on the next line
#define showStep \
    cout << "\nThe next value to be printed is argv[" << ctr << "] = "; \
    cout << "\nPress 'Enter' to continue ... "; \
    cin.get()

#define block1 1 // to EXCLUDE compiling block1 below ... change 1 to 0 here
#define block2 0

int main( int argc, char **argv )
{
    // both blocks below ... illustrate a common coding error ... that the
    // programmer is responsible to prevent ... 'array buffer overflow'

    cout << "Firstly ... the good code ... \n\n";
    
    cout << "code that prints out ONLY all the command line par's yields: ";
    for( int ctr = 1; ctr < argc; ++ctr )
    {
#if showSteps
        // include this block while debugging ... to see where/why coding error
        showStep;
#endif
        cout << "'" << argv[ctr] << "' ";
    }
    
    cout << "\n\ncode that prints out reversed ... as above ...       yields: ";
    for( int ctr = argc- 1; ctr > 0; --ctr )
    {
#if showSteps
        // include this block while debugging ... to see where/why coding error
        showStep;
#endif
        cout << "'" << argv[ctr] << "' ";
    }


    cout << "\n\n\nNow ... the defective code ... \n\n";
    
#if block1
    // problematic code that 'hopes' to printout ONLY all the command line par's
    for( int ctr = 1; ctr <= argc; ++ctr ) // Note: should be ... ctr < argc; ...
    {
        // include this block while debugging ... to see where/why coding error
        showStep;
        
        cout << "'" << argv[ctr] << "' ";
    }
#endif

#if block2
    // problematic code that 'hopes' to printout reversed ... as above
    for( int ctr = argc; ctr > 0; --ctr ) // Note: should be int ctr = argc-1;
    {
        // include this block while debugging ... to see where/why coding error
        showStep;
        
        cout << "'" << argv[ctr] << "' ";
    }
#endif

    cout << "\n\nPress 'Enter' to exit ... ";
    cin.get();
    return 0;
}



Is This A Good Question/Topic? 1
  • +

Replies To: Array buffer overflow (and underflow) ...

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Posted 20 September 2011 - 07:53 PM

Some typos corrected ... * *

1. Recently at DIC, a student had a programming problem that possibly could have been resolved quite quickly, if the student had obtained *a* firm crasp on this single fact ...

that for arrays with size sz

the last VALID index is ... sz - 1


The following code,

first illustrates the correct code

then ... the problematic code ...

with some simple debugging tactics added.

// showCmdLinePars.cpp

// an example of (desired) usage: showCmdLinePars.exe 1 2 3

// 2. // this code illustrates a *common* coding problem: array/buffer OVERflow
// (array/buffer UNDERflow, is not illustrated here, but also causes a similar problem)

// some simple debugging also illustrated ...


#include <iostream>
using namespace std;

#define showSteps 1

// Note: the 'back-slash' at the end of each line below ... (in this macro)
// indicates to the compiler that the code is continued on the next line
#define showStep \
    cout << "\nThe next value to be printed is argv[" << ctr << "] = "; \
    cout << "\nPress 'Enter' to continue ... "; \
    cin.get()

#define block1 1 // to EXCLUDE block1 below (at compile time)... change 1 to 0 here
#define block2 0

int main( int argc, char **argv )
{
    // both blocks below ... illustrate a common coding error ... that the
    // programmer is responsible to prevent ... 'array buffer overflow'

    cout << "Firstly ... the good code ... \n\n";
    
    cout << "code that prints out ONLY all the command line par's yields: ";
    for( int ctr = 1; ctr < argc; ++ctr )
    {
#if showSteps
        // include this block while debugging ... to see where/why coding error
        showStep;
#endif
        cout << "'" << argv[ctr] << "' ";
    }
    
    cout << "\n\ncode that prints out reversed ... as above ...       yields: ";
    for( int ctr = argc- 1; ctr > 0; --ctr )
    {
#if showSteps
        // include this block while debugging ... to see where/why coding error
        showStep;
#endif
        cout << "'" << argv[ctr] << "' ";
    }


    cout << "\n\n\nNow ... the defective code ... \n\n";
    
#if block1
    // problematic code that 'hopes' to printout ONLY all the command line par's
    for( int ctr = 1; ctr <= argc; ++ctr ) // Note: should be ... ctr < argc; ...
    {
        // include this block while debugging ... to see where/why coding error
        showStep;
        
        cout << "'" << argv[ctr] << "' ";
    }
#endif

#if block2
    // problematic code that 'hopes' to printout reversed ... as above
    for( int ctr = argc; ctr > 0; --ctr ) // Note: should be int ctr = argc-1;
    {
        // include this block while debugging ... to see where/why coding error
        showStep;
        
        cout << "'" << argv[ctr] << "' ";
    }
#endif

    cout << "\n\nPress 'Enter' to exit ... ";
    cin.get();
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Posted 23 September 2011 - 03:52 PM

Good but I'd also like to stress that in C++ the base index of arrays is 0; that is the first element is indexed at array[0] - many beginners fall into the trap of thinking it is 1.
Was This Post Helpful? 1
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Posted 24 September 2011 - 09:24 AM

View PostRicky65, on 23 September 2011 - 06:52 PM, said:

Good but I'd also like to stress that in C++ the base index of arrays is 0; that is the first element is indexed at array[0] - many beginners fall into the trap of thinking it is 1.


Thanks ... yes, I'd also like to stress that in C++ the base index of arrays is 0; that is the first element is indexed at array[0] - many beginners fall into the trap of thinking it is 1 ...

If anyone would like to see a C++ dynamic array (vector) that handles any integer range ... I did an example a few years ago that I may be able to find and e-mail to you if you like.
Was This Post Helpful? 1
  • +
  • -

#5 flypro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-November 11

Posted 07 November 2011 - 04:15 PM

View PostDavid W, on 24 September 2011 - 04:24 PM, said:

View PostRicky65, on 23 September 2011 - 06:52 PM, said:

Good but I'd also like to stress that in C++ the base index of arrays is 0; that is the first element is indexed at array[0] - many beginners fall into the trap of thinking it is 1.


Thanks ... yes, I'd also like to stress that in C++ the base index of arrays is 0; that is the first element is indexed at array[0] - many beginners fall into the trap of thinking it is 1 ...

If anyone would like to see a C++ dynamic array (vector) that handles any integer range ... I did an example a few years ago that I may be able to find and e-mail to you if you like.




yeah i wouldn't mine seeing it :), ive done a similar thing reading a data wav file into a vector... problem is it don't work properly :P lol :), good example thanks :)

Flypro.
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,780
  • Joined: 20-September 08

Posted 14 November 2011 - 06:07 PM

View Postflypro, on 07 November 2011 - 06:15 PM, said:

...yeah i wouldn't mine seeing it :), ive done a similar thing reading a data wav file into a vector... problem is it don't work properly :P lol :), good example thanks :)
Flypro.


Here is a link ...
http://developers-he...index.php/topic,2019.15.html
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1