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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:36:37+00:00 2026-06-15T20:36:37+00:00

#include <stdlib.h> #include <stdio.h> int main() { static char* buf; buf = (char*)malloc(20*sizeof(char)); scanf(%s,buf);

  • 0
#include <stdlib.h>
#include <stdio.h>

int main()
{
    static char* buf;

    buf = (char*)malloc(20*sizeof(char));

    scanf("%s",buf);

    while(buf[0] != NULL)
        printf("\n%s\n",buf++);

    free(buf);
    buf=NULL;

    system("pause");
    return 0;
}

Message box during execution free(buf):

Windows has triggered a breakpoint in clean_rough_draft.exe.

This may be due to a corruption of the heap, which indicates a bug in
clean_rough_draft.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while
clean_rough_draft.exe has focus.

The output window may have more diagnostic information.

What’s the reason? I just want to free memory without a leak…

  • 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-15T20:36:38+00:00Added an answer on June 15, 2026 at 8:36 pm

    Because you’re incrementing buf, and then trying to free() it. By the time you free() it, it is no longer pointing to what malloc() returned.

    Also (this isn’t related to your crash), you probably should be checking buf[0] != '\0' instead of buf[0] != NULL.

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

Sidebar

Related Questions

#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
Here is my code: #include <stdio.h> #include <stdlib.h> int main(){ int n=10; char *s=
#include <stdio.h> #include <stdlib.h> int main(void) { int x; int *in, *begin; in =
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
In this C program #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main()
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> static char *dup_str(const char *s) {

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.