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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:43:28+00:00 2026-06-09T00:43:28+00:00

I wrote the following code today to experiment with returning pointers to allocated memory.

  • 0

I wrote the following code today to experiment with returning pointers to allocated memory.
The program works fine but I do have a few questions:

  1. To allocate the memory for the return value of rmch I use realloc. I understand the function and what it does for the most part I’m just quite not sure what the point of (char *) in the line ret = (char *) realloc(ret, c * sizeof(char)); I understand that realloc(ret, c * sizeof(char)); resizes the allocated memory to #c chars, but what does the (char *) part do?

  2. I do not free the allocated memory that ret points to anywhere, but I do free a pointer to ret which is called in my main function. What is happening to the allocated memory? If it is not being freed how would I go about freeing it?

Code

#include <stdio.h>
#include <stdlib.h>
char* rmch(char *str, char ch)
{
    char *ret = NULL;
    int c = 0, i;

    for(i = 0; str[i] != '\0'; i++)
    {
        if(str[i] != ch)
        {
            c++;
            ret = (char *) realloc(ret, c * sizeof(char));
            ret[c - 1] = str[i];
        }
    }
    ret[c] = '\0';
    return ret;
}

int main(void)
{
    char *foo = rmch("f o o", ' ');

    printf("%s", foo);
    free(foo);
    return 0;
}
  • 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-09T00:43:29+00:00Added an answer on June 9, 2026 at 12:43 am

    Freeing Foo in main will free the ret you’ve realloc’d in rmch.

    The reason being is free() goes to the address specified by the pointer which is returned by rmch.

    Also, as you have tagged this post with the “C” tag, you should never cast the return value of allocations. void *'s are automagically, implicitly promoted to whatever they are stored in, provided they were given the correct byte sizes during allocation.

    As an aside, you should never directly store any allocated memory directly into the pointer you are using/will be using as this can lead to memory leaks if the pointer returned is NULL and you were still pointing to old memory.

    Better to do this:

    temp = realloc();
    if( temp == NULL)
    {
         printf("realloc failed to reallocated memory!");
         return NULL;
    }
    ret = temp;
    

    And then you would need to check for a NULL return in your main as well.

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

Sidebar

Related Questions

i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code to select text from database,but when i echo the
i wrote following code to do some of a specifice column of gridview.. but
I wrote the following code for text file encryption and decryption. The encryption process
if wrote the following code and do not understand why the trace returns false:
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32
I wrote the following code to count the node of an XML file: private
I wrote the following code to rotate a cube on x axis an entire

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.