0

I wrote a code that return the error of segmentation fault. The code is long but I wrote the essential part of that as follows. Note that All the variables of defined function inside the main functioned are defined and I tested the code ad it does not go inside the Function_1. That is whay I think the problem is about something like declaration

    typedef struct node_type
{
    int data;
    struct node_type *next;
} node;

typedef node *list;

void Function_1(list R, int *Matrix, int Length, int A);
int main()
{
    int S = 5;
    int A = 1;
    int Mat1[5];
    //int *Mat = malloc(S*sizeof(int));
    int *Mat = &Mat1[0];
    printf("Enter the Matrix with length of 10:\n");
    for (int i = 0; i < S; i++)
    {
        scanf("%i", &(*(Mat+i)));
    }

    list head;
    head = (list)malloc(sizeof(node));
    head->next = NULL;
    for (int i = 0; i < S; i++)
        {
            printf("%d\t", *(Mat+i));
        }
    //printf("\nEnter the Array = \n");
    //scanf("\n%d", &A);
    printf("\nA = %i", A);
    Function_1(head, Mat, S, A);

    while (head != NULL)
    {
        printf("%d\t", head->data);
        head = head->next;
    }

        return 0;
    }


void Function_1(list R, int *Matrix, int Length, int A)
{
    printf("11111");
    list tail;
    tail = R;
    int Counter = 0;
    int i = 0;
    while (tail != NULL)
    {
        if (*(Matrix+i) == A)
        {
        Counter++;
        tail->next = (list)malloc(sizeof(node));
        tail->next->data = i;
        tail->next->next = NULL;
        tail = tail->next;
        }
    i++;
    }
R->data = Counter;
printf("%d", Counter);
}

Thanks

15
  • 2
    I am having difficulty finding a the segfault from the code you have supplied. Perhaps you could post a small (complete) program to demonstrate the segfault? Commented Apr 24, 2014 at 15:36
  • So, why are you sending the matrix as a pointer? Are you trying to update the data in the memory cells in Function_1? If so, then why aren't you just doing Function_1(head, &Mat1, S, A);? Are you also referencing the data correctly for Mat in your function? Also you need to send the head of the link list by reference. You should also pass the head by reference. Commented Apr 24, 2014 at 15:38
  • As I said the is compiled perfectly until exactly before calling Function_1. I think that the problem is a kind of declaration of this function's arguments. I check all of them but I don't know where is the problem. Commented Apr 24, 2014 at 15:39
  • 1
    Could you upload the whole code to somewhere? You could as well add a copy to your question. I have a couple of guesses, but I don't want to give an answer out of my guesses. Probably others wouldn't want to do so, either. Commented Apr 24, 2014 at 15:41
  • Let us see all the code up until you call your Function_1. Commented Apr 24, 2014 at 15:41

1 Answer 1

1

With that code you've provided, I can say that the segmentation fault or access violation occurs due to the lack of <stdlib.h> or <malloc.h> inclusion, causing malloc to be assumed, and then head to get assigned with something else than what you'd expect.

I don't know, it might also be happening due to random dots in your code...

Edit:

As per your latest edit, within your loop inside your function Function_1, you are attempting to access the contents of the memory locations Matrix + i, with i having no bounds. It is fine to access there for i equals 0 to 4 but not after that, and there is nothing holding it from growing any larger.

Consider changing your while condition into i < 5 or something like that. I couldn't find out what your aim could be with while ( R != NULL ) but R never changes throughout the loop, so that condition won't become false after being true once, or the opposite.

Edit 2:

Similar when the while condition is rather tail != NULL which will evaluate to 0 only if malloc failed to allocate memory. i will most likely exceed 4 before you use up so much memory that malloc starts failing.

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

7 Comments

Of course I put all the required libraries and as you see I canceled the malloc function. I put printf("1") at the begining of the Function_1 but it is not complied which means that the problem is before Function_1
@user42037 How sure are you? Look, here, if you have included both the <stdio.h> and <stdlib.h>, then your code until the Function_1 call works all fine: ideone.com/F7SFDI There is something else wrong in your code, something else than what you've shown to us.
I updated the post and put an example for Function_1. you can compile it. and you see that printf("11111"); would not be compiled
@user42037 And now, I have updated my answer. After changing the condition into i < 5, the code should work just fine, as you may see in: ideone.com/pItgk5 Errors from future can prevent your console/terminal from showing things that they should have already shown, doesn't happen on my Windows 8.1, but I wouldn't be surprised if it really does happen as you say on your end.
sorry it is just a silly mistake and it was tail instead of R. If you run the code I will understand that it does not compile printf("1111") at the beginning of Function_1. I update it.
|

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.