本文发表在 rolia.net 枫下论坛Thank you for your discussion!
a)
1. You answer of 0.02-0.08 seconds is not reasonable. You answer means that under that condition any program can not last to run for more than 0.02-0.08 seconds.
2. My understanding of upper bound here is as long as possible.
3. Of course as you said we can also assume i is a huge digital which occupies all of the 1 MB memory except several bytes used by the instructions for iterative operation. But in such case we will waist a lot of memory to do iteration operations. The more the iteration operations (for(){for(){for(){...}}}) we put in a program the more time it can last to run.
b)
// The range of subscripts for vals[] should be 0 to 199 if this is a C program.
// vals[200] is out of the memory block you allocated. Dangerous!
// Please change vals[200] to vals[199], then run it and tell me the result.
// Let's learn from our mistakes! Thank you again!
//
int *vals,*ptr;
vals = malloc(sizeof(int)*200);
memset (vals,'\0',sizeof(int)*200);
while(!vals[200]) // error: vals[200] is out of the block of you allocated
{
*(ptr = vals) += 1;
while (*ptr == 10)
{
*ptr++ = 0;
*ptr += 1;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
a)
1. You answer of 0.02-0.08 seconds is not reasonable. You answer means that under that condition any program can not last to run for more than 0.02-0.08 seconds.
2. My understanding of upper bound here is as long as possible.
3. Of course as you said we can also assume i is a huge digital which occupies all of the 1 MB memory except several bytes used by the instructions for iterative operation. But in such case we will waist a lot of memory to do iteration operations. The more the iteration operations (for(){for(){for(){...}}}) we put in a program the more time it can last to run.
b)
// The range of subscripts for vals[] should be 0 to 199 if this is a C program.
// vals[200] is out of the memory block you allocated. Dangerous!
// Please change vals[200] to vals[199], then run it and tell me the result.
// Let's learn from our mistakes! Thank you again!
//
int *vals,*ptr;
vals = malloc(sizeof(int)*200);
memset (vals,'\0',sizeof(int)*200);
while(!vals[200]) // error: vals[200] is out of the block of you allocated
{
*(ptr = vals) += 1;
while (*ptr == 10)
{
*ptr++ = 0;
*ptr += 1;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net