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

The Archive Base Latest Questions

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

The below program takes input a sentence , an old word , and a

  • 0

The below program takes input a sentence, an old word, and a new word.
The objective is to replace all the occurrences old word with the new word.

int countOccurrence(char* sen,char* word) //counts occurrences of old word
{
    int count=0,i,k,len1,len2;

    len1=strlen(sen);
    len2=strlen(word);
    for(i=0;i<len1-len2+1;)
    {
            k=0;
            while(word[k] && sen[k+i]==word[k])
                    k++;
            if(k==len2 && sen[k+i]==' ' || sen[k+i]=='\0')
            {
                    count++;
                    i+=len2;
            }
            else ++i;
    }
    return count;
}

void replace(char* sen,char* oldword,char* newword)
{
    int count,len1,len2,len3,i,top=-1,k;
    char *ptr;

    count=countOccurrence(sen,oldword);

    if(!count) return;

    len1=strlen(sen);
    len2=strlen(oldword);
    len3=strlen(newword);

    ptr=(char*)malloc(sizeof(len1+count*(len3-len2)+1));

    for(i=0;i<len1-len2+1;)
    {
            k=0;
            while(oldword[k] && sen[k+i]==oldword[k])
                    k++;
            if(k==len2 && sen[k+i]==' ' || sen[k+i]=='\0')
            {
                    for(k=0;newword[k];++k)
                            ptr[++top]=newword[k];
                    i+=len2;
            }
            else
            {
                    ptr[++top]=sen[i];
                    ++i;
            }
    }       
    ptr[++top]='\0';

    strcpy(sen,ptr);

    free(ptr); <-------------------------------
} 

After executing, i am getting the error as: http://ideone.com/mh3X1

*** glibc detected *** ./prog: free(): invalid next size (fast): 0x08f49008 ***
======= Backtrace: =========
/lib/libc.so.6[0xb75fcfd4]
/lib/libc.so.6(cfree+0x9c)[0xb75fe87c]
./prog[0x8048834]
./prog[0x80484c1]
======= Memory map: ========    

The programs works when i comment the statement:

free(ptr);    

see here: http://ideone.com/fr34H

Why i am getting error when i try to free() the memory allocated on heap?

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

    You messed up the internal information kept by malloc and it detected it when at the next operations (which was the free). Here’s the reason:

    ptr=(char*)malloc(sizeof(len1+count*(len3-len2)+1));
                      ^^^^^^
    

    In this context the sizeof evaluates to the size of the integer on your platform: almost surely less than what you want. So when you write to ptr, after 4 or 8 bytes you’ll step outside the allocated area and all bets are off.


    Side note: it’s largely a matter of taste but you should probably stop casting the value returned by malloc.

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

Sidebar

Related Questions

In the below program,i am not getting values from printf . #include<stdio.h> int main()
I have a program below that tries to take input from the user and
My Perl program takes some text from a disk file as input, wraps it
I have a program Sample which takes input both from stdin and a non-standard
I'm implementing part of a Scala program that takes input strings of the form
My program has a main thread that takes command input from a user. Separately,
The below program is used to take the input of integers from a text
Given below is program for encrypting a string. I had taken this code from
I have wrote the code below which was taken from Java How to program
Below program contains two show() functions in parent and child classes, but first show()

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.