Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:34:36+00:00 2026-05-31T06:34:36+00:00

I have a piece of code that checks to see if a macro is

  • 0

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 = &macro_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 = &macro_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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:34:37+00:00Added an answer on May 31, 2026 at 6:34 am

    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_name[MAX_MACRO_NAME_LEN];
        char macro_body[MAX_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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code that checks a time and then adds hours
I have a piece of code that load a very big image in memory.
I have this little piece of code that just checks for a particular text
I have created a piece of code that checks files for a user-submitted string
I have a piece of code that checks that a survey response picked by
I have the following piece of code that checks if the returned video object
I have a small piece of code that checks a string for a url
I have a piece of code that, when the page loads, checks through a
I have a piece of Python code that (for better or worse) checks a
I have a piece of code that was written by someone else before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.