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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:12:22+00:00 2026-05-18T09:12:22+00:00

In my code, I have several pointers to pointers (e.g. float ** variables), and

  • 0

In my code, I have several pointers to pointers (e.g. float ** variables), and I seem to be having issues freeing up their memory so as to not cause memory leaks. Here is the code that I have written:

float *one, **part1, **part2;

one = malloc(sizeof(&one) * nx * nx);
part1 = malloc(sizeof(&part1) * nx);

if(one == NULL || part1 == NULL) {
 printf("Memory error.\n");
 exit(2);
}

for(k = 0; k < nx; k++)
 part1[k] = &one[k * nx];

one = malloc(sizeof(&one) * nx * nx);
part2 = malloc(sizeof(&part2) * nx);

if(one == NULL || part2 == NULL) {
 printf("Memory error.\n");
 exit(2);
}

for(k = 0; k < nx; k++)
 part2[k] = &one[k * nx];

... (Other code here)

for(k = 0; k < nx; k++) {
 free(part1[k]);
 free(part2[k]);
}

free(one);
free(part1);
free(part2);

This code runs through, does the calculations correctly, but then errors out in the free loop. It works for k = 0, but then when trying to free part1[1] and part2[1], it gives me the “glibc detected” error.

  • 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-18T09:12:22+00:00Added an answer on May 18, 2026 at 9:12 am

    First of all:

    one = malloc(sizeof(&one) * nx * nx);
    part1 = malloc(sizeof(&part1) * nx);
    

    These should be

    one = malloc(sizeof(*one) * nx * nx);
    part1 = malloc(sizeof(*part1) * nx);
    

    You want to allocate a bunch of floats and float *s, not float **s and float ***s

    Secondly, you make 4 allocations — you allocate one, and indices into it in part1,
    and allocate one again, (forgetting the old address), and indices into it in part2.

    This means you should have 4 free()s: part1, part2, and both chunks of memory to which one pointed. Because you overwrite the first one, you’ve lost that pointer and can’t directly free() it. Fortunately you did save that pointer in part1[0], and can use that to free() all the memory pointed to.

    Another (perhaps clearer and more idiomatic) option would be to allocate differently. Allocate part1, and then loop to allocate each part1[k], and the same for part2.

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

Sidebar

Related Questions

In my code I have several macros. Macro A is the main macro. Macro
I have several blocks of the following code that each use there own matrix.
i have the following jquery code. basically i will have several overlapped divs and
I'm working on a code base in which we have several configurable types. One
In a desktop application needing some serious re-factoring, I have several chunks of code
I have a block of code that basically intializes several classes, but they are
I have a regular control in my code with several items. <mx:List id=myList> <mx:String>Item
I have a block of JSP code that needs to be used in several
I have several like this in my HTML code: <input class=table type=checkbox name=interest[] value=finger
In a new piece of code I have several different classes that refer to

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.