Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>FrameType *temp; temp-&gt;funcName ... </code></pre> <p>dereferences an uninitialized pointer, which yields <strong><em>undefined behavior</em></strong>, which in this case you are able to observe as segmentation fault. One of the possible solutions is using an object with automatic storage duration instead of pointer:</p> <pre><code>FrameType temp; temp.funcName ... </code></pre> <hr> <p>Also note that</p> <pre><code>temp-&gt;funcName = fname; </code></pre> <p>assigns the value of pointer <code>fname</code> (i.e. an address it points to) to the pointer <code>funcName</code>, no copying is performed and once the memory associated with <code>fname</code> is freed, this <code>temp-&gt;funcName</code> will be an invalid (dangling) pointer and using it will also result in <em>undefined behavior</em>.</p> <p>On the other hand:</p> <pre><code>strcpy(temp-&gt;funcName, fname); </code></pre> <p>tries to copy null-terminated string stored in <code>fname</code> to <code>funcName</code> and in case the string is not null terminated or there is no memory associated with this pointer, it will result in <em>undefined behavior</em> as well.</p> <p>Note that:</p> <pre><code>typedef struct { char *funcName; VarType parms[MAX_PARMS]; int numParms; } FrameType; </code></pre> <p>only declares a pointer <code>funcName</code> so you should either explicitly allocate a memory using <code>malloc</code> before trying to copy a string to it or yet even better, use a buffer with automatic storage duration instead:</p> <pre><code>typedef struct { char funcName[255]; VarType parms[MAX_PARMS]; int numParms; } FrameType; </code></pre>
 

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