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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:33:00+00:00 2026-06-03T06:33:00+00:00

This is my code: #include <stdio.h> #include <stdlib.h> #include <string.h> void getinfo(unsigned int a,

  • 0

This is my code:

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

void getinfo(unsigned int a, unsigned int b, char **pStr);

int main(){
    unsigned int len_max = 8;
    unsigned int current_size = 0;
    current_size = len_max;
    char *host, *user;
    char *pStr = malloc(len_max);
    if(pStr == NULL){
        perror("\nMemory allocation\n");
        return EXIT_FAILURE;
    }
    printf("Inserisci hostname: ");
    getinfo(len_max, current_size, &pStr);
    if((host=malloc(strlen(pStr)+1 * sizeof(char))) == NULL) abort();
    strncpy(host, pStr, strlen(pStr)+1);
    printf("Inserisci username: ");
    getinfo(len_max, current_size, &pStr);
    if((user=malloc(strlen(pStr)+1 * sizeof(char))) == NULL) abort();
    strncpy(user, pStr, strlen(pStr)+1);
    printf("\nHostname: %s\nUsername: %s\n", host, user);
    free(pStr);
    free(host);
    free(user);
    return EXIT_SUCCESS;
}

void getinfo(unsigned int a, unsigned int b, char **pStr){
    unsigned int i = 0;
    int c = EOF;
    while((c = getchar()) != '\n'){
        (*pStr)[i++] = (char)c;
        if(i == b){
            b = i+a;
            if((*pStr = realloc(*pStr, b)) == NULL){
                perror("\nMemory allocation error\n");
                exit(EXIT_FAILURE);
            }
        }
    }
    (*pStr)[i]='\0';
}

The problem is that if realloc fails i have to exit (because i cannot allocate memory). But before exit there’s to free all the used pointers.
The problem is that if the function fails the first time there’s only 1 pointer that have to be freed (pStr).
But if it fails the second time there’s 2 pointers that have to be freed (pstr & user).
How can i fix it?

  • 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-03T06:33:01+00:00Added an answer on June 3, 2026 at 6:33 am

    As already noted, if you are going to exit, then all practical modern O/S will release the allocated memory before exit. It was not always thus; early versions of AmigaDOS, IIRC, did not reclaim allocated memory automatically until reboot.

    This is a simple case. There are more complex cases, such as you are parsing a file into memory and the 579th memory allocation fails, and you’d like to release the previous 578 memory allocations so that the user can try again.

    In such cases, you have to keep a record of each memory allocation that is relevant (which may itself require some memory allocation — though if you’re parsing a file, you probably have a master structure which contains the complete description) and then release all the allocated data.

    In your example, if this was not a main() function and if you did not abort on memory allocation error, then you would need to ensure that the three allocated pointers are released on exit from the function. The standard tricks for that include:

    1. Initialize the pointers to 0 so they can be freed reliably:

      char *host = 0;
      char *user = 0;
      
    2. When using realloc(), do not assign the result to the expression passed as the first parameter:

      Do NOT do:

      ptr = realloc(ptr, newsize);
      

      If (when) ptr is the only reference to the allocated memory and the reallocation fails, you’ve just leaked memory; there is no way to release the memory that is still allocated to you.

      Use:

      void *newptr = realloc(oldptr, newsize);
      if (newptr == 0)
          ...recover from memory allocation failure
      oldptr = newptr;
      

      The trouble with the simpler version is that you’ve just thrown away the only reference to the allocated memory. (Note that your code falls into the dangerous/leaking pattern).

    3. Note that pretty much every function that acquires resources must either release the acquired resource before returning, or make the resource available to some other part of the program so that the other part can release the resource when it is done with it.

      The ‘make available’ operation might be returning the acquired resource (think of it as memory, but it could be a file descriptor, directory stream, or any of a large number of other allocated resources) to the calling function, or storing it in a structure that was passed to the current function, or copying it to a global or (file) static variable, or even stashing it in a (function) static variable so if the function is called again, it has some resource available on entry.

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

Sidebar

Related Questions

This is my code: #include <stdio.h> #include <stdlib.h> void getinfo(unsigned int a, unsigned int
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
My code #include <stdio.h> #include <stdlib.h> #include <ctype.h> void getData(short int *number, char *string)
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this very simple code: #include <stdio.h> #include <stdlib.h> int maxArr(int *arr) {
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
Is this code standard conforming ? #include <stdio.h> #include <cstdio> int main() { FILE
I wrote this code in C: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
I have a piece of code shown below #include <stdio.h> #include <stdlib.h> void Advance_String(char

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.