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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:29:26+00:00 2026-05-26T04:29:26+00:00

EDIT: I have changed my program according to suggestions people have made but I

  • 0

EDIT: I have changed my program according to suggestions people have made but I am unable to fix memory leaks. Also, I need to free them without using argc, so i need to somehow keep track of the array length so I marked the last element as null.

Currently I’m writing a C program that copies the command line arguments into a dynamically allocated array. My code looks like:

char **array;                                                                                                                                                                                     
int j;                                                                                                                                                                                        

array = malloc(sizeof(char*) * (argc + 1));                                                                                                                                                       
int i;                                                                                                                                                                                            
int index = 0;                                                                                                                                                                                    

for(i = 0; i < (argc); i++){                                                                                                                                                                      
    int length = strlen(*(argv + i));                                                                                                                                                             
    array[i] = malloc((length + 1) * sizeof(char));                                                                                                                                                                                                                                                                                                                                    
        // Cycle through all the chars and copy them in one by one                                                                                                                                
    for(j = 0; j <= length; j++){                                                                                                                                                                 
        array[i][j] = toupper(argv[i][j]);                                                                                                                                                        
    }                                                                                                                                                                                             
}      
array[i + 1] = NULL;                                                                                                                                                                                           

return array;      

Later, I try to free the memory:

char** array_copy = array;
while(*array_copy != NULL){
    free(*array_copy++);
}
free(*array_copy) // free the null at the end
free(array); 

However, I still get memory leaks. I’m not quite sure what I’m doing wrong. If anyone could give me tips that would be great.

Thank!

  • 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-26T04:29:26+00:00Added an answer on May 26, 2026 at 4:29 am

    Your last line free(array) isn’t freeing your original malloc, because you’ve incremented array as you are freeing its contents.

    Also (as others point out):

    • your loop to free the array contents is checking for non-zero, but you don’t ensure that the elements are zero to begin with.

    • You allocate argc+1 elements in array, when you only need argc.

    • *(argv + i) is the same as argv[i].

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

Sidebar

Related Questions

EDIT: I have changed the question as it was too localized and gained negative
EDIT: I have fixed all but two warnings now, so thank you all for
EDIT: solved, I know how but I don't understand why. I changed variables declaration
I'm in a trouble trying to fix some problem.. I have a program with
EDIT: I have a table with 3 rows like so. ID NAME REV 1
Edit: I have solved this by myself. See my answer below I have set
EDIT: I have submitted a bug report and Microsoft have acknowledge that it is
EDIT: I have edited my post... Working on a project (c#), I have a
EDIT i have something like this in a file: imagecolor=0 arrayimagecolorcopy=0 arrayimagecolorcopy3d=0 when i
EDIT: I have realized the source of my problem. I only have count information

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.