1

so the thing is that this program crashes at the marked point. The most interesting thing is that everything works perfectly under linux, but on the windows platform it crashes immediately. Any thoughts?

 typedef struct BigDecimalTag* BigDecimal;   
     struct BigDecimalTag
        {
            int number;
            struct BigDecimalTag * next;
            struct BigDecimalTag * prev;

        };

        BigDecimal set(char symbol[]){

            if(symbol!=NULL){   

                if(strlen(symbol)>0){   

                    int end=0;  
                    int i=(int)strlen(symbol)-1; 
                    BigDecimal head=(BigDecimal)malloc(sizeof(BigDecimal));

                    if(head==NULL) return NULL;

                    if(symbol[0]=='-'){

                        if(strlen(symbol)>1&&symbol[1]!='0'){ 
                            head->number=negative; 
                            end=1;
                        }

                        else return NULL;
                    }



                    else if(symbol[0]<='9'&&symbol[0]>'0'){ 
                        head->number=positive;              
                    }

                    else if(symbol[0]=='0'&&strlen(symbol)==1){ 
                        head->number=zero;
                    }

                    else {
                        free(head);
                        return NULL;
                    }   

                        BigDecimal new; 
                        BigDecimal tail=head;

                        for(i;i>=end;i--){

                            if(symbol[i]>'9'||symbol[i]<'0'){
                                trash(&head); 
                                return NULL;
                            }

                            else {

                                new=(BigDecimal)malloc(sizeof(BigDecimal)); 
                                if(new==NULL)return NULL;

                                tail->next=new; 
                                **new->prev=tail;** //<-crash point
                                new->number=(int)symbol[i]-48; 
                                tail=new; 

                            }

                        }

                        return head;

                }

                else return NULL;
            }

            else return NULL;
        }

1 Answers1

4

This:

new=(BigDecimal)malloc(sizeof(BigDecimal)); 

under-allocates, since BigDecimal is a typedef that hides a pointer. Never do this!

It should just be:

new = malloc(sizeof *new);

But I strongly suggest removing the typedef-hidden pointer, it's just confusing.

unwind
  • 391,730
  • 64
  • 469
  • 606