Bug in memory, but not revealed

S

Stefan Istrate

I compile the following code with gcc and then I run it. It doesn't
give any memory error. Is there any flag for compiler to avoid this or
a method to guaranty me the access only to the valid memory?

int main() {
int v[10];
v[17] = 33;
}

Thank you,
Stefan Istrate
 
R

Richard Tobin

I compile the following code with gcc and then I run it. It doesn't
give any memory error. Is there any flag for compiler to avoid this or
a method to guaranty me the access only to the valid memory?

No. C systems don't check this. You will only get an error if there
is no memory at that address, and there is likely to be memory after
the end of the array because of the way operating systems allocate it.

There are debugging tools such as valgrind, but they generally
only check malloc()ed memory.

-- Richard
 
V

vippstar

I compile the following code with gcc and then I run it. It doesn't
give any memory error. Is there any flag for compiler to avoid this or
a method to guaranty me the access only to the valid memory?

int main() {
int v[10];
v[17] = 33;

}
For integer constants, a warning could be possible, but pointless.
Think about it, how many times have you used an integer constant
(other than 0) in "real-world" developing?
For non-constant expressions, the compiler can't do that. Such warning
is not very useful.
 
C

Chris McDonald

For integer constants, a warning could be possible, but pointless.
Think about it, how many times have you used an integer constant
(other than 0) in "real-world" developing?

Nearly every day, for me.
 
S

santosh

Stefan said:
I compile the following code with gcc and then I run it. It doesn't
give any memory error. Is there any flag for compiler to avoid this or
a method to guaranty me the access only to the valid memory?

int main() {
int v[10];
v[17] = 33;
}

Most C implementations do not do bounds checking by default. While many
implementations have no means to do so, most mainstreams ones *will*
compile extra code to check memory access if you tell it do so, (and
possibly link with the appropriate external libraries). You need to
read the documentation for your compiler to find out the details, but
for gcc I think the '-fmudflap' option might do the trick. You also
need to provide a linker command to link with the "mudflap" library.

You might also try external tools like ElectricFence or Valgrind for
heap checking.
 
B

Ben Bacarisse

Stefan Istrate said:
I compile the following code with gcc and then I run it. It doesn't
give any memory error. Is there any flag for compiler to avoid this or
a method to guaranty me the access only to the valid memory?

int main() {
int v[10];
v[17] = 33;
}

<off topic>
If you install the mudflap library and compile (and link) with the
right flags you get (pretty much) what you want.
</off topic>
 
V

vippstar

Nearly every day, for me.
Alright, fine. My point is that having an integer constant bigger than
the arrays size is quite rare.
Just curious, but is it possible to tell me a situation or two where
you see the integer constants in array indexes?
Surely in cryptographic functions, and anything that has to do with
parsing of things whose size is always known. (IP addresses, etc)
But other than that? I might just be missing the obvious. Regardless,
I haven't done much "real" programming, so you most certainly know
better.
 
W

Walter Roberson

Just curious, but is it possible to tell me a situation or two where
you see the integer constants in array indexes?
Surely in cryptographic functions, and anything that has to do with
parsing of things whose size is always known. (IP addresses, etc)
But other than that?

Arrays are often used as a form of data structuring. For example,
it is common to have 2D arrays whose rows are [X, Y, Z] triples.
This -could- be implemented as an array of
struct {double X; double Y; double Z} but with the possible
padding between elements of a struct this could take more room than
it needs to -- and there are times where the appropriate coordinate
number needs to be calculated and so a struct can become cumbersome.

I mostly program in MATLAB these days; it is quite common in MATLAB
to see constructs such as V:),3) meaning the 3rd column of all rows
of V, or W(2,:) meaning all columns of the 2nd row of W.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top