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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:34:44+00:00 2026-05-22T23:34:44+00:00

I just can’t figure out how to do a malloc . The following code

  • 0

I just can’t figure out how to do a malloc. The following code just types the first 5 lines and then stops, any help would be appreciated!

// Read query points from query file------------------------------
double **queryPoint;

token=(char*)malloc(40);
int qp_count=0;

i=0;
qp_count=0;
while(fgets(line,sizeof(line),queryFile)!=NULL)
{
    queryPoint=(double*)malloc(sizeof(double**));
    queryPoint[qp_count]=(double*)malloc(sizeof(double*)*2);

    printf("line:%s",line); 

    token = strtok(line," ,\t");        

    queryPoint[qp_count][0]=atof(token);        
    token = strtok(NULL, " ,\t");

    printf("l[%d]=%lf \n",qp_count,queryPoint[qp_count][0]);

    queryPoint[qp_count][1]=atof(token);
    token = strtok(NULL, " ,\t");
    printf("l[%d]=%lf \n",qp_count,queryPoint[qp_count][1]);

    qp_count++;
}

{
This is the form of the query file

9.85797 5.72533
9.58711 2.09899
2.28203 7.19344
4.49096 5.50094
6.05297 1.60751
6.19901 1.52312

}
…
30 lines total

  • 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-22T23:34:45+00:00Added an answer on May 22, 2026 at 11:34 pm

    Initialize at the beginning:

    double **queryPoint = 0;
    int qp_count = 0;
    

    For every line call:

    // call realloc to make the space for points larger by one element
    queryPoint = realloc(queryPoint, sizeof(double*)*(qp_count+1));
    
    // allocate space for the new point
    queryPoint[qp_count] = malloc(sizeof(double)*2);
    
    // increase the point count
    qp_count++;
    

    Note that you may use realloc to resize the space for lines. Also note that you should add error handling.

    To free the memory you need to call:

    for(int i=0;i<qp_count;i++)
        free(queryPoint[i]);
    
    free(queryPoint);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can not figure out the following code. int d = 5; float
I'm experiencing some craziness that I just can't figure out. I've created the following
I just can not figure this out, or find any docs. I have a
I just can't figure out how do I make sure an argument passed to
I'm new to regular expression and just can't seem to figure this out: '/^[A-Za-z0-9](?:.[A-Za-z0-9]+)$/'
I just can't seem to figure out how to make efficient and clean looking
I just can't figure this out, for some reason this: $string = #mainparent {
I just can't seem to figure this one out... As far as I know,
This ought to be simple but I just can't figure this one out: The
I just can't figure out how to write an equivalent interface in c#. Java:

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.