1

I have problem with my school project, the server which is testing this build returns this error:

./__runscript: line 2: 489 Segmentation fault ./a.out < __input

Compile is OK, but this error is displayed just when it starts running. But if I run it on Windows 7 with visual studio 10, everything is fine. I think the mistake might be in the insert() function.

POSTUP *insert(POSTUP *first)
{
    POSTUP* current,*pom;
    current=(POSTUP*)malloc(sizeof(POSTUP));
    pom=(POSTUP*)malloc(sizeof(POSTUP));
    int i,count_of_elements;

    scanf("%d", &count_of_elements);

    if(first==NULL)
    {
        first=current;
        first->array= (int*)malloc(count_of_elements*sizeof(int));
        for(i=0; i<count_of_elements; i++)
        {
            scanf("%d",&first->array[i]);
        }
        first->elements=count_of_elements;
        first->next=NULL;
        return first;
    }
    else
    {
        pom->array= (int*)malloc(count_of_elements*sizeof(int));
        for(i=0; i<count_of_elements; i++)
        {
            scanf("%d",&pom->array[i]);
        }
        pom->next=NULL;
        pom->elements=count_of_elements;
        current=first;
        while(1)
        {
            if(current->next==NULL)
            {
                current->next=pom;
                return first;
            }   
        }
    }
    return 0;
}
int main(void)
{
    int count, i;
    POSTUP* first,*current;
    first= (POSTUP*)malloc(sizeof(POSTUP));
    first=NULL;

    scanf("%d",&count);
    for(i=0; i<count; i++)
    {
        first=insert(first);

    }
}    
3
  • 1
    pom leaks if first is NULL, and current leaks otherwise. Why not just allocate a single new node? Commented Oct 7, 2012 at 21:03
  • scanf("%d",&prvy->array[i]); -- why not &first->array[i] here? what's prvy? Commented Oct 7, 2012 at 21:05
  • Don't cast the malloc result in C. Commented Oct 7, 2012 at 21:19

1 Answer 1

1

This code in main():

POSTUP *first, *current;
first = (POSTUP*)malloc(sizeof(POSTUP));
first = NULL;

scanf("%d", &count);
for (i = 0; i < count; i++)
{
    first = insert(first);
}

is clearly problematic. You allocate space and store the only pointer to it in first; you then overwrite the pointer, leaking memory. You should officially check that the allocation was not null. But your insert() code is equipped to handle a null pointer as input, so the memory allocation in main is redundant.

In insert(), you allocate memory without checking for success; this is a recipe for trouble, though more usually later than sooner.

Your code is never checking the result of scanf(); that means you could be getting rubbish into your system if there's a format error in the data.

Likely cause of crash: In the 'if (first == NULL)block ininsert()`, you have:

    for(i=0; i<count_of_elements; i++)
    {
        scanf("%d", &prvy->array[i]);
    }

There's no declaration of prvy in sight in the code you show; it should almost certainly be referring to &first->array[i] anyway. This could easily be good for a crash. Otherwise, you've carefully stashed the data in a different set of memory than you thought, so the array for first is uninitialized gibberish.

The same code does not use pom, so you've leaked memory in this part of the code.

In the main else clause in insert(), you overwrite current (which contained a memory allocation) with first, thus leaking memory there too.

Don't allocate memory until you're about to use it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.