Results 1 to 4 of 4
I am trying to enable some sort of warning or segfaults for illegal memory access (read/write), using gcc.
For the code below, I expected a segfault or some warning from ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-04-2013 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 19
how to warn or crash on illegal accesses with gcc
I am trying to enable some sort of warning or segfaults for illegal memory access (read/write), using gcc.
For the code below, I expected a segfault or some warning from gcc.
and compiled it using:Code:int main() { char b='a'; char a[10]; int i; for(i=-1;i<10;i++) { a[i] = i; printf("a[%d] = %d\n", i, a[i]); } printf("\nb= %c %d"); }
the program runs completely without causing any errors! The value of variable "b" is overwritten with -1. How do I force gcc to warn of these things? valgrind is an option, but its not feasible to check for every minor change using valgrind. Can anybody point me some references. All my searches for illegal memory access take me to valgrind.Code:gcc -Wall -ansi -pedantic <filename.cpp>
- 02-05-2013 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,141
This is a C program, and it is completely legal code! Yes, you can use negative indexes! Example:
Like your code, this is perfectly legal C code. It is very dangerous code, but it is legal. So, this is where anCode:int main(void) { char* buffer = (char*)calloc(1, 100); /* Buffer points to allocated memory */ buffer += 10; /* Point buffer to 10 bytes past beginning of allocated memory; for (int i = 0; i > -10; i--) { buffer[i] = (char)i; } return 0; }statement may be appropriate, certainly during testing. You will need to enable the DEBUG flag for asserts to work, which is why they don't work in production code. This is one reason why I usually use C++ to build even C code since I can interject try/catch/throw exception handling for stuff like that.Code:assert()
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 02-05-2013 #3Linux Newbie
- Join Date
- Mar 2010
- Posts
- 152
Actually, strictly speaking the C99 standard says that if you overstep the bounds of an array the result is undefined, with the exception of pointer arithmetic involving a pointer to one past the last element (so long as you don't try to dereference such a pointer).
tpb261 - are you coming from a language such as Python or Java? In those languages (and many others) arrays are fully qualified objects with methods, properties and such and can offer protection to detect when you overstep their bounds in the way that you have. However, in C and C++ arrays (not including instances of C++'s std::array class) are just raw blocks of memory, and accessing them is just the same as accessing any other part of memory.
Segfaults are for when you try to access code that's outside of your program's memory space, not just a particular array, or when you try to access memory in a way you're not allowed (such as trying to write to read-only memory). There's no guarantee that overstepping the bounds of an array will produce a segfault (but if you go far enough you probably will...).
As for detecting illegal memory accesses, try std::array if you're using C++, or use valgrind. You don't need to check for every minor change, you can limit this to when you build major releases or suspect something "funny" is going on.Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)
- 02-05-2013 #4Just Joined!
- Join Date
- Sep 2007
- Posts
- 19
and @JohnGraham - I use(d) C - and am actually trying to learn Python.
I guess I posted the question in a very stupid manner. I know its legal to have negative indices. My question is how to detect them? One of my codes was running fine with gcc on Ubuntu. But it caused a illegal access in VC++ 2010 (yeah, its an IDE, so things work differently). And it was an error that valgrind would have caught, if I had run it. So, I want to avoid such things, in C (not Cpp). And I can't do without pointer arithmetic because the code's mostly for embedded devices.


Reply With Quote

