how to test this piece of C code
- From: "subramanian100in@xxxxxxxxx, India" <subramanian100in@xxxxxxxxx>
- Date: 9 Mar 2007 21:56:04 -0800
I am not sure whether I am posting this question to the right forum.
If wrong, kindly let me know the appropriate forum.
Consider the following code segment used in getline function to read
an arbitrarily long line from a file.
We can assume blockSize to be 1KB and initially curSize will be zero.
Both curSize and blockSize are of type size_t.
blockSize is a fixed size by which curSize is incremented whenever
more memory is needed. curSize will be passed to realloc. SIZE_MAX is
4294967295 in my gcc Redhat Enterprise Linux implementation. I am
using Intel Pentium D dual core based desktop machine with RAM size
1GB. Since SIZE_MAX value is 4GB - 1, how will I test the code inside
the "if" condition ?
if (curSize > SIZE_MAX - blockSize)
{
printf("maximum value of size_t is SIZE_MAX =
%zu. "
"Current memory size allocated is %zu.
"
"To this value a block of %zu bytes has
to be "
"added in the subsequent call to
realloc. "
"But it will exceed SIZE_MAX value and
so "
"it cannot be passed to realloc because
"
"realloc expects size_t as argument\n",
SIZE_MAX,
curSize,
blockSize
);
free(str);
str = NULL;
break;
}
Correct me if my question is wrong.
.
- Follow-Ups:
- Re: how to test this piece of C code
- From: Richard Heathfield
- Re: how to test this piece of C code
- Prev by Date: Re: Converting Numbers to Words in English by recursion.
- Next by Date: Re: origin of "application" for computer programs
- Previous by thread: Converting Numbers to Words in English by recursion.
- Next by thread: Re: how to test this piece of C code
- Index(es):