0

I am newer to C and do some practice,in this example,i want construct a table which can contains alot of element which is Symbol type,but i don't konw how to write that part.I want to use malloc to allocate heap memory to Symbol and insert into the table(SymbolTable).

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

typedef struct {

    char *name;
    uint32_t addr;
}Symbol;

typedef struct {
    Symbol* tbl;
    uint32_t len;
    uint32_t cap;
    int mode;
} SymbolTable; /*this is the table i want to mantiply*/

SymbolTable* create_table(int mode) {
    SymbolTable* st = (SymbolTable*)malloc(sizeof(SymbolTable));
    if (st != NULL)
    {
        st->mode = mode;
        st->len = 0;
        return st;
    }
    printf("Memory allocation failed!\n");
    return NULL;
}

void free_table(SymbolTable* table) {
    int i;
    for (i = 0; i < table->len; ++i)
    {
        free(table->tbl[i].name);
        free(&(table->tbl[i]));
    }
    free(table);
}

int add_to_table(SymbolTable* table, const char* name, uint32_t addr) {
    if (addr % 4 != 0)
    {
        printf("Address alignment erron!\n");
        return -1;
    }
    int table_len = table->len;
    if (table->mode == 1)
    {
        int i;
        for (i = 0; i < table_len; ++i)
        {
            if (*((table->tbl[i]).name) == *name)
            {
                printf("Name existed!\n");
                return -1;
            }
            `I don't know how to inset element here`
        }
    }
    return 0;
}

int main()
{
    SymbolTable * st = create_table(0);
    add_to_table(st, "aaa", 4);
    add_to_table(st, "bb", 8);
    write_table(st, stdout);
    free_table(st);
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
joe lami
  • 38
  • 5
  • 2
    "I don't know how to write that part" is not a specific question. We won't just write the whole thing for you. Please explain more specifically what is preventing you from writing the code. – kaylum Apr 17 '17 at 08:23
  • Welcome to StackOverflow. Please take the tour stackoverflow.com/tour, learn asking good questions stackoverflow.com/help/how-to-ask. You might also want to read up on the concept of [linked list](https://en.wikipedia.org/wiki/Linked_list). Or were you aware of that concept and only asking for the code? In that case see the comment by @kaylum. – Yunnosch Apr 17 '17 at 09:52

3 Answers3

0

Firstly, I think you need to allocate *tbl and set cap (if he contain the max nb of cell in your table) in create_table().

Next, in add_to_table(), try to malloc(sizeof(struct symbol)) if (len < cap) and allocate memory for *name, and set up to your value (don't forget the \0 at the end of *name). Assign this to tbl[len] and do not forget to increment len.

Try to separate in little function, like int is_in_table(const char *name) who return the index or -1, or symbol new_symbol(const char *name, Uint32 addr) who create and set new symbol.

I hope that I have been useful for you :).

ghost
  • 23
  • 2
  • 6
0

There are few problems with your code as it is; also, I had to make a few assumptions since you didn't specify those details in your question:

  1. cap is the capacity of table->tbl, i.e. how much memory we have allocated
  2. When adding a new symbol, we should copy the string containing its name, rather than just assigning that pointer to our new Symbol entry.

You should also pick one coding style and stick to it (braces on same vs new line, T* ptr vs T *ptr etc). Finally, I've removed the cast in create_table; see Do I cast the result of malloc?

Here is a fixed version of your code; in add_to_table, if we don't have enough memory to add a new one, we double the capacity of our Symbol array (calling realloc every single time to add space for one more element would be wasteful). When we extend the capacity of our array, we must take care to set each name pointer to NULL, because if we don't and free_table is called when cap > len, we'd be trying to free an uninitialized pointer (whereas calling free on NULL is perfectly fine and does nothing).

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

typedef struct
{
    char* name;
    uint32_t addr;
} Symbol;

typedef struct
{
    Symbol* tbl;
    uint32_t len;
    uint32_t cap;
    int mode;
} SymbolTable; /*this is the table i want to mantiply*/

SymbolTable* create_table(int mode)
{
    SymbolTable* st = malloc(sizeof(SymbolTable));
    if (st != NULL)
    {
        st->tbl = NULL;
        st->mode = mode;
        st->len = 0;
        st->cap = 0;
        return st;
    }
    printf("Memory allocation failed!\n");
    return NULL;
}

void free_table(SymbolTable* table)
{
    int i;
    for (i = 0; i < table->cap; ++i)
    {
        free(table->tbl[i].name);
    }
    free(table->tbl);
    free(table);
}

int add_to_table(SymbolTable* table, const char* name, uint32_t addr)
{
    if (addr % 4 != 0)
    {
        printf("Address alignment erron!\n");
        return -1;
    }
    int table_len = table->len;
    if (table->mode == 1)
    {
        int i;
        for (i = 0; i < table_len; ++i)
        {
            if (!strcmp(table->tbl[i].name, name))
            {
                printf("Name existed!\n");
                return -1;
            }
        }

        if (table_len + 1 > table->cap)
        {
            // allocate more memory
            uint32_t new_cap = table->cap ? table->cap * 2 : 2;
            table->tbl = realloc(table->tbl, new_cap * sizeof(*table->tbl));
            if (table->tbl == NULL)
            {
                // handle the error
            }

            table->cap = new_cap;
            int i;
            for (i = table_len; i < new_cap; ++i)
            {
                table->tbl[i].name = NULL;
            }
        }

        uint32_t name_len = strlen(name);
        table->tbl[table_len].name = malloc(name_len + 1);
        strncpy(table->tbl[table_len].name, name, name_len);
        table->tbl[table_len].name[name_len] = '\0';

        table->tbl[table_len].addr = addr;
        table->len++;
    }

    return 0;
}

int main(void)
{
    SymbolTable*  st = create_table(1);
    add_to_table(st, "aaa", 4);
    add_to_table(st, "bb", 8);
    write_table(st, stdout);
    free_table(st);
}
Community
  • 1
  • 1
user4520
  • 3,401
  • 1
  • 27
  • 50
0

Use the concept of implementing Self-Referential structure. Giving hints:

typedef struct S{
    char *name;
    uint32_t addr;
    S* next;
}Symbol;

typedef struct {
    Symbol* tbl;
    uint32_t len;
    int mode;
} SymbolTable;

http://www.how2lab.com/programming/c/link-list1.php

self referential struct definition?

Possible Implementation

#include<stdio.h>
#include<stdlib.h> 
#include<stdint.h>
#include<string.h>
#include <stdio.h> 
typedef struct S{
    char *name;
    uint32_t addr;
    S* next;
}Symbol;

typedef struct {
    Symbol* tbl;
    uint32_t len;
    int mode;
} SymbolTable; /*this is the table I want to maltiply*/

SymbolTable* create_table(int mode) {
    SymbolTable* st = (SymbolTable*)malloc(sizeof(SymbolTable));
    if (st != NULL)
    {
        st->tbl = NULL;
        st->mode = mode;
        st->len = 0;
        return st;
    }
    printf("Memory allocation failed!\n");
    return NULL;
}

void free_table(SymbolTable* table) {
    free(table);
}

int add_to_table(SymbolTable* table,  char* name, uint32_t addr) {
    if (addr % 4 != 0)
    {
        printf("Address alignment erron!\n");
        return -1;
    }
    int table_len = table->len;
    if (table->mode == 1)
    {
        if (table_len == 0)
        {
            Symbol *t = (Symbol*)malloc(sizeof(Symbol));
            t->name = name;
            t->next = NULL;
            table->len++;
            table->tbl = t;
        }
        else
        {
            Symbol *t = table->tbl;
            while (t->next != NULL)
            {
                if (t->name == name)
                {
                    printf("Name existed!\n");
                    return -1;
                }
                t = t->next;
            }   
            if (t->name == name)
            {
                printf("Name existed!\n");
                return -1;
            }
            t->next = (Symbol*)malloc(sizeof(Symbol));
            table->len++;
            t->next->name = name;
            t->next->next = NULL;
        }
    }
    return 0;
}
void write_table(SymbolTable *st)
{
    Symbol *t = st->tbl;
    while (t != NULL)
    {
        printf("%s\n",t->name);
        t = t->next;
    }
    printf("\n");
}
int main()
{
    SymbolTable *st = create_table(0);
    st->mode = 1;// Table mode setting to 1 for importing next value to table.
    // You may implement it in your own way.
    add_to_table(st, "cc", 8);
    st->mode = 1;
    add_to_table(st, "bb", 8);
    st->mode = 1;
    write_table(st);
    free_table(st);
}
Community
  • 1
  • 1
Yasin shihab
  • 382
  • 1
  • 5