Thread: Question about arrays, pointers and address arithmetics

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    Question about arrays, pointers and address arithmetics

    Hi.

    I have 2 functions, for most things I understand, but can someone tell me why are we using allocbuf in the cycle:

    Code:
    if (allocbuf + ALLOCSIZE - allocp >= n)
    allocbuf is an array and the name of the array is a pointer to the first element &allocbuf [0]. I understand the point of removing the pointer's current position from the whole ALLOCSIZE and comparing if the remainder is greater than what we need.

    Also what is the point of the same thing in the following cycle:

    Code:
        if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
            allocp = p;
    Which pointer are we suppose to feed to afree ()? If we feed it allocp it will free the space after allocp's current location, if we give it the first element of &allocbuf [0], it will free the whole buffer?

    The whole code is here:
    Code:
                                                /* Allocation functions. */
    #define ALLOCSIZE 10000                     // Size of available space.
    
    static char allocbuf [ALLOCSIZE];           // Storage for alloc ().
    static char *allocp = &allocbuf [0];        // Next free position.
    
    char *alloc (int n)                         // Return pointer to n characters.
    {
        if (allocbuf + ALLOCSIZE - allocp >= n){// It fits.
             allocp += n;
             return allocp - n;                 // Old p.
        } else                                  // Not enough room.
            return 0;
    }
    
    void afree(char *p)                         // Free storage pointed to by p.
    {
        if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
            allocp = p;
    }
                                                /* Allocation functions END. */
    Please reply with examples.
    Last edited by ArakelTheDragon; 02-27-2020 at 08:33 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's a very simple last in first out allocator.
    The last one you allocate must be the first one you free.

    Code:
    #include <stdio.h>
    
    /* Allocation functions. */
    #define ALLOCSIZE 10000                     // Size of available space.
    
    static char allocbuf [ALLOCSIZE];           // Storage for alloc ().
    static char *allocp = &allocbuf [0];        // Next free position.
    
    char *alloc (int n)                         // Return pointer to n characters.
    {
        if (allocbuf + ALLOCSIZE - allocp >= n){// It fits.
             char *result = allocp;
             printf("DEBUG: ALLOC=%p\n",allocp);
             allocp += n;
             return result;                 // Old p.
        } else                                  // Not enough room.
            return 0;
    }
    
    void afree(char *p)                         // Free storage pointed to by p.
    {
        if (p >= allocbuf && p < allocbuf + ALLOCSIZE) {
            allocp = p;
            printf("DEBUG: FREE=%p\n",allocp);
        }
    }
    
    int main ( ) {
      char *ptrs[5];
      for(int i = 0 ; i < 5 ; i++) {
        ptrs[i] = alloc(32);
        printf("%p\n", ptrs[i]);
      }
      for(int i = 4 ; i >= 0 ; i--) {
        afree(ptrs[i]);
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I understand what it does, what I was wondering is how do they use allocbuf. I understand its like giving 0 for the address.

    Example: allocbuf + ALLOCSIZE - allocp >= n

    means that from 0 address + maximum size - the pointer's current position >= what we want to allocate.
    It will not really be 0 address, C will alocate allocbuf from some address and reserve 10 000 elements. So in truth it means:

    from 55534th address + 10 000 elements - the pointer's current position >= the elements we need to reserve from allocbuf. All reservations are done within the allocbuf array.

    And then for freeing the cells of allocbuf[10 000] we need to just set the pointer to the first address we need to be free.

    EDIT: its a very bad way of programming it, its unclear and ambiguous. Instead they should use (pt = &allocbuf [0]) + ALLOCSIZE - allocp >= n

    Code:
    static char *pt;
    pt = &allocbuf [0]
    
    pt + ALLOCSIZE - allocp >= n
    


    Last edited by ArakelTheDragon; 02-28-2020 at 01:47 AM.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Sorry for the double post. There seems to be something wrong.

    Also why is it necessary to free them from the last one back? Can't I just free a big chunk from back, like reserve 128 by 1, but afree (ptrs[0]) directly?

    Question about arrays, pointers and address arithmetics-alloc_debug-pngQuestion about arrays, pointers and address arithmetics-alloc_debug-png

    Why not just use addresses from 0 to 300, but then free from 0 to 100? Like this I can have something on addresses from 100 to 300, but have free the space from 0 to 100? I will have to keep track of it of course.
    Last edited by ArakelTheDragon; 02-28-2020 at 02:19 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    EDIT: its a very bad way of programming it, its unclear and ambiguous. Instead they should use (pt = &allocbuf [0]) + ALLOCSIZE - allocp >= n
    Your suggestion looks the same to me, but worse than the original because now you have to keep track of one more variable instead of just using allocbuf.

    Quote Originally Posted by ArakelTheDragon
    Also why is it necessary to free them from the last one back? Can't I just free a big chunk from back, like reserve 128 by 1, but afree (ptrs[0]) directly?
    Then you need to keep track of the end of the freed portion at the start in addition to the start of the free portion towards the end, whereas if you free from the back, you only keep track of the start of the free portion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Thanks to everyone!

    as for this part:
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    when I offer to do it, it normally results in me being called stupid and my contract terminated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers and arrays
    By Daikon in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2016, 05:08 AM
  2. Pointer Arithmetics and Arrays
    By GokhanK in forum C Programming
    Replies: 4
    Last Post: 09-28-2016, 03:57 PM
  3. Wrong array length with pointers arithmetics
    By MartinR in forum C Programming
    Replies: 2
    Last Post: 10-23-2015, 10:06 PM
  4. Question in arrays and pointers
    By ASophokleous in forum C Programming
    Replies: 3
    Last Post: 10-27-2013, 11:35 AM
  5. Replies: 7
    Last Post: 05-19-2010, 02:12 AM

Tags for this Thread