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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:55:27+00:00 2026-05-26T12:55:27+00:00

I am malloc ing an array of c strings. After releasing it, I get

  • 0

I am mallocing an array of c strings. After releasing it, I get the following error:

Assembler(87536) malloc: *** error for object 0x108500840: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Why is that? I am pretty sure I am doing the malloc correctly. I’m pretty experienced with memory management, but I am not sure why this is giving me an error. The array is should hold three strings, each of which is 2 characters long.

Here is how I am mallocing the array:

char **reg_store;
reg_store = malloc(3 * (sizeof(char*)));
if (reg_store == NULL) {
     fprintf(Out, "Out of memory\n");
     exit(1);
}

for (int i = 0; i < 3; i++) {
    reg_store[i] = malloc(2 * sizeof(char));
    if (reg_store[i] == NULL) {
          fprintf(Out, "Out of memory\n");
          exit(1);
    }
}

Here is how I am freeing it:

for (int i = 0; i < 3; i++) {
    free(reg_store[i]);
}
free(reg_store);

Here is what I have in between:

// Keeps a reference to which register has been parsed for storage

int count = 0;
char *reg = NULL;
char *inst_ptr // POINTS TO SOME STRING. EXAMPLE: $t2, $t1, $a0

while (1) {

    // Parses the string in inst_ptr with dollar, comma and space as a delimiter.
    reg = parse_token(inst_ptr, " $,\n", &inst_ptr, NULL);

    if (reg == NULL || *reg == '#') {
        break;
    }

    reg_store[count] = reg;
    count++;
    free(reg);
}

I am printing out reg after I call parse_token and it does print out correctly. I am also printing out reg_store[count] and it does also print out correctly.

  • 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-26T12:55:28+00:00Added an answer on May 26, 2026 at 12:55 pm

    Your problem is here:

    reg_store[count] = reg;
    free(reg);
    

    and later

    free(reg_store[i]);
    

    reg is already freed and you free it another time (not talking about the problems with using it later). to fix this replace

    reg_store[count] = reg;
    

    with

    strcpy(reg_store[count], reg);
    

    or as suggested in the comments, since you know its two charaters, its better to memcpy it:

    memcpy(reg_store[count], reg, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dual Search(8896,0xb014b000) malloc: *** error for object 0x5a1e0f0: pointer being freed was not allocated
I am trying to malloc and free a small array/table of single letter strings.
When using malloc, if it produces a core dump with the error: malloc(): memory
Possible Duplicates: Malloc a 3-Dimensional array in C? dynamic allocation/deallocation of 2D & 3D
I'm using malloc to make an error check of whether memory can be allocated
// the malloc style, which returns a pointer: struct Cat *newCat = malloc(sizeof(struct Cat));
I need help with malloc() inside another function . I'm passing a pointer and
If I use malloc in my code: int *x = malloc(sizeof(int)); I get this
Just got query regarding c malloc() function. I am read()ing x number of bytes
When malloc() fails, which would be the best way to handle the error? If

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.