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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:47:54+00:00 2026-05-16T02:47:54+00:00

I have a struct struct request { int code; char *message; }; that I’d

  • 0

I have a struct

struct request {
  int code;
  char *message;
};

that I’d like to free properly.

I have the following function to do that:

void free_request(struct request *req) {
  if (req->message != NULL) {
      free(req->message);
  }
  free(req);
  req = NULL;
}

The problem is that I get an “free(): invalid pointer”/segfault error from the compiler when I try to free a request that has been created using a string literal:

struct request *req;
req = malloc(sizeof(struct request));
req->message = "TEST";
free_request(req);

Since I want to create request structs in different places, once using literals (on the client side) and once using *chars that I read from a socket (on the server side) I was wondering if there is a function to make sure that I don’t try to free the literals while still allowing me to free the message I have created using a malloc.

  • 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-16T02:47:54+00:00Added an answer on May 16, 2026 at 2:47 am

    There is no standard function that lets you know if a pointer was dynamically allocated or not. You should include a flag in your struct to inform yourself of it, or only use dynamically allocated strings (strdup is your friend in this case). Depending on your networking setup, it might be simpler to use strdup (well, to tell the truth, it is simpler to use the strdup at all).

    With strdup:

    struct message* req;
    req = malloc(sizeof *req);
    req->message = strdup("TEST");
    free_request(req);
    

    With a flag:

    struct message
    {
        int code;
        char* message;
        bool isStatic; // replace to 'char' if bool doesn't exist
    };
    
    void free_request(struct message* req)
    {
        if (!req->isStatic) free(req->message);
        free(req);
    }
    
    struct message* req;
    req = malloc(sizeof *req);
    req->message = "TEST";
    req->isStatic = 1;
    free_request(req);
    

    Also, don’t forget to zero your allocated memory when you create an object. That could save you a lot of trouble.

    req = malloc(sizeof *req);
    memset(req, 0, sizeof *req);
    

    That, and setting req to NULL from free_request won’t have any effect. You either need to take a struct message** or do it yourself after the function calls.

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

Sidebar

Related Questions

I have a struct that looks something like: struct foo_t { template <std::size_t x,
I have a data structure like this: struct foo { int id; int route;
I have a shared structure, and inside it a request structure: struct shared_data {
Ok so I have struct like this typedef struct { float x; float y;
using C++, I have struct Base { virtual void stuff(/*base stuff*/); }; struct Derived
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have a struct, MyStruct , that has a private member private bool[] boolArray;
I have a struct that contains three lists and an integer as can be
I have a struct Primitive, which has the following definition: typedef struct Primitive {
Hi I have a simple browser that sends a request to yahoo.com which 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.