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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:40:45+00:00 2026-06-17T22:40:45+00:00

I have to safely free an array: char** a; It’s like a string list.

  • 0

I have to safely free an array: char** a; It’s like a string list. I know how many char* I have in it. But I have trouble to release ALL the memory. Is there like a function that I can use like to free 20 bytes ? I tried:

for (int i = 0; i < length; i++)
    if (a[i] != null)
        free(a[i]); // some of a[i] ARE null, non-null have different sizes
free(a); // crashes here

but I get runtime errors with asm debugging.
Every thing in a has been malloced. For a I malloced 5 strings (each pointer 4 bytes) -> 20 bytes. How can I free the whole char** ?

  • 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-17T22:40:46+00:00Added an answer on June 17, 2026 at 10:40 pm

    You can’t free 20 bytes unless you allocated 20 bytes. You can only free a block. The size of that block is specified at allocation time. For each block allocated, you need a separate de-allocation.

    You can try to change the size of a block by using realloc but that’s not deleting an arbitrary part of that block.

    If both the array and the indicvidual items in the array have been allocated using malloc, then your approach is correct. Free each of the elements, then free the array:

    char **arr = malloc (10 * sizeof (char*));
    if (arr != NULL)
        for (int i = 0; i < 10; i++)
            arr[i] = malloc (50 + i * 10); // sizes 50, 60, 70, ..., 140
    
    // Use the ten X-character arrays here
    //     (other than NULL ones from malloc failures, of course).
    
    if (arr != NULL) {
        for (int i = 0; i < 10; i++)
            free (arr[i]);           // Okay to free (NULL), size doesn't matter
        free (arr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know if i can safely use normal string or I have
I have rendered one of my controls into a string. I want to safely
When I have a cursor, I know I can safely execute a query as
Possible Duplicate: Can php variable hold harmfull code safely? Let's say I have array
I have a string like this namespace.fun1.fun2.fun3 passed from the client. It's telling the
I would like to have a service that stores a users password safely while
If I have a vector in C++, I know I can safely pass it
Suppose I have an array of data, can 2 threads safely write to different
I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=

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.