Up vote 0 down vote favorite share g+ share fb share tw.
I have a piece of code that checks to see if a macro is already defined, and if it isn't then it allocates memory for a new macro and adds it onto the current list. If it is already defined then it just changes the macro body, and keeps the name the same. Static struct macro *macro_lookup(char *name){ struct macro * temp = ¯o_list; while(temp->next!
= NULL){ if(strcmp(temp->macro_name,name) == 0){ return temp; } } return NULL; } void macro_set(char *name, char *body){ //Need to check to see if a macro is already set for the name if so just change the body struct macro * p = macro_lookup(name); //Will return NULL if macro is not in the list. This line gives me the error of segmentation fault 11, if I comment it out the program works. //Need to make a new macro and add it to the list if(p == NULL){ //Make a new macro struct macro * new_macro = (struct macro *) Malloc(sizeof(struct macro)); //Malloc is my version of malloc, it works just fine.
If(new_macro == NULL){ fprintf(stderr,"Error while allocating space for the new marco. \n"); exit(EXIT_FAILURE); } new_macro->macro_name = name; new_macro->macro_body = body; //Create a pointer to the list and traverse it until the end and put the new macro there struct macro * temp = ¯o_list; while(temp->next! = NULL){ temp = temp->next; } temp->next = new_macro; } //The macro already exists and p is pointing to it else{ //Just change the body of the macro p->macro_body = body; } } I don't know why the error line above gives me a problem, I can statically set p to null and test it and it works fine, but when I use the macro_lookup function it gets a seg fault.
C pointers segmentation-fault link|improve this question asked Mar 13 at 5:35Greg Brown8717 100% accept rate.
2 You should learn how to use a debugger. It will help you pinpoint the exact line your program crashes and also let you examine variables to see if any of them is e.g. NULL. The most common debugger in Linux is probably GDB.
– Joachim Pileborg Mar 13 at 6:22.
This is likely the problem: new_macro->macro_name = name; new_macro->macro_body = body; You should normally allocate enough space for the strings and then copy them. You can't simply hand them over like that unless the calling code does the memory allocation and releases the information to the macro_set() function. It would have been helpful if you had shown the definitions of your macro structure.
I'm assuming that it is roughly: struct macro { char *macro_name; char *macro_body; }; Rather than: struct macro { char macro_nameMAX_MACRO_NAME_LEN; char macro_bodyMAX_MACRO_BODY_LEN; }; If it were the latter, you'd only need to use strcpy(), but you would have to check for overflows before doing so.
Also, you're not incrementing temp (which is bad because your loop will never end).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.