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 8290707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:53:23+00:00 2026-06-08T12:53:23+00:00

Following is the implementation of a stack struct in C. I need to write

  • 0

Following is the implementation of a stack struct in C. I need to write a function (not a method) to dispose off an existing stack and ofcourse free any memory allocated for it on the heap. the dispose (stack *s) function takes in a pointer to a stack that is to be disposed off.

typedef struct {
    int allocatedLength;
    int logicalLength;
    int elementSize;
    void *elems;
} stack;

If I implement the dispose() function as follows, will it lead to some memory never being freed?

void dispose (stack *s) {
    free (s->elems);
}

In the above implementation, I am only freeing the memory allocated to elems, which is not even on the heap. We need to remember that elems is a pointer to something. Shouldn’t we be freeing that something, instead of freeing the memory allocated to the pointer?

  • 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-06-08T12:53:24+00:00Added an answer on June 8, 2026 at 12:53 pm

    You misunderstand: free(p) does not free the memory for p, but the memory for *p — that is, the memory pointed to by p. Thus, you are correctly freeing the allocated memory to which you stored a pointer in s->elems, and all is well.

    Feel free to blame everyone who talks about “freeing a pointer” for this misnomer, when really they should be saying “freeing the memory to which I have a pointer”.

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

Sidebar

Related Questions

Consider the following implementation of a function to compute factorial: [1] (define fac-tail (lambda
The following code is part of a stack implementation, implemented via a linked-list, in
Take the following implementation of a array-based stack of chars for example: public char
I was told in class that the following function is not tail recursive due
I'm trying to statically verify the following partial implementation of an array-based stack with
In the following implementation of a hypothetical navigation module the module object returns properties
I recently saw the following implementation of enqueue for a BlockingQueue ( source )
I have a problem with the following implementation of hook_cron in Drupal 6.1.3. The
Given I have two File objects I can think of the following implementation: public
The following XML implementation seems to work fine: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent

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.