Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault - GNU C
    primarykey
    data
    text
    <p>I've been getting a segmentation fault that I cannot figure out. The function that causes the segmentation fault is shown below:</p> <pre><code>Expression *IntegerLiteral_init(int intgr) { Expression *the_exp = safe_alloc(sizeof(Expression)); the_exp-&gt;type = expr_IntegerLiteral; the_exp-&gt;expr-&gt;intgr = intgr; the_exp-&gt;exec_count = 0; return the_exp; } </code></pre> <p>An Expression is defined:</p> <pre><code>typedef struct { expr_type type; u_expr *expr; int exec_count; } Expression; </code></pre> <p>u_expr and expr_type are defined:</p> <pre><code>typedef union { char *ident; int intgr; } u_expr; typedef enum { expr_Identifier, expr_IntegerLiteral } expr_type; </code></pre> <p><code>expr_type</code> is an enum of <code>expr_IntegerLiteral</code> and <code>expr_Identifier</code>.</p> <p>According to GDB, the segfault is caused on the line: <code>the_exp-&gt;expr-&gt;intgr = intgr;</code>. Strangely, it does not always cause a segfault - a segfault occurs if I call the function in this manner:</p> <pre><code>Expression *e = IntegerLiteral_init(0); </code></pre> <p>But in another part of my program, I call it using:</p> <pre><code>Expression *e; ... e = IntegerLiteral_init( (int)strtol(num_str, (char **)NULL, 10)); </code></pre> <p>which works without any problem. <code>num_str</code> has been parsed from some input and has the value <code>"0"</code>.</p> <p>I don't understand why the context in which I call <code>IntegerLiteral_init()</code> should affect whether or not this segfault occurs, if the given <code>intgr</code> parameter is the same. If anyone could shed any light on this, I would be most grateful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload