0

So I have a structure called fastarray which contains a poiner to another structure called token_det. My problem is trying to fill a char array inside the array of structs fails mid way through and gives a error message as "The exception unknown software exception (0x0000417) occured in the application at location 0x78b2ae6e". I tried increasing the size of the char array using malloc but the string concat function keeps failing after concatinating a few strings. Below is a example of the code:

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream.h>

using namespace std;

#define MAX_TOKENS 300000

struct token_det
{
    int token;

    std::string data;
    char mdepth[300];

};

typedef struct fastarray
{
    token_det *td;  //MAX_TOKENS
}FASTARRAY;


int main()
{
    printf("inside main\n");

    int lv_ret = 0;
    int count = 0;
    char log[50] = {""};
    int wtoken = 0;

    FASTARRAY *f_array = NULL;

    f_array = (FASTARRAY *)malloc(sizeof(FASTARRAY));
    f_array->td = NULL;
    f_array->td = (token_det *)malloc(MAX_TOKENS * sizeof(token_det));

    printf("after malloc\n");

    memset(f_array, 0, sizeof(f_array));
    memset(f_array->td, 0, sizeof(f_array->td));

    int x=0;
    while(x<=10000)
    {
        printf("inside while");
        f_array->td[x].data = "104,";

        f_array->td[x].data.append("stasimorphy");  
        f_array->td[x].data.append(",");

        f_array->td[x].data.append("psychognosy");
        f_array->td[x].data.append(",");

        f_array->td[x].data.append("whoever");
        f_array->td[x].data.append(",");

        x++;

        sprintf_s(log,sizeof(log),"Data for x-%d = %s\n",x,f_array->td[x].data);
        printf(log);
    }

    free(f_array->td);
    free(f_array);

    printf("after while\n");
    return 0;
}
Ghos3t
  • 174
  • 1
  • 14

1 Answers1

1

Explanation of what I was doing and why

When I tried to understand what you wanted to do there I've had no problem except for the parts in which you're using memset. With memset(f_array, 0, sizeof(f_array)); you're explicitly setting the f_array to point to 0 in the memory which was constantly throwing exceptions for me.

As I've never really been a friend of malloc I've been using C++ syntax as follows:

For allocating a single instance I'd use FASTARRAY *f_array = new fastarray;. You can read up on why using new instead of malloc is favorable in C++ here.

In the same way I've been using C++ syntax for allocating the dynamic array f_array->td = new token_det[MAX_TOKENS]; A Q&A about that topic for reference can be found here.

For filling the data string inside the dynamic array's struct I've been using the += syntax as it's easier to read, in my opinion. Accessing the element inside the f_array has been achieved using (*(f_array->td + x)).data += "stasimorphy";

You can try my solution online here.

Code Dump

I tried to change as little as possible to make it work.

#include <sstream>
#include <string>
#include <iostream>

using namespace std;

#define MAX_TOKENS 300000

struct token_det
{
    int token;

    std::string data;
    char mdepth[300];

};

typedef struct fastarray
{
    token_det *td;  //MAX_TOKENS
}FASTARRAY;


int main()
{
    std::cout << "inside main\n";

    int lv_ret = 0;
    int count = 0;
    char log[50] = { "" };
    int wtoken = 0;

    FASTARRAY *f_array = new fastarray;
    f_array->td = new token_det[MAX_TOKENS];

    std::cout << "after malloc\n";

    int x = 0;
    while (x <= 10000)
    {
        std::cout << "inside while";
        std::stringstream log;
        (*(f_array->td + x)).data = "104,";

        (*(f_array->td + x)).data += "stasimorphy";
        (*(f_array->td + x)).data += ",";

        (*(f_array->td + x)).data += "psychognosy";
        (*(f_array->td + x)).data += ",";

        (*(f_array->td + x)).data += "whoever";
        (*(f_array->td + x)).data += ",";

        log << "Data for x-" << x << " = " << (f_array->td + x)->data << std::endl;
        std::cout << log.str();

        x++;
    }

    delete[] f_array->td;
    free(f_array);

    std::cout << "after while\n";
    return 0;
}
Tornadowarnung
  • 368
  • 3
  • 11
  • 1
    Thank you, your code helped me find the error in my own solution. Apparently the size of the log char array which I was using to print the results was too small for the data and that was causing the problem. – Ghos3t Jan 23 '18 at 05:27