1

I'm having problems with a segmentation fault on a Linux system. I'm using code from Aho and Ullman's "Foundations of Computer Science" C edition. This is the code

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

typedef struct que_element
{
    long mem_address;
    long mem_data;

    int msg;
} Qq;

typedef struct CELL *LIST;
struct CELL
{

    Qq el;

    int element;
    LIST next;
};


Qq l1element;

typedef struct
{
    LIST front;
    LIST rear;
} QUEUE;

main()
{
    QUEUE *l1c2l1d; /*L1 Controller to L1 Data */

    l1c2l1d->front = malloc(sizeof *l1c2l1d);
}
3
  • If you want to represent true/false, use typedef char BOOLEAN. ;) int usually has 4 bytes on 32-bit, while char has always 1 byte. Commented Feb 17, 2012 at 21:11
  • @Gandaro But an int is usually faster to access than a char. Commented Feb 17, 2012 at 22:33
  • Why define your own boolean instead of including stdbool.h? Commented Feb 18, 2012 at 18:44

2 Answers 2

2

Is there an initializing step I'm missing?

Yes, l1c2l1d is uninitialized in your code. Dereferencing it means dereferencing NULL (since l1c2l1d is global). Try this:

l1c2l1d = malloc(sizeof *l1c2l1d);
l1c2l1d->front...

EDIT In light of last edit

You have this:

main()
{
    QUEUE *l1c2l1d; /*L1 Controller to L1 Data */

    l1c2l1d->front = malloc(sizeof *l1c2l1d);
}

In this case l1c2l1d is uninitialized, it points to garbage. Try this (copy-paste it this time):

main()
{
    QUEUE *l1c2l1d; /*L1 Controller to L1 Data */

    l1c2l1d = malloc(sizeof *l1c2l1d);
    l1c2l1d->front = malloc(*l1c2l1d->front);
}
Sign up to request clarification or add additional context in comments.

9 Comments

I just tried l1c2l1d = malloc(sizeof *l1c2l1d) , but it also causes the segmentation error.
I just tried making defining l1c2l1d in main(), still segmentation error.
@Mark Something else must be causing it (like your enqueue function).
I removed all of the function definitions (enque, deque, etc.). I just have main() and the QUEUE and link list definitions. It still dies on that first line (the malloc).
I think it's time to punt. I'll just use an array of structures as my linked list. Not as fancy but I won't have to worry about malloc(). Thank you for your help.
|
0

You seem to be going for something along the lines of:

main()
{
    QUEUE l1c2l1d = { 0, 0 }; /*L1 Controller to L1 Data */

    l1c2l1d->front = l1c2l1d->rear = malloc(sizeof CELL);
}

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.