0

In the function, I created an array of size two which will hold two PolyTerms. Then, within the function, i created a List. After that, i want to pass the array elements into the List as linked list.

I'm getting an error after the if statement head->next = nodePtr; (Bad Access).

Thanks.

Part of my code:

struct Fraction {
    int num;
    int denom;
};

struct PolyTerm {
    int ex;
    struct Fraction coe;
};
typedef struct PolyTerm PTerm;
typedef struct PolyTerm* PTermPtr;

struct PolyListNode {
    PTermPtr termAddr;
    struct PolyListNode* next;
};
typedef struct PolyListNode PList;
typedef struct PolyListNode* PNodeAddr;
typedef struct PolyListNode* PolyList;

PolyList sortPoly(void);

PolyList sortPoly() {

    int arraySize = 2;
    int i = 0;

    //Array of PTermPtr. Each element holds ex, num and denom.
    //Populating 2 elements for arrayTerm

    PTermPtr arrayTerm;
    arrayTerm = (PTermPtr) malloc(arraySize);
    ((arrayTerm) + 0)->ex = 2;
    ((arrayTerm) + 0)->coe.num = 2;
    ((arrayTerm) + 0)->coe.denom = 2;

    ((arrayTerm) + 1)->ex = 3;
    ((arrayTerm) + 1)->coe.num = 2;
    ((arrayTerm) + 1)->coe.denom = 2;

    PNodeAddr nodePtr; //To create nodes
    PolyList head = 0; //New List
    PNodeAddr current; //To store Address of List Head
    current= head; //Store address of head of list

    while (i < arraySize) {

        nodePtr = (PNodeAddr) malloc(sizeof(PList));
        nodePtr->termAddr = (arrayTerm + i);
        nodePtr->next = 0;

        if (current == 0) {
            head->next = nodePtr; //ERROR. Bad Access
        } else {
            while (current != 0) {
                current = current->next;
            }
            current->next = nodePtr;
        }
        i++; 
    }
    free (arrayTerm);
    return head;
}
6
  • I'm going to write it so that nobody else has to. Use the debugger. Commented Apr 2, 2015 at 21:44
  • if you eliminate the many typedef's and write out each of the struct references, the code would be much easier to follow/understand for both us and (later) you Commented Apr 2, 2015 at 21:44
  • head should have pointed the first allocated node that's why you're getting an error. Commented Apr 2, 2015 at 21:44
  • in C, the returned value from malloc() and family of functions should not be cast. However, it should be checked (!= NULL) to assure the operation was successful Commented Apr 2, 2015 at 21:48
  • 0) arrayTerm = (PTermPtr) malloc(arraySize); --> arrayTerm = (PTermPtr) malloc(arraySize*sizeof(PTerm)); Commented Apr 2, 2015 at 21:52

1 Answer 1

2

Just think about what is going on the first time the code goes thru the loop:

PolyList head = 0; //New List

Head is now 0 (or null).

current= head; //Store address of head of list

Current and head are now 0

    if (current == 0) {

It is.

        head->next = nodePtr; //ERROR. Bad Access

Try to access head which is 0 and null. you are accessing null


It should also be noted the sizes you pass in to malloc are wrong. You are passing in the size of the arrays you want to create not the size of memory needed.

For example you are need a 2 element array of type mytype you want this code:

 newarray = malloc(2 * sizeof(mytype));

then

 newarray[0] 

and

 newarray[1]

have space for a mytype.

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

5 Comments

I edited head->next=nodePtr to head=nodePtr and allocated memory as you said. Now it runs but when i display the returned list. it's showing 2097200/1x3 instead of 2/2x2+2/2x3. i set up my display menu to display this way but it's displaying 2097200/1x3
I already fixed that part to arrayTerm = (PTermPtr) malloc(2 * sizeof(PTerm)); Changed head->next = nodePtr; //ERROR. Bad Access to head = nodePtr and i also changed current->next = nodePtr; to current = nodePtr; I'm not getting back the correct linked list.
@Denominator - there is no way to know what is wrong -- you could debug it yourself or post a new question with that code.
Solved. I was missing head->next = current; after the while loop to update the head. I don't know how to debug. I need to learn how. THank you.!! and the current = head; should go inside the while loop
That is awesome @Denominator congrats!

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.