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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:33:32+00:00 2026-05-24T08:33:32+00:00

I have a problem with free() function in C linux. here is my sample

  • 0

I have a problem with free() function in C linux. here is my sample code:

typedef struct
{
    char index[2];
    char color[2];
    char duration[2];
} ps;

ps *_ps = NULL;

I need to allocate and free the above structure repeatedly using the following function:

ps * readMatrix(mmc_item_item_screen *_item_screen,int rows,int skip)
{
    if(_ps)
    {
        free(_ps);
        _ps=(ps *)NULL;
    }

    _ps=(ps *)calloc(rows-1,sizeof(ps));


memset(_ps,'\0',((rows-1)*sizeof(ps)));
    int i=0;
    for(i=0;i<rows;i++)
    {
        if((i+1)>=skip)
        {
            sprintf(_ps[i].index,"%d",i);
            sprintf(_ps[i].color,"%s",getCDKMatrixCell(_item_screen->matrix,i+2,1));
            sprintf(_ps[i].duration,"%s",getCDKMatrixCell(_item_screen->matrix,i+2,2));

        }
        else
        {
            sprintf(_ps[i].index,"%d",i);
            sprintf(_ps[i].color,"%s",getCDKMatrixCell(_item_screen->matrix,i+1,1));
            sprintf(_ps[i].duration,"%s",getCDKMatrixCell(_item_screen->matrix,i+1,2));

        }

    }

    return _ps;
}

main()
{
while(1)
....
_ps=readMatrix(_item_screen,rows,getCDKMatrixRow(_item_screen->matrix));
....
}

First time that I call the above function, _ps is NULL so free() is not invoked. but the second time(_ps already allocated), program crashes as soon as free() is called.

Can you help me with this?

(ps and _ps are both global)

Thank you.
Edit1 I put the whole function that manipulates _ps. and this is the only function working on _ps. could you help pllease?

  • 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-24T08:33:33+00:00Added an answer on May 24, 2026 at 8:33 am

    YOU ARE allocating rows-1 elements but accessing rows elements in your loop..
    so me because of that…
    one more thing if your _ps is global then why you are returning that..???

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

Sidebar

Related Questions

I've a weird problem with malloc. I have this typedefs: typedef struct buffer {
I actually seem to have problem with this (only on linux, described here with
I have this idea for a free backup application. The largest problem I need
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with fancybox. I want to write a function that will run
I'm stucked with a problem with Mono Interop in Linux. I have a native
I have a problem with the function replace . What I want to accomplish
First, the problem: I have several free projects, and as any software they contains
I have a function to convert integer to string .The function is char *
I have problem with Indy IdHttp Post method. Function CallRpc() compiled with Delphi 2007

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.