On 31/03/11 22:08, Bob Harris wrote:
> On Mar 31, 2011, at 4:25 PM, Tom Hughes wrote:
>
>> The simple answer is that, assuming you don't actually run of of memory or
>> address space, valgrind really couldn't care less how small or large the
>> chunks of memory you use are. If you were to run of space then you would get
>> an error.
>
> Thanks, it is helpful to know valgrind will report such an error. I'm
> assuming you
> mean that valgrind would report an error in that case, that a call to
> malloc or realloc
> has failed.

Well either valgrind will report an error, or the function will return a 
null pointer in the normal way - it probably depends on exactly what 
fails which happens.

> So one question I have though is what "you" means in your last sentence.  Does
> it mean that my target program has run out of memory, or the combination of
> valgrind and my target, together, has run out of memory.

Either could happen - if the primary allocation fails then I believe you 
will get a null pointer back in the normal way If the shadow memory 
allocation fails then I think you will get an error from valgrind instead.

>> Valgrind has told you that you are reading more data into a buffer than you
>> have allocated space for - it is almost certainly correct. Trust it and
>> investigate the problem it is telling you about.
>
> Hmmm... that may be true in the original target (though, at the risk
> of stepping on
> toes, I am still not convinced that this isn't an artifact of a
> failure of large memory
> allocation when valgrind is present).  But in the simple test case I
> don't do anything
> with the allocated memory but free it.  And yet I get some of the same
> complaints
> from valgrind.

I hadn't looked at the test program before, but I think I know what is 
happening there...

You aren't checking the realloc return value, and when realloc fails it 
returns null but leaves the block intact. My guess is that it is failing 
and then the following realloc calls are doing realloc on a null pointer 
(ie malloc) and probably failing as well.

At the end you free a null pointer (defined as safe) and are then left 
with the orphaned block from the point when the first failure occurred.

Tom

-- 
Tom Hughes ([email protected])
http://compton.nu/

------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself; 
WebMatrix provides all the features you need to develop and 
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to