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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:12:27+00:00 2026-06-12T18:12:27+00:00

I ran into a terrible error in testing some code. I figured out that

  • 0

I ran into a terrible error in testing some code. I figured out that it was caused by calling free(p_current_item->s) where p_current_item is a node in a linked list, and s is the char* it contains. I created the node by calling a method addItem(node,char*) which simply adds the element to the list: p_head=addItem(p_head,"this gets added");
What is bothering me is:

1) Why I would need to free the string contained in the element. I know it is necessary when s is declared as char* s = snprintf(s,(size_t),30,"this gets added"); (which is how it was done in my class example) but why is that necessary- is it possible to have the attributes contained within a struct (in this case the linkedList node) be freed when the struct itself is freed?

2) what was happening when I tried to free the value of s which was explicitly declared

3) Do i need to free up the value of s some other way?

Thanks 🙂

  • 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-12T18:12:28+00:00Added an answer on June 12, 2026 at 6:12 pm

    You can only free memory which has been allocated using the malloc family of calls.

    Strings encapsulated in double quotes are compile time constants. They get imported into your runtime program together with the instruction text. Thus those have not been allocated dynamically.

    Also snprintf returns the length of the produced string, not a pointer to it. the correct usage would be:

    size_t length = snprintf(s,(size_t)30,"this gets added");
    

    1) You only need to free s if it was allocated using malloc, calloc or another call from that family. e.g. char* s = malloc(1024);

    2) free("constant string"); will at best result in a segmentation violation because you are trying to deallocate memory which has not been allocated by the malloc family of calls. Generally this is undefined behaviour.

    3) You do not need to free constant strings, they are part of your text and cannot be freed even if you wanted to do so.

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

Sidebar

Related Questions

Ran into an Out of Stack Space error trying to serialize an ASP.Net AJAX
Ran into this error message while trying to select some records off a table.
I ran into some trouble while creating a C-Extension for ruby that got me
Ran into another challenge. I looked through some of the questions that I found
I ran into an issue with some third party software that we use to
I am writing a (terrible) CSS minifier and ran into some trouble when removing
Ran into a bit of an issue with some jQuery code, but in essence,
I ran into a problem using Springsource Tool Suite when writing some groovy scripts
I ran into a problem that suggests I may be implementing a design pattern
I ran into a strange issue with tinyMCE that i was not able to

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.