two different loops, are they doing the same thing ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
int myfree(int *array, int * block)
{
int i;
int position;
int blockSize;
while(&array[i]!=block)
i++;
if (&array[i] == block && array[i] !=0)
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
}
array[0] += blockSize;
for (int j = position; j<position+blockSize; j++)
array[j] = 0;
return 1;
}
Ok there is some misunderstanding here and I will try to explain it.
This code above is the code of my version of free function that frees ( releases) blocks from our main memory. What was intended to do was to find a block of memory(given as argument block) from the main memory(array) and make it available for allocation again.
So as this code says:
while(&array[i]!=block) <== find a block which has the same address at the position of the memory at index "i", When you find it you know that is the block you where looking for and you know the size of that block so you don't need to loop for the next number of position in array so you exit the loop and you save the value i,
Then
for (int j = position; j<position+blockSize; j++)
array[j] = 0;
you start a loop at the point you found the address of the block and loop through the elements of the block allocated by array and assign them with value 0 to identify that that block of memory will be free again for use.
That is what i was trying to do with this loop.
And my question was : Does this achieve the same thing with this loop :
Code:
for (int i =1 ; &array[i]!=block; i++)
{
if (&array[i] == block && array[i] !=0)
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
break;
}
}
I have only put the part am interested, assume that are both for myfree function.
int myfree(int *array, int * block)
{
int i;
int position;
int blockSize;
while(&array[i]!=block)
i++;
if (&array[i] == block && array[i] !=0)
{
printf("array[i] = %d\n ", array[i]);
printf("array[i+1]= %d\n", array[i+1]);
printf("array[i-1]= %d\n", array[i-1]);
position = i;
blockSize = *block;
}
array[0] += blockSize;
for (int j = position; j<position+blockSize; j++)
array[j] = 0;
return 1;
}
Your indentation is still misleading in that code, as previously pointed out in this thread, but anyway...
So, if I've got this straight,
function myfree is passed:
pointer to array of ints (*array) and
pointer to the first element in a block of used elements in that array (*block), whose contents contains the number of elements in that block of used elements.
logic is:
It loops through array starting at element 1, until the address of the indexed element matches the address in the pointer block. i.e. it finds the element index number (i) of the element in array pointed to by block.
It then sets blockSize from that element.
it then adds blockSize to array[0]. (which I assume is the count of unused elements)
It then uses, i/position and blockSize to zero those elements of the array in the second for loop(j)
Wouldn't you just be better off using the pointers directly and avoiding that unnecessary first loop and the need for the index variable all together? Something like:
Even if for some strange reason you didn't want to use the pointers directly and needed the index value, instead of that costly first loop, couldn't you just do something like:
Code:
i = (block - array) / sizeof(array[0])
I'm very rusty with my C as I said before, and I wasn't all that good at it when I wasn't rusty, so please forgive any errors I've made above. Someone please correct me if I'm missing something or have got something wrong.
I don't want to comment on the differences between your two loops as to me, they both seem to be somewhat flawed. (no offence ment by that).
Last edited by GazL; 12-04-2009 at 12:28 PM.
Reason: added additional pointer loopblock, missed that one. ;) Told you I was rusty.
It loops through array starting at element 1, until the address of the indexed element matches the address in the pointer block. i.e. it finds the element index number (i) of the element in array pointed to by block.
It then sets blockSize from that element.
it then adds blockSize to array[0]. (which I assume is the count of unused elements)
It then uses, i/position and blockSize to zero those elements of the array in the second for loop(j)
Wouldn't you just be better off using the pointers directly and avoiding that unnecessary first loop and the need for the index variable all together? Something like:
Even if for some strange reason you didn't want to use the pointers directly and needed the index value, instead of that costly first loop, couldn't you just do something like:
Code:
i = (block - array) / sizeof(array[0])
In your example here you are getting the blockSize from the frist element of the block as u assumed correctly which is the size of the allocated block(*block refers to the 1st element which previously somewhere in the program was set)
Then u set loopblock to the block. Ok with that BUT
How the elements of the ARRAY from our memory are affected ?
I believe that the two loop achieve the same, but they are not the same; in that the for loop has an additional conditional statement in it.
The while loop just checks for the location of the block of memory which results in a single conditional statement
The for loop looks for the location of the block of memory but within the loop it also looks to see if it has found the block of memory and if so it then stops the loop, so the check appears twice.
I have one comment on the loops, in that they never check to ensure that the counter doesn't exceed the size of the array. Whilst you may argue that this shouldn't happen it could and so the code should cater for that sort of error.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.