Why does last line fail?

The routine below fails on the last line. It ran fine on an 8 bit machine some years ago and the lines were actually in a for statement. The last line bombs it when I compile it as a standard unix tool with Xcode. Any help about why it fails much appreciated. Need to get the original routine running again.

*****************

struct TRIE {
char *letters;
struct TRIE *subtries;
struct TRIE *tlink;
struct allomorph *amorphs;
};

struct allomorph {
char *rest_key;
char conds[2];
union {
char *morphname;
struct SD_affix *morpheme;
} dp;
struct allomorph *alink;
};

struct SD_affix {
char *morphname;
char category[8];
unsigned props;
};

struct amlist {
struct allomorph *amp;
int alen;
struct amlist *amlink;
};

int main()
{
struct allomorph *ap;
struct amlist *lp;
struct amlist *pfxlist;

lp = pfxlist;
ap = pfxlist->amp;
lp = lp->amlink;
ap = lp->amp; /*fails here with "EXC_BAD_ACCESS" message or BUS ERROR*/

return(0);
}

// the debugger gives EXC_BAD_ACCESS and compiled for runtime gives BUS ERROR


None of the pointers in main() are initialized...
jsmith got it, nvm
Last edited on
Been 20+ years since I maintained the code. I can't find where any of the pointers were initialized. Guess I wonder why it ran on the 8 bit computers (used Aztec C) and won't run now.

I know I'm ignorant, but give me an example of initializing one of the pointers, please.

Thanks
closed account (S6k9GNh0)
When you create a pointer, it allocates just enough memory to handle the address to a variable. It doesn't actually allocate space for the variable it's meant to hold.
1
2
3
int * pExample;
int bob;
*example = bob; //this is not valid because space has not been attached to the pointer pExample. 
If we were to try and access *example, it probably wouldn't give anything (??? because it's NULL ???).

There are many solutions, one concerning stack and the other concerning heap.
1
2
3
4
int *pExample;
int bob;
pExample = &bob; //This assigns pExample the address of bob. All variables have they're own unique adress. Now we can do...
*pExample = 3; //This sets the space assigned to pExample to 3. Now bob = 3. 
This is one method of allocating and assigning a pointer in stack. This isn't normally done since we have limited space on stack and this can be costly.

The more common method of doing this is to allocate space on heap like so:
1
2
3
int *pExample; //Allocate space on stack for pointer to hold memory in heap.
pExample = new int; //This allocates space on the heap. So now we can do something like...
*pExample = 3; //This assigns the memory we allocated on heap to 3.  


This covers the basics. If you want more information on the difference on stack and heap, please consult the reference on the site.
Last edited on
Topic archived. No new replies allowed.