LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2009, 10:15 AM   #16
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0

Code:
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.

Thanks,
venix

Last edited by venix; 12-04-2009 at 10:19 AM.
 
Old 12-04-2009, 12:08 PM   #17
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Code:
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:
  1. 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.
  2. It then sets blockSize from that element.
  3. it then adds blockSize to array[0]. (which I assume is the count of unused elements)
  4. 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:

Code:
blockSize = *block ;

loopblock=block ;
while ( loopblock < (block + blockSize) )
{
  *loopblock = 0 ;
  loopblock++ ;
}

*array += blockSize ;
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.
 
Old 12-04-2009, 01:06 PM   #18
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
[QUOTE=GazL;3779969][CODE]



logic is:
  1. 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.
  2. It then sets blockSize from that element.
  3. it then adds blockSize to array[0]. (which I assume is the count of unused elements)
  4. 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:

Code:
blockSize = *block ;

loopblock=block ;
while ( loopblock < (block + blockSize) )
{
  *loopblock = 0 ;
  loopblock++ ;
}

*array += blockSize ;
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 ?
 
Old 12-04-2009, 01:48 PM   #19
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
That while loop should do exactly what your for (j ....) loop does. *loopblock = 0; is the same as your array[j]=0,

Atleast that is, if I've not got it wrong.
 
Old 12-05-2009, 04:24 AM   #20
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-05-2009, 09:36 AM   #21
venix
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks, very much.. I think you covered me
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using of arrays in loops relikwie Programming 1 09-03-2009 11:23 AM
for loops, if statements PAvandal Linux - Newbie 13 07-20-2009 09:31 PM
STP loops eth777 Linux - Networking 3 10-31-2007 07:52 AM
while loops + while : blizunt7 Linux - General 3 12-04-2004 05:27 PM
loops JMK Linux - Newbie 11 04-09-2004 05:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration