Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Understanding the function in c program . char * allocc(){} K&R page 101

Status
Not open for further replies.

thannara123

Advanced Member level 5
Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,601
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Activity points
10,610
trying to understand the following function.
Can I get the correct working?
Code:
[COLOR=gray][FONT=Consolas]#define[/FONT][/COLOR][COLOR=black][FONT=Consolas] ALLOCSIZE [/FONT][/COLOR][COLOR=maroon][FONT=Consolas]1000[/FONT][/COLOR][/FONT][/COLOR]
[COLOR=black]
[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=black] [/COLOR][COLOR=#00008B]char[/COLOR][COLOR=black] allocbuf[/COLOR][COLOR=black][[/COLOR][COLOR=black]ALLOCSIZE[/COLOR][COLOR=black]];[/COLOR][COLOR=black]
[/COLOR][COLOR=#00008B]static[/COLOR][COLOR=black] [/COLOR][COLOR=#00008B]char[/COLOR][COLOR=black] [/COLOR][COLOR=black]*[/COLOR][COLOR=black]allocp [/COLOR][COLOR=black]=[/COLOR][COLOR=black] allocbuf[/COLOR][COLOR=black];[/COLOR][COLOR=black]
[/COLOR][COLOR=#00008B]char[/COLOR][COLOR=black] [/COLOR][COLOR=black]*[/COLOR][COLOR=black]alloc[/COLOR][COLOR=black]([/COLOR][COLOR=#2B91AF]int[/COLOR][COLOR=black] n[/COLOR][COLOR=black])[/COLOR][COLOR=black]
[/COLOR][COLOR=black]{[/COLOR][COLOR=black] 
    [/COLOR][COLOR=#00008B]if[/COLOR][COLOR=black]([/COLOR][COLOR=black]allocbuf [/COLOR][COLOR=black]+[/COLOR][COLOR=black] ALLOCSIZE [/COLOR][COLOR=black]-[/COLOR][COLOR=black] allocp [/COLOR][COLOR=black]>=[/COLOR][COLOR=black] n[/COLOR][COLOR=black])[/COLOR][COLOR=black]
    [/COLOR][COLOR=black]{[/COLOR][COLOR=black]
       allocp [/COLOR][COLOR=black]+=[/COLOR][COLOR=black] n[/COLOR][COLOR=black];[/COLOR][COLOR=black]
       [/COLOR][COLOR=#00008B]return[/COLOR][COLOR=black] allocp [/COLOR][COLOR=black]-[/COLOR][COLOR=black] n[/COLOR][COLOR=black];[/COLOR][COLOR=black]
    [/COLOR][COLOR=black]}[/COLOR][COLOR=black]
    [/COLOR][COLOR=#00008B]else[/COLOR][COLOR=black]
      [/COLOR][COLOR=#00008B]return[/COLOR][COLOR=black] [/COLOR][COLOR=maroon]0[/COLOR][COLOR=black];[/COLOR][COLOR=black] [/COLOR][COLOR=#222426][FONT=Arial][COLOR=black][FONT=Consolas]}[/FONT][/COLOR]

For more https://stackoverflow.com/questions...ram-char-allocc-kr-page-101/36245596#36245596

May i get any simple example program in microcontroler section by calloc() function .
 

The code is mutilated and hard to read, e.g. missing spaces. Please correct.
 

The referenced code from The C Programming Language, 2nd edition, page 101, simply allocates n number of bytes from a predefined character array, allocbuf[] which has been initially allocated 1000 bytes of char for storage.

Essentially the routine:

Code:
char * alloc(int n)

Packs one of more smaller character arrays into the predefined:

Code:
static char allocbuf[ALLOCSIZE];

With each of the smaller character arrays accessible by the pointer returned by the routine.

Each successive call of:

Code:
char * alloc(int n)

First checks the current position of the pointer:

Code:
static char * allocp

In relation to the end of the allocbuf, to ensure there is sufficient space to allocate n bytes/char:

Code:
if(allocbuf + ALLOCSIZE - allocp >= n)

If there is sufficient space, the pointer allocp is update to the new position and a pointer is returned for the beginning of the allocated section:
Code:
       allocp += n;
       return allocp - n;

If there is NOT sufficient space to allocate n bytes/char a NULL is returned instead:

Code:
return 0;

When utilizing such a technique with a microcontroller, some thought should be given to the:

Code:
#define ALLOCSIZE 1000

A 1000 bytes of RAM maybe too large is many cases, depending of the microcontroller in question, to dedicate solely to a character array.


BigDog
 
this is the full code
Code:
#define ALLOCSIZE 1000 /* size of available space */

static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */

char *alloc(int);
void afree(char *);

int main(void)
{
    char *p;
    printf("%p\n",allocp);
    
    p=alloc(100);
    printf("%p\n",allocp);

    afree(p);
    printf("%p\n",allocp);

    return 0;
}
    
    
char *alloc(int n) /* return pointer to n characters */
{
    if( allocbuf + ALLOCSIZE - allocp >= n)
    {
        allocp += n;
        return allocp -n; /* old p */
    }
    else
        return 0;
}

void afree(char *p) /* free storage pointed to by p */
{
    if(p >= allocbuf && p < allocbuf + ALLOCSIZE)
        allocp = p;
}


And i tried to get the variable values at each time and understood the result as seen the picture below
Capture.PNG
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top