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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:03:05+00:00 2026-05-23T08:03:05+00:00

If I say: #include <stdlib.h> #include <stdio.h> #include <string.h> char *x; char *y; int

  • 0

If I say:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *x;
char *y;

int main() {
        x = malloc(sizeof("Hello, world!"));
        strcpy(x, "Hello world!");
        y = "Hello, world";
        free(x);
        fprintf(stderr, "okay");
        free(y);
}

Then, obviously, the program will print “okay” followed by dying because the “pointer being freed was not allocated”—obviously, because the string was a string literal.

I’d like to write a function that does nothing when given string literals, but calls free when given non-string literals. Is that possible, and if so, how?

  • 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-23T08:03:06+00:00Added an answer on May 23, 2026 at 8:03 am

    No. In C, you have to keep track of what you’ve allocated yourself.

    Some malloc implementations (such as dlmalloc) provide some extra functionality for inspecting the heap, but you shouldn’t rely on those. dlmalloc has the function dlmalloc_inspect_all, which will walk the heap and return to you all of the regions of memory that malloc has allocated, except for memory-mapped chunks. So you could use that to test if a pointer points to a non-memory-mapped allocation, but it’s still a bad idea in general.

    And on Windows, don’t even think about using [IsBadReadPtr] to test if a pointer points to readable memory or not — it should really be called CrashProgramRandomly.

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

Sidebar

Related Questions

Say you have a C code like this: #include <stdio.h> int main(){ printf("Hello, world!\n");
So basically: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> int main(void){ //test strrev
Let us say i have File1.c: #include<stdio.h> #includeFile2.c void test(void) { sum(1,2); } int
Let's say I compiled the application below and stripped it's symbols. #include <stdio.h> int
Say I have something like: #include <vector> #include <algorithm> #include <tr1/functional> void bar(int value)
Lets say we have the following code: #include <iostream> #include <string> struct A {
Please Explain the following code #include <iostream> using namespace std; int main() { const
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
In the following code, I copy a string in to a char* str, which
Sorry for a very generic sounding question. let's say #include <sys/socket.h> #include <sys/types.h> #include

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.