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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:30:46+00:00 2026-05-18T23:30:46+00:00

I have trouble deallocating memory that I allocated using malloc. The program runs fine

  • 0

I have trouble deallocating memory that I allocated using malloc. The program runs fine until it the part where it’s supposed to deallocate memory using free. Here the program freezes. So I was wondering what the problem could be since I’m just learning C. Syntactically the code seems correct so could it be that I need to delete all the stuff in that location before deallocating memory from that location or something else?

Here’s the code.

// Program to accept and print out five strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOOFSTRINGS 5
#define BUFFSIZE 255

int main()
{
    char buffer[BUFFSIZE];//buffer to temporarily store strings input by user
    char *arrayOfStrngs[NOOFSTRINGS];
    int i;

    for(i=0; i<NOOFSTRINGS; i++)
    {
        printf("Enter string %d:\n",(i+1));
        arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1));//calculates string length and allocates appropriate memory
        if( arrayOfStrngs[i] != NULL)//checking if memory allocation was successful
        {
            strcpy(arrayOfStrngs[i], buffer);//copies input string srom buffer to a storage loacation
        }
        else//prints error message and exits
        {
            printf("Debug: Dynamic memory allocation failed");
            exit (EXIT_FAILURE);
        }
    }

    printf("\nHere are the strings you typed in:\n");
    //outputting all the strings input by the user
    for(i=0; i<NOOFSTRINGS; i++)
    {
        puts(arrayOfStrngs[i]);
        printf("\n");
    }

    //Freeing up allocated memory
    for(i=0; i<NOOFSTRINGS; i++)
    {
        free(arrayOfStrngs[i]);
        if(arrayOfStrngs[i] != NULL)
        {
            printf("Debug: Memory deallocation failed");
            exit(EXIT_FAILURE);
        }
    }

    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-05-18T23:30:46+00:00Added an answer on May 18, 2026 at 11:30 pm

    You misuse strlen() and this results in buffer overrun:

    arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1)); //pointer from gets() is incremented and passed to strlen()  - that's wrong
    

    should be

    arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer))+1); //pointer from gets() is passed to strlen(), then returned value is incremented - correct
    

    also free() doesn’t change the pointer passed to it. So that

     char* originalValue = pointerToFree;
     free( pointerToFree ); 
     assert( pointerToFree == originalValue ); //condition will always hold true
    

    and so in your code freeing memory should just be

    //Freeing up allocated memory
    for(i=0; i<NOOFSTRINGS; i++)
    {
        free(arrayOfStrngs[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble using Perl grep() with a string that may contain chars that
I have trouble getting boost:thread to work. It runs fine when compiling without optimization:
I have trouble comparing 2 double in Excel VBA suppose that I have the
I have trouble with the following piece of code. When I go through with
I have trouble defining a trigger for a MySQL database. I want to change
I have no trouble building 1.35.0, as well as 1.36.0 on the timesys arm-gcc
I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
I am developing a small windows app, but have some trouble deciding whether to
Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how

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.