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

  • Home
  • SEARCH
  • 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 463409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:10:21+00:00 2026-05-12T23:10:21+00:00

I am a C beginner, and I am writing a very simple linked-list. I

  • 0

I am a C beginner, and I am writing a very simple linked-list. I am wondering if there would be a memory leak in the following code:

void removeListEntry(struct tableEntry *symp, struct tableEntry *previous) {
  if (symp->next = 0){
    symbolList.tail = previous;
    previous->next =0;
  } else {
    previous->next = symp->next;
    symp->next = 0;
  }
}

I am pretty sure if the pointer symp is not stored in another variable, there’s no way of accessing the list entry that was pointed by the pointer, thus I will have a memory leak.
In C, we use malloc() function to allocate memory space for a data structure, and I remember using new keyword to “dynamically” allocate memory in C++. What are the differences between allocating memory using malloc() and using new? Is there indeed a memory leak in my code?

  • 1 1 Answer
  • 1 View
  • 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-12T23:10:21+00:00Added an answer on May 12, 2026 at 11:10 pm

    I am curious about what is supposed to be happening in your code:

    if (symp->next = 0){
      symbolList.tail = previous;
      previous->next =0;
    } else {
      previous->next = symp->next;
      symp->next = 0;
    }
    

    When would symb->next not be zero? You aren’t doing anything if symb is empty as the head node would also be null.

    The confusing part is that you are appending previous to symb in the first if (which should always be the case) but in the next one you are appending symb to previous. What rationale is there for this second one, and in what case will it ever happen?

    As others have mentioned, if you allocate memory you need to free it, else you have a memory leak, as there is no garbage collector in C/C++, so every node that is going to be freed needs to be deallocated.

    The symb->next = 0 is probably just a typo, as was pointed out, as that will always be true, and is a frequent bug. What I started to do to help catch this is to do:
    if (0 == symb->next), so if you did 0=symb->next then you will get a compiler error.

    UPDATE:

    As was pointed out in a comment, this function will always go to the ‘else’ clause, which may be expected behavior, actually.

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

Sidebar

Related Questions

I am a beginner and writing code to display data in a Gridview using
i am writing javascript code on nodepad++,i am at beginner level and write just
Beginner in Android development. My code crashes. I have made a simple Java method
I am writing a test code (I'm just a beginner) where I need to
A very common beginner mistake when writing recursive functions is to accidentally fire off
I'm ocaml absolute beginner and I'm writing some code for classes and I've got
I'm just a beginner in C++. I'm writing small and simple program that prints
I am a beginner at programming. I am writing code using class inheritance. Here
Sorry guys!!! pardon me. I'm a beginner in Python. I am writing the following
I'm a beginner in Java and I'm writing a program to read from multiple

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.