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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:05:43+00:00 2026-06-06T11:05:43+00:00

In an attempt to free up memory after a malloc command to prevent memory

  • 0

In an attempt to free up memory after a malloc command to prevent memory leaks, I am running into a heap corruption problem during runtime. My debug is telling me that my application is trying to write to memory after the end of the heap buffer has been reached. Here is what I have simplified to isolate my problem.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main(){

int i,j,n;
int temp[4];
int **a;

printf("Amount of intervals to be entered: ");
scanf("%d", &n);

//allocate memory for a 2d array of size (n x 4)
a = (int**) malloc(n*sizeof(int*));
for (i=0;i<4;i++){
    a[i] = (int*) malloc(4*sizeof(int));
}

if (!a){
    printf("malloc failed %d\n",__LINE__);
    exit(0);
}

printf("Please enter the intervals a line at a time followed by a return: \n");
for(i=0;i<n;i++){
    scanf("%d %d %d %d",&a[i][0], &a[i][1], &a[i][2], &a[i][3]);
}

for(i=0;i<n;i++){

    printf("%d, %d, %d, %d\n", a[i][0], a[i][1], a[i][2], a[i][3]);

}


// free up the allocated memory 


for (i=0;i<n;i++){
    free(a[i]);
}

free(a);




}

I’m not extremely familiar with allocating and de-allocating memory so I am at a loss as to what I should do.

  • 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-06T11:05:45+00:00Added an answer on June 6, 2026 at 11:05 am

    There may be other problems. But this at least is wrong:

    a = (int**) malloc(n*sizeof(int*));
    for (i=0;i<4;i++){                   <---- 4 should be n
        a[i] = (int*) malloc(4*sizeof(int));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First attempt to use this cool site - after searching for 2 hours: So
When I attempt to deserialize: { tags: {} } into: public static class Foo
When I insert a row into a table with 1000+ entries, and attempt to
I have a strange problem freeing allocated memory in my mpi program: Here is
I'm consistently receiving a memory warning right after pressing the Use button on the
I'm trying to experiment with malloc and free in assembly code (NASM, 64 bit).
My attempt to customize the method contains of ArrayList, I used Eclipse to generate
I attempt to use webservice return POCO class generated from entity data model as
The following is my attempt at a prepare statement. It is causing the page
In an attempt to achieve git nirvana, I'm spending the day learning how 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.