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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:41:36+00:00 2026-05-31T20:41:36+00:00

I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*

  • 0

I have declared the following struct:

typedef struct _RECOGNITIONRESULT {
        int begin_time_ms, end_time_ms;
        char* word;
} RECOGNITIONRESULT;

There is a method that creates an array of RECOGNITIONRESULT and fills it (for test purposes only):

void test_realloc(RECOGNITIONRESULT** p, int count){
    int i;
    *p = (RECOGNITIONRESULT *)realloc(*p, count * sizeof(RECOGNITIONRESULT));
    for (i = 0; i < count; i++){
        (*p)[i].begin_time_ms = 2*i;
        (*p)[i].end_time_ms = 2*i+1;
        (*p)[i].word=(char *) malloc ( (strlen("hello"+1) * sizeof(char ) ));
        strcpy((*p)[i].word,"hello");
    }
}

The method to free memory is:

void free_realloc(RECOGNITIONRESULT* p, int count){
    int i = 0;
    if(p != NULL){
        if (count > 0){
            for (i = 0; i < count; i++){
                free(p[i].word);      //THE PROBLEM IS HERE.
            }
        }
        free(p);
    }
}

The main method calls those methods like this:

int main(int argc, char** argv)
{
    int count = 10;
    RECOGNITIONRESULT *p = NULL;
    test_realloc(&p,count);
    free_realloc(p,count);
    return 0;
}

Then if I try to free the memory allocated for “word”, I get the following error:

HEAP CORRUPTION DETECTED: after normal block (#63) at 0x003D31D8.
CRT detected that the application wrote to memory after end of heap buffer.

Using the debugger I’ve discovered that the crash occurs when calling free(p[i].word);

What am I doing wrong? How can I free he memory for the strings?

  • 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-31T20:41:37+00:00Added an answer on May 31, 2026 at 8:41 pm

    The problem is in your allocation of memory for word. strlen("hello"+1) should be strlen("hello")+1.

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

Sidebar

Related Questions

If I have the following: typedef struct _MY_STRUCT { int a; float b; }
I have a project that requires the following. Four arrays will be declared in
I have a struct that's defined with a large number of vanilla char* pointers,
Suppose I have the following struct and function returning a pointer: typedef struct {
I have the following code: int main(void) { struct { int x; } a,
I have been working on some code that is similar to the following: typedef
I have a struct that is declared on the stack. Here is what the
I have the following variant from the boost lib: typedef boost::variant<int, float, double, long,
Say I have a struct declared like the following: public struct Test { public
In following program . I have one doubt. I have declared one global variable

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.