10
\$\begingroup\$

I've created a program to calculate Pascal's triangle:

#include <stdio.h>
#include <stdlib.h>
void pascal(int limit)
{
    int *begin;
    begin=(int*)malloc(2*sizeof(int));
    *begin=1;
    *(begin+1)=1;
    int* p,q,s,t;
    int row=2,n=3,x;
    p=begin;
    q=begin+1;
    for(n=3;n<=limit+1;n++)
    {
        if(n==4)
        {
            free(begin);
        }
        x=1;
        printf("Row (%d) : ",row);
        for(int* i=p;i&lt;=q;i=i+1)
        {
            printf("%d ",*i);
        }
        putchar('\n');
        s=(int*)malloc(n*sizeof(int));
        t=s+n-1;
        *s=*p;
        *t=*q;
        for(int* g=p;g&lt;q;g=g+1)
        {
            *(s+x)=*g+*(g+1);
            x++;
        }
        p=s;
        q=t;
        row++;
    }
}

int main(void)
{
    int i;
    printf("Till which row would you like to calculate the Pascal Triangle?\n\n");
    printf("Type your answer:\t");
    scanf("%d",&i);
    putchar('\n');
    if(i<n)
        printf("Nope, you can't do that\n");
    else
    {
        printf("Row (1) : 1\n");
        pascal(i);
    }
}

My idea was to work with 2 arrays at the same time, but there is still a problem in my algorithm. How am I supposed to free the memory I allocated before? The first 8 bytes of begin are easy to free, but what should I do so I can free the memory I allocated through the pointers?

\$\endgroup\$
  • \$\begingroup\$ Please format your code! \$\endgroup\$ – MrSmith42 Mar 14 '14 at 10:04
  • \$\begingroup\$ Can someone please explain to me what does "format your code" mean? Thank you. \$\endgroup\$ – Giorgos Patelis Mar 14 '14 at 10:09
  • \$\begingroup\$ Indent blocks, remove empty lines (if they are not needed for better reading) ... You can e.g use prettyprinter.de/module.php?name=PrettyPrinter \$\endgroup\$ – MrSmith42 Mar 14 '14 at 10:10
  • 1
    \$\begingroup\$ Thank you so much.This is webseit is really useful.By the way is it good this way? \$\endgroup\$ – Giorgos Patelis Mar 14 '14 at 10:25
  • \$\begingroup\$ How to format a code is a matter of taste, every developer likes it a bit different. But the main reason is, that the code is easier to read when the code is formatted. \$\endgroup\$ – MrSmith42 Mar 14 '14 at 10:48
5
\$\begingroup\$
  • you don't free s which means you have a memory leak

  • *(begin+1) can be rewritten as begin[1], which is much clearer

  • 1 char variable names are very hard to get into, use longer/descriptive names where needed

the entire function can be rewritten as

void pascal(int limit)
{

    int *row;
    row=(int*)malloc(2*sizeof(int));
    row[0]=1;
    row[1]=1;

    for(int n=3;n<=limit+1;n++){

    printRow(buffer,n);

    int* nextRow = (int*)malloc((1+n)*sizeof(int));
    nextRow[0]=row[0];
    nextRow[n]=row[n-1];

    for(int i=1;i<n;i++){
        nextRow[i]=row[i-1]+row[i];
    }
    free(row);
    row=nextRow;

    }
}

instead of the malloc->assign->free sequence I could have used realloc and then worked backwards:

row = (int*)realloc(row,(1+n)*sizeof(int));
row[n]=0;

for(int i=n;i>0;i--){
    row[i]+=row[i-1];
}

but this is less clear

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.