Why the program crased even thoungh I got the correct result?

Related searches

I am learning the data structure of LinkList, and I have already implemented the source code that works for me. Today I try to do it in another way like below. And I am confusing why the programe will be crashed even though I get the result I want.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define OK 1
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode, LinkList;   //I don't use the *LinkList on purpose to check it out

//Initialize LinkList: Create a head node
Status InitList(LinkList *L)
{
    L = malloc(sizeof(LinkList));
    if(!L) return ERROR;
    L->next = NULL;

    return OK;
}

//CreateList
Status CreateListHead(LinkList *L, int n)
{
    LinkList *s;
    int i;

    if(n < 1) return ERROR;
    InitList(L);
    srand(time(0));

    for(i=0; i<n; i++)
    {
        s = malloc(sizeof(LinkList));
        if(!s)  return ERROR;
        s->data = rand()%10+1;
        s->next = L->next;
        L->next = s;
    }

    return OK;
}

//Travese LinkList
void Traverse(LinkList *L)
{
    while(L->next)
    {
        printf("%d ", L->next->data);
        L->next = L->next->next;
    }
}

int main()
{
    LinkList *L;
    int s;

    s = InitList(L);
    if(s) printf("Successful!");
    else printf("Failed!");

    CreateListHead(L, 10);

    Traverse(L);
    return 0;
}

And the result is: Succesful! 1 6 4 6 1 1 8 2 8 2 And then the program crashed

The problem here is, whatever memory you allocate to L inside InitList(), is not going to be reflected back to the actual argument passed to the function when being called.

So, in your code

if(n < 1) return ERROR;
InitList(L);               ----------------(1)
srand(time(0));

for(i=0; i<n; i++)
{
    s = malloc(sizeof(LinkList));
    if(!s)  return ERROR;
    s->data = rand()%10+1;
    s->next = L->next; -------------------(2)
    L->next = s;
}

at point (2), L is still uninitialized. Accessing that will invoke undefined behaviour.

C uses pass-by-value, so if you have to modify the argument itself, you need to pass a pointer to that. Something like

InitList(&L);

and then,

Status InitList(LinkList **L)
{
    *L = malloc(sizeof(**L));
    if(!*L) return ERROR;
    (*L)->next = NULL;

    return OK;
}

should do the job.

Why Your Computer Crashes, To make you feel even better, modern audio/video electronics has also gotten so complex On top of the software bugs, we also have to deal with hardware that can have See some examples for MacOS and Windows to the right. gone wrong, resulting in the crash message displayed by the operating system with the � But even the background thread causes the entire application to crash. At this point I'm considering putting the MODI DLL in a command-line utility, but there's going to be a performance cost because I will have to read in an entire dictionary on every call as to validate the OCR-results.

Problem is InitList initializes a local copy of L, but not the L that is in your main function. Change it to InitList(LinkList **L) and call as InitList(&L);, and change your implementation accordingly.

Crash early and crash often for more reliable software, This last point is critical: even though this example is contrived, it or The resulting crash and stack trace will be extremely obvious, easy to debug, and fix. caller of doSomething() is broken and should have a proper bar . Well, admittedly the deviation from the correct result is quite small. So in many cases you won’t even bother. The reason is quite ‘technical’: According to Microsoft, the reason for this wrong result is the so-called binary format which the numbers are converted to for calculation (more info on Wikipedia).

I am actually pretty surprised that it happens to work on your machine in the first place. (It's what's technically known as undefined behaviour and can manifest itself in a variety of ways, including no visible symptomps.)

Anyway, one immediate issue is in InitList: when you set L there, the newly allocated pointer does not propagate back the caller. For that you need to turn it into a pointer to pointer:

Status InitList(LinkList **L)
{
    *L = malloc(sizeof(LinkList));
    if(!*L) return ERROR;
    (*L)->next = NULL;

    return OK;
}

You'll then need to call it like so:

InitList(&L);

Top 6 reasons mobile apps crash, Experts share their thoughts on the primary causes of crashes and how to fix them. People hate when apps crash, or even when they slow down or freeze for a few Your software needs to be a "good citizen in the app ecosystem," he says. "On Android, you have much deeper [memory] control and you can usually� Hi Folks Just a heads up on what happened last night when I upgraded a client from 2020.2.2 (the early release to the latest release) - things went wrong. Normally the upgrade is an entire new and separate install with MYOB enabling you to retain the previous version eg 2019.4 to 2020.1 to 2020

Reasons for a C++ program crash, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles,� I'm working through C for Dummies. It has an example code for converting inches to cm (p.135, if you have the text) using gets and atoi.. Now, I wanted to try using scanf rather than the dreaded gets, and this is the best I could come up with (based on the code the author provided).

Crash (computing), In computing, a crash, or system crash, occurs when a computer program such as a software Most crashes are the result of executing invalid machine instructions . and Unix GUI applications respond by displaying a dialogue box (such as the one shown to the right) with the option to attach a debugger if one is installed. "App crashes show a poor image of the company, so they are not acceptable". Then, careful development should be performed, and catching even improbable exceptions may be an option. If so, this must be done selectively and kept in reasonable limits.

Where i work we scan and attach documents from out network drive to a program. Ive never had problems deleting files before until i got this new computer with windows 8.1 (previous was windows 7). What i do is attach the file to our program and when the program says "completed" i go back to my file, right click, and delete.