0

I am writing a program that uses linked lists to save information, this is the definition of the struct in a header file.

structures.h:

typedef struct node* symbolPtr;

/* this will represent a member in the symbol table */
typedef struct node {
    char name[32];
    int address;
    
    symbolPtr next;
} Symbol;

And I noticed that the address of the 'address' field is the same as the address of the 'name' field in the next node, and I don't understand why.

This is the main file assembler.c:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structures.h"


int firstRound(FILE*, symbolPtr, int*, int*);

int main() {
    FILE *file;
    int i;
    int *IC, *DC;
    symbolPtr smblTable;
    
    IC = malloc(sizeof(int));
    DC = malloc(sizeof(int));
    
    smblTable = malloc(sizeof(Symbol));
    
    smblTable->name[0] = '\0';
    smblTable->next = NULL;
    
    
    file = fopen("a.as", "r");
        
    

    firstRound(file, smblTable, IC, DC);
    
    free(IC);
    free(DC);
    free(smblTable);
    return 0;
}

And this is the file where the problem happens, firstMove.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "structures.h"

static int addToSmblTable(char*, symbolPtr, int);
static void printAd(symbolPtr smblTable);

int firstRound(FILE *file, symbolPtr smblTable, int *IC, int *DC) {
    char checker[32];
    
    *IC = 100, *DC = 0;

    /* this will go until we reach the end of the file */
    for (; !feof(file);) {
        fscanf(file, "%s", checker);
        
        addToSmblTable(checker, smblTable, *DC);//this saves the value of *DC in the node
        (*DC)++;
    }
    
    printAd(smblTable);// prints all the values of the linked list at the end
    
    return 0;
}

static void printAd(symbolPtr smblTable) {
    symbolPtr node;
    
    for (node = smblTable; node->next; node = node->next) {
        printf("\n%s:   %p  %p  %d\n", node->name, node->next->name, &(node->address), node->address);
//prints the string, the address of the next string and the address of the current 'address' field.
    }
}


static symbolPtr getLastNode(char *, symbolPtr);


static int addToSmblTable(char *symbol, symbolPtr table, int address) {
    symbolPtr node;
    
    
    
    node = getLastNode(symbol, table);//the last node
    
    if (node->name[0] != '\0') {
        node->next = malloc(sizeof(symbol));
        node = node->next;
    }
    
    /* saving the symbol */
    
    strcpy(node->name, symbol);//here the address of the last node is being changed
    node->address = address;
    
    
    
    node->next = NULL;
    
    
    return 0;
}


static symbolPtr getLastNode(char *symbol, symbolPtr table) {
    symbolPtr node;
    
    /* going over the symbol table and checking for the symbol */
    node = table; 
    while (node->next) {
        node = node->next;
    }
    
    return node;
}

This is the makefile:

test: assembler.o firstMove.o syntax.h structures.h
    gcc -g -ansi -Wall -pedantic assembler.o firstMove.o -o test

assembler.o: assembler.c structures.h
    gcc -c -g -ansi -Wall -pedantic assembler.c -o assembler.o

firstMove.o: firstMove.c structures.h
    gcc -c -g -ansi -Wall -pedantic firstMove.c -o firstMove.o

lets say this is a.as:

;file

;sample

.entry

.extern 

STR:

MAIN:

LOOP:

la

jmp

Next:

LIST:

bgt

la

sw

bne

call

jmp

la

.extern

.dh

K:

END:

.entry

Then this is the output:

In file a.as:

;file:  0x5566c6197920  0x5566c6196300  0

;sample:    0x5566c6197940  0x5566c6197940  1953391918

.entry: 0x5566c6197960  0x5566c6197960  1954047278

.extern:    0x5566c6197980  0x5566c6197980  978474067

STR::   0x5566c61979a0  0x5566c61979a0  1313423693

MAIN::  0x5566c61979c0  0x5566c61979c0  1347374924

LOOP::  0x5566c61979e0  0x5566c61979e0  24940

la: 0x5566c6197a00  0x5566c6197a00  7368042

jmp:    0x5566c6197a20  0x5566c6197a20  1954047310

Next::  0x5566c6197a40  0x5566c6197a40  1414744396

LIST::  0x5566c6197a60  0x5566c6197a60  7628642

bgt:    0x5566c6197a80  0x5566c6197a80  24940

la: 0x5566c6197aa0  0x5566c6197aa0  30579

sw: 0x5566c6197ac0  0x5566c6197ac0  6647394

bne:    0x5566c6197ae0  0x5566c6197ae0  1819042147

call:   0x5566c6197b00  0x5566c6197b00  7368042

jmp:    0x5566c6197b20  0x5566c6197b20  24940

la: 0x5566c6197b40  0x5566c6197b40  1954047278

.extern:    0x5566c6197b60  0x5566c6197b60  6841390

.dh:    0x5566c6197b80  0x5566c6197b80  14923

K:: 0x5566c6197ba0  0x5566c6197ba0  977555013

END::   0x5566c6197bc0  0x5566c6197bc0  1953391918

.entry: 0x5566c6197be0  0x5566c6197be0  1953391918

And you can see that the address of each 'address' field is the same as the address of the 'name' field in the next node.

Why is that? Am I not allocating the struct memory correctly?

1 Answers1

1

Consider what this is doing

node->next = malloc(sizeof(symbol));

How many bytes is allocated here? What is symbol ?

Okay:

static int addToSmblTable(char *symbol, symbolPtr table, int address) {
    symbolPtr node;
    
    node = getLastNode(symbol, table);//the last node
    
    if (node->name[0] != '\0') {
        node->next = malloc(sizeof(symbol));

so symbol is a char*. Are you sure that you want to allocate memory for a char pointer?

Could this be a simple typo? symbol -> Symbol

A better way would be:

node->next = malloc(sizeof *(node->next));
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63