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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:10:48+00:00 2026-05-15T08:10:48+00:00

I have recently started working on my master thesis in C that I haven’t

  • 0

I have recently started working on my master thesis in C that I haven’t used in quite a long time. Being used to Java, I’m now facing all kinds of problems all the time. I hope someone can help me with the following one, since I’ve been struggling with it for the past two days.

So I have a really basic model of a database: tables, tuples, attributes and I’m trying to load some data into this structure. Following are the definitions:

typedef struct attribute
{
    int type;
    char * name;
    void * value;
} attribute;

typedef struct tuple
{
    int tuple_id;
    int attribute_count;
    attribute * attributes;
} tuple;

typedef struct table
{
    char * name;
    int row_count;
    tuple * tuples;
} table;

Data is coming from a file with inserts (generated for the Wisconsin benchmark), which I’m parsing. I have only integer or string values. A sample row would look like:

insert into table values (9205, 541, 1, 1, 5, 5, 5, 5, 0, 1, 9205, 10, 11, 'HHHHHHH', 'HHHHHHH', 'HHHHHHH');

I’ve “managed” to load and parse the data and also to assign it. However, the assignment bit is buggy, since all values point to the same memory location, i.e. all rows look identical after I’ve loaded the data. Here is what I do:

char value[10]; // assuming no value is longer than 10 chars
int i, j, k;

table * data = (table*) malloc(sizeof(data));
data->name = "table";
data->row_count = number_of_lines;
data->tuples = (tuple*) malloc(number_of_lines*sizeof(tuple));

tuple* current_tuple;

for(i=0; i<number_of_lines; i++)
{
    current_tuple = &data->tuples[i];
    current_tuple->tuple_id = i;
    current_tuple->attribute_count = 16; // static in our system
    current_tuple->attributes = (attribute*) malloc(16*sizeof(attribute));

    for(k = 0; k < 16; k++)
    {
        current_tuple->attributes[k].name = attribute_names[k];

        // for int values:
        current_tuple->attributes[k].type = DB_ATT_TYPE_INT;
        // write data into value-field
        int v = atoi(value);
        current_tuple->attributes[k].value = &v;

        // for string values:
        current_tuple->attributes[k].type = DB_ATT_TYPE_STRING;
        current_tuple->attributes[k].value = value;
    }

    // ...
}

While I am perfectly aware, why this is not working, I can’t figure out how to get it working. I’ve tried following things, none of which worked:

 memcpy(current_tuple->attributes[k].value, &v, sizeof(int));

This results in a bad access error. Same for the following code (since I’m not quite sure which one would be the correct usage):

 memcpy(current_tuple->attributes[k].value, &v, 1);

Not even sure if memcpy is what I need here…

Also I’ve tried allocating memory, by doing something like:

 current_tuple->attributes[k].value = (int *) malloc(sizeof(int));

only to get “malloc: *** error for object 0x100108e98: incorrect checksum for freed object – object was probably modified after being freed.” As far as I understand this error, memory has already been allocated for this object, but I don’t see where this happened. Doesn’t the malloc(sizeof(attribute)) only allocate the memory needed to store an integer and two pointers (i.e. not the memory those pointers point to)?

Any help would be greatly appreciated!

Regards,
Vassil

  • 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-15T08:10:49+00:00Added an answer on May 15, 2026 at 8:10 am

    So, you have several things going on here. First, don’t cast the return value of malloc in the C programming language. Next, this is going to cause troubles:

    int v = atoi(value);
    current_tuple->attributes[k].value = &v;
    

    You are having value point to memory allocated on the stack. That’s bad if you access it after that current “v” has gone out of scope.

    Also, you are not using any branching condition to determine if the value should be string or int. Therefore, you will end up with memory leaks, assuming you assigned things properly as mentioned before. I suspect this is partly because it is just an example.

    Check the return value of malloc. You can create a wrapper function to do this for you. Also, you may want to do some better logging.

    Essentially, you need to become more familiar with how pointers work in C and the difference between allocating on the heap and allocating on the stack. Stack or automatic variables go out of scope. When you malloc it stays around forever until you get rid of it. Don’t ever set a pointer equal to the memory location of something allocated on the stack unless you really mean to do that (again, your example with “v”, that memory address will be invalid as soon as that particular iteration of the loop is finished, the worst case here is that it works when you test it and you don’t notice the error.

    Additionally, “//” is not ANSI-C89 style comment. Use “/” and “/”

    I made a couple changes; I can’t guarantee it will work now as I haven’t tested it obviously. However, I recommend reading the C Programming Language

    char value[10]; /* assuming no value is longer than 10 chars */
    int i, j, k;
    
    table * data = malloc(sizeof(table));
         if(!data)
             exit(1); /* error */
    
    data->name = "table";
    data->row_count = number_of_lines;
    data->tuples = malloc(number_of_lines*sizeof(tuple));
         if(!data->tuples)
             exit(1); /* error */
    
    tuple* current_tuple;
    
    for(i=0; i<number_of_lines; i++)
    {
        current_tuple = &data->tuples[i];
        current_tuple->tuple_id = i;
        current_tuple->attribute_count = 16; /* static in our system */
        current_tuple->attributes = malloc(16*sizeof(attribute));
    
        for(k = 0; k < 16; k++)
        {
            current_tuple->attributes[k].name = attribute_names[k];
    
                            if(k % 2)
                            {
                   /* for int values:*/
                   current_tuple->attributes[k].type = DB_ATT_TYPE_INT;
                   /* write data into value-field */
                                   current_tuple->attributes[k].value = malloc(sizeof(int));
                                   if(!current_tuple->attributes[k].value)
                                   {
                                        exit(1); /* error */
                                   }    
                   *current_tuple->attributes[k].value = atoi(value);
    
                            }
                            else
                            {
                   /* for string values: */
                   current_tuple->attributes[k].type = DB_ATT_TYPE_STRING;
                                   current_tuple->attributes[k].value = malloc(strlen(value) +1);
                                   if(!current_tuple->attributes[k].value)
                                   {
                                        exit(1); /* error */
                                   }
                   strcpy(current_tuple->attributes[k].value, value);
                            }
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been a long time user of R and have recently started working with
I have recently started working on a very large C++ project that, after completing
I have recently started working on an app which has both Java and native
I have a project that I have recently started working on seriously but had
I'm very used to working in WPF, but I have recently started building websites
I have recently started working on a legacy application that has most of its
I have recently started working with Unified Communication Managed API 2.0 (UCMA) and Office
I have only recently started working with the MVC approach, so I suppose this
I have recently started using Vim as my text editor and am currently working
I have recently started learning F#, and this is the first time I've ever

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.