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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:49:10+00:00 2026-06-10T11:49:10+00:00

I believe I am doing this right but wanted to make sure. In a

  • 0

I believe I am doing this right but wanted to make sure. In a program I am using two pointers that point to allocated memory returned by a function. When I finish using that memory I free() it, but then I use those same pointers to point at a new allocated memory space. Here is my program to give an example of what I mean (Commented to show my thought process):

int main(void)
{
    char *data, *url;
        int i = 1;

    while(i)
    {
        printf("Enter URL: ");
        if(!(url = getstr())) return 1;                                  //url now points to allocated memory from getstr();
        if(strlen(url) <= 0) i = 0;
        if(data = readsocket(url, 80, "http")) printf("\n%s\n\n", data); //data now points to allocated memory from readsocket();
        else printf("\n\n");
        free(url);                                                       //free allocated memory that url points to url
        free(data);                                                      //free allocated memory that data points to data
    }
    return 0;
}

Is this correct, or is there a better more generally preffered method of doing this? Or am I just completely doing it wrong?

  • 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-10T11:49:11+00:00Added an answer on June 10, 2026 at 11:49 am

    Assuming your functions getstr and readsocket malloc the memory internally then this is perfectly fine.

    My only suggestion is that it’s often helpful to allocate memory in the same scope that you free it, this often helps reason about when things need to be freed.

    For example:

    url = malloc(URL_MAX_LEN);
    if (!url) return 1;
    if (!getstr(url)) {
        free(url);
        return 1;
    }
    /* do something with url */
    free(url);
    

    It can be nice to do this using goto, if you have lots of objects and exit points.

    object1 = malloc(OBJECT1_SIZE);
    /* do work, if there's an error goto exit1 */
    object2 = malloc(OBJECT2_SIZE);
    /* do work, if there's an error goto exit2 */
    object3 = malloc(OBJECT3_SIZE);
    /* do work, if there's an error goto exit3 */
    exit3:
    free(object3);
    exit2:
    free(object2);
    exit1:
    free(object1);
    return 1;
    

    If your objects get more complex and require a lot of work to create and destroy then you can wrap malloc and free, but it’s critical you apply exactly the same rules to your create/destroy functions as you would to malloc/free to avoid leaks. As a matter of good practice you should always have a destroy to match a create, even if it just wraps free. It’s much harder to go wrong that way.

    struct x *create_x() {
        struct x *p = malloc(10);
        p->val1 = 7;
        return p;
    }
    void destroy_x(struct x *p) {
        free(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Believe I'm doing this correctly but it is not getting the stored cookie
Alright, so I'm about 90% sure that this isn't possible, but I thought I'd
I believe this is a fairly simple question but it is something I am
Now I am doing this in VB6 but I don't think it matters what
I have a program i frequently use that is made with .NET. This program
--EDIT-- I believe this is a valid question that may have multiple answers (as
I'm new to knockout and don't know if I'm doing this right at all
This is a general question, I believe it maybe a math question but it
Is this doing 4 threads on an array, and then assigning that array to
I believe I've set this up correctly. Can somebody see anything wrong with this

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.