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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:40:26+00:00 2026-06-11T15:40:26+00:00

I’ve a long version of a program that uses this code and a short

  • 0

I’ve a long version of a program that uses this code and a short version. This is the short version and oddly, this code runs perfectly fine in short version program, but because I get an access violation in the larger version, I get the feeling something is wrong. Does anyone see anything terribly wrong (that might lead to data corruption and/or access violation errors) with the following code? Or is this alright?

char strlist[5][11] = {
    { "file01.txt" },
    { "file02.txt" },
    { "file03.txt" },
    { "file04.txt" },
    { "file05.txt" }
};

typedef struct DYNAMEM_DATA {
    char *string;
    int   strlen;
} DYNAMEM_DATA;

typedef struct DYNAMEM_STRUCT {
    struct DYNAMEM_DATA **data;
    int           num_elements;
} DYNAMEM_STRUCT;

DYNAMEM_STRUCT *create_dynamem_struct(int num_elements, char *strlist)
{
    DYNAMEM_STRUCT *ds = (DYNAMEM_STRUCT *)calloc(1, sizeof(DYNAMEM_STRUCT));
    wchar_t wstring[128];
    char len[3];
    int i;

    ds->data = (DYNAMEM_DATA **)calloc(num_elements, sizeof(DYNAMEM_DATA *));
    ds->num_elements = num_elements;

    for(i = 0; i < num_elements; i++) {
        ds->data[i] = (DYNAMEM_DATA *)calloc(1, sizeof(DYNAMEM_DATA));
        ds->data[i]->string = (char *)calloc(1, strlen(&strlist[i*11])+5);
        ds->data[i]->strlen = strlen(&strlist[i*11]);
        sprintf(ds->data[i]->string, "%s, %d", &strlist[i*11], ds->data[i]->strlen);
        mbstowcs(wstring, ds->data[i]->string, 128);
        MessageBox(NULL, wstring, TEXT("Error"), MB_OK);
    }

    return ds;
}
  • 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-11T15:40:28+00:00Added an answer on June 11, 2026 at 3:40 pm

    This code runs, produces 5 ‘Error’ messages (which is to be expected), and doesn’t leak any memory. Running valgrind (3.7.0) on Mac OS X 10.7.4 (using GCC 4.7.1), it is given a clean bill of health.

    The problem is not, apparently, in this version of the code.

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <wchar.h>
    
    char strlist[5][11] = {
        { "file01.txt" },
        { "file02.txt" },
        { "file03.txt" },
        { "file04.txt" },
        { "file05.txt" }
    };
    
    typedef struct DYNAMEM_DATA {
        char *string;
        int   strlen;
    } DYNAMEM_DATA;
    
    typedef struct DYNAMEM_STRUCT {
        struct DYNAMEM_DATA **data;
        int           num_elements;
    } DYNAMEM_STRUCT;
    
    enum { MB_OK = 0 };
    static void destroy_dynamem_data(DYNAMEM_DATA *dd)
    {
        free(dd->string);
        free(dd);
    }
    static void destroy_dynamem_struct(DYNAMEM_STRUCT *ds)
    {
        for (int i = 0; i < ds->num_elements; i++)
            destroy_dynamem_data(ds->data[i]);
        free(ds->data);
        free(ds);
    }
    static void MessageBox(const void *null, const wchar_t *wcs, const char *ncs, int status)
    {
        if (null == 0 || status == MB_OK)
            fprintf(stderr, "%s\n", ncs);
        else
            fwprintf(stderr, L"%s\n", wcs);
    }
    static const char *TEXT(const char *arg) { return arg; }
    
    extern DYNAMEM_STRUCT *create_dynamem_struct(int num_elements, char *strlist);
    
    DYNAMEM_STRUCT *create_dynamem_struct(int num_elements, char *strlist)
    {
        DYNAMEM_STRUCT *ds = (DYNAMEM_STRUCT *)calloc(1, sizeof(DYNAMEM_STRUCT));
        wchar_t wstring[128];
        //char len[3];
        int i;
    
        ds->data = (DYNAMEM_DATA **)calloc(num_elements, sizeof(DYNAMEM_DATA *));
        ds->num_elements = num_elements;
    
        for(i = 0; i < num_elements; i++) {
            ds->data[i] = (DYNAMEM_DATA *)calloc(1, sizeof(DYNAMEM_DATA));
            ds->data[i]->string = (char *)calloc(1, strlen(&strlist[i*11])+5);
            ds->data[i]->strlen = strlen(&strlist[i*11]);
            sprintf(ds->data[i]->string, "%s, %d", &strlist[i*11], ds->data[i]->strlen);
            mbstowcs(wstring, ds->data[i]->string, 128);
            MessageBox(NULL, wstring, TEXT("Error"), MB_OK);
        }
    
        return ds;
    }
    
    int main(void)
    {
        DYNAMEM_STRUCT *ds = create_dynamem_struct(5, strlist[0]); 
        destroy_dynamem_struct(ds);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.