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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:16+00:00 2026-05-13T14:45:16+00:00

i have a structure malloc()’d, and after using them, i want to free() it,

  • 0

i have a structure malloc()’d, and after using them, i want to free() it, but my program freezes out here. Can anyone tell me, what i am doing wrong?

Here is my code:

struct data  
{  
char *filename;  
char *size;  
};   
 //primarypcs is a long type variable
struct data *primary = (struct data *)malloc( primarypcs * sizeof( struct data ) );  
memset( primary, 0, sizeof(struct data *) * primarypcs );  
...
...
...
for ( i = 0; i < primarypcs; i++ )  
{
   free( primary[i].filename );  //<----my program freezes here
   free( primary[i].size );      //<----or here
}
free( primary );  

Thanks in advance!

kampi

EDIT:

How can i correctly malloc memory for filename and size?

EDIT2:

Sorry, but i was in a hurry, and i didn’t told you all the information that you need. Let me do it now 🙂 Basicly, i want create an application, which gets the file list of two given drives/folders and then compares them. I thought (and still do), that the easiest way is, when i store the filenames and their size in a structure like mentioned above. So i have to allocate memory dynamically (i think this what they call it) for filename and size and of corse for the structure too.

  • 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-13T14:45:16+00:00Added an answer on May 13, 2026 at 2:45 pm

    You are not presenting the entire code, where a lot of things can go wrong, but one error is already obvious. The line

    memset( primary, 0, sizeof(struct data *) * primarypcs );   
    

    is not doing what you think it is doing. It is not zeroing the entire array due to type mistake in sizeof. It was most likely supposed to be

    memset( primary, 0, sizeof(struct data) * primarypcs );   
    

    Note no * under sizeof. Because of this error, most of the pointers in your array contain garbage as their initial values. If you don’t set them to something meaningful in the omitted code, your calls to free will receive garbage arguments and fail.

    In general, to reduce the chance of such errors it is best to avoid mentioning type names in your program, except in declarations. Since your question is tagged C++ (even though it surely looks like C), it is not possible to get rid of type casts on malloc, but otherwise I’d say that the following looks better

    struct data *primary = (struct data *) malloc( primarypcs * sizeof *primary );   
    memset( primary, 0, primarypcs * sizeof *primary );   
    

    And, as a side note, if your code was intended to be C++, you could get the same result in a much more elegant, compact and portable way

    data *primary = new data[primarypcs]();
    

    Of course in this case you’ll have to deallocate the memory using the appropriate C++ functionality instead of free.

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

Sidebar

Ask A Question

Stats

  • Questions 340k
  • Answers 340k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Its not too hard to do. Make sure "Selection" is… May 14, 2026 at 4:43 am
  • Editorial Team
    Editorial Team added an answer Are you useing urlencode()? $email =urlencode($_POST['email']); Here are some code… May 14, 2026 at 4:43 am
  • Editorial Team
    Editorial Team added an answer Declare an array before and assign the values to it:… May 14, 2026 at 4:43 am

Related Questions

I have a structure: struct pkt_ { double x; double y; double alfa; double
I have a structure that has an array of pointers. I would like to
Friends In our C++ , Iam current using realloc method to resize the memory
I am a C beginner, and I am writing a very simple linked-list. I
I have a VxWorks application running on ARM uC. First let me summarize the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.