Dynamic memory issue: accessing more than allocated

G

gbisht

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.


Thanks,
Gautam.



// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>

using namespace std;

int main(){

int *x = new int [10];

for ( int i =0; i<10; i++)
x = i;

for ( int i =0; i<20; i++)
cout << x<< endl;;

return 0;
}

////////////////////////////

$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0
 
J

Jack Klein

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.

Why were you expecting to get an error? Attempting to access memory
that does not belong to your program causes undefined behavior. The
C++ language places no requirements, nor makes any specifications at
all, on what will happen when you produce undefined behavior.

If there was a requirement that an error occur, that would not be
undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
B

Brian Szmyd

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.


Thanks,
Gautam.

You are simply overrunning your buffer here, and will be able to do so
until you reach a protected memory area. libc does not protect against
accessing outside you allocated buffer. Needless to say, this is bad, but
will not cause a run-time error which is why it's a hard problem to track
down sometimes.
 
O

Oops-c++

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.

Thanks,
Gautam.

// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>

using namespace std;

int main(){

int *x = new int [10];

for ( int i =0; i<10; i++)
x = i;

for ( int i =0; i<20; i++)
cout << x<< endl;;

return 0;

}

////////////////////////////

$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0


You can use memory tracking tools like valgrind to get the information
on invalid memory access and on memory leaks
 
T

terminator

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.

Thanks,
Gautam.

// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>

using namespace std;

int main(){

int *x = new int [10];

for ( int i =0; i<10; i++)
x = i;

for ( int i =0; i<20; i++)
cout << x<< endl;;

return 0;

}

////////////////////////////

$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0


checking for the overflow/underflow of an index to an array increases
code size and run time.
The golden rule of C and C++ is to not pay for what you do not want to
use.So in order to produce short fast erunning code ,C/C++ compilers
do not perform a bounds check for indexing strip arrays.

cheers,
FM.
 
J

Jim Langston

Hi,

I wrote a simple code in which I allocated a variable memory of 10*int
and was trying to access memory more than I allocated. I was expecting
to get an error, but the code just ran. Could someone explain why this
would be happening? I am using g++ version 4.0.1 on Mac OS X.

Thanks,
Gautam.
// main.cpp
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <memory>

using namespace std;

int main(){

int *x = new int [10];

for ( int i =0; i<10; i++)
x = i;

for ( int i =0; i<20; i++)
cout << x<< endl;;

return 0;
}

////////////////////////////

$ g++ main.cpp
$ ./a.out
0
1
2
3
4
5
6
7
8
9
0
0
0
0
0
0
0
0
0
0


Bounds checking (checking for array overflows) is not normally part of a
program. You may be able to turn on bounds checking for the compiler, check
your compiler settings. Bounds checking makes the program run slower (maybe
a little, maybe a lot) and is not 100% perfect.

In Microsoft Visual C++ .net 2003 it's called "Buffer Security Check"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top