Please translate

This is a discussion on Please translate within the C Programming forums, part of the General Programming Boards category; The purpose of the Code: for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr) /*if ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The purpose of the
    Code:
    	for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		/*if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break;  freed block at start or end of arena
    		}*/
    section is to find the free block before or after the current block and join those two blocks together, to reduce heap fragmentation.

    [At least, that's what it looks like].

    Obviously, falling off the edge of mymalloc is a bad thing, and a good compiler should spot that there's a possibility of that. If you don't want to break a new block of "OS" memory, then at least return NULL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    No the malloc is actually fine. It is the fault of the free. What are the reasons that I get a segmentation fault, here? It successfully deletes 2 memory slots, then I get a segmentation fault.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    No the malloc is actually fine. It is the fault of the free. What are the reasons that I get a segmentation fault, here? It successfully deletes 2 memory slots, then I get a segmentation fault.
    As to the segmentation fault, it's possible that as your for-loop goes, p gets set to NULL or something equally devastating, at which time trying to do p->whatever makes the computer go boom.

    And our point is that, even though your malloc appears to work, you are very possibly not leaving freeptr, or the ptr links, in the correct state, so that later trying to do something with that memory will cause problems.

  4. #19
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Wouldnt removing the for loop help?

  5. #20
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    forget it stupid question

  6. #21
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Question: Can we have more than 3 condition in a for loop?
    example, more than just initializing, field and increment
    Last edited by Ron; 07-30-2008 at 05:36 PM.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    Question: Can we have more than one condition in a for loop?
    If you want to hook them together with && or || as appropriate, sure. (Note: your for loop currently has two conditions in it -- the for loop won't stop until both are true.)

  8. #23
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    No, what I meant is, can I initialize two varibales, put a condition and increment any one variabel in the same for loop.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can do whatever you want.
    Code:
    for (a = 5, b = 19, c = q*4, d = 11876; a < b; a++, b--, d /=4)
    Note , instead of ;. (You also already have this in your code too! The for loop in mymalloc does two things at the end of every loop.)

  10. #25
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    OK, I finally figured what wrong. In the for loop I just assigned p to a prevptr, before doing any changes to p pointer. Every time I make the content of the node free, or malloc it, I label the node as occupied or free. Now the code works, beautifully.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  2. Translate number to sentence
    By alphaoide in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2004, 01:56 AM
  3. Please help me... Translate from c++ to visual basic
    By westcard in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2004, 02:29 PM
  4. can anyone translate this all to aschii characters?
    By tetra in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 09:18 PM
  5. just translate !
    By black in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-03-2002, 02:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21