R
R
Razor982019-01-28 14:25:24
C++ / C#
Razor98, 2019-01-28 14:25:24

Where am I losing my memory?

Hello!
In general, I’m just starting my acquaintance with C and have recently dealt with structures ...
Well, now I’m trying to deal with lists
: he does not see in memory. So the problem is in the element itself.
Could someone explain to me then why I'm adding the list item incorrectly?
It can be seen that the code is educational, so it’s simple: where didn’t you finish your materiel?
I present the code in its entirety to make it clearer...

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


/* Определяем элемент списка */
typedef struct list_node {
    struct list_node *next;
    void *data;
    void *name;
    void *surname;
} list_node_t;

/* Определяем сам список */
typedef struct list {
    /* 
     * Размер списка хранить не обязательно, 
     * он нужен для упрощения работы 
     */
    int size;
    /* начало списка */
    list_node_t *head;
    /* конец списка */
    list_node_t *tail;
} list_t;

/* Инициализация массива */
list_t * create_list(void)
{
    list_t *lt = malloc(sizeof(list_t));

    lt->size = 0;
    lt->head = NULL;
    lt->tail = lt->head;

    return lt;
}

/* Добавляем элемент в начало списка */
void list_push(list_t *lt, void * data, void * name, void * surname)
{
    list_node_t * node = malloc(sizeof(list_node_t));
    node->data = data;
    node->name = name;
    node->surname = surname;    
    node->next = lt->head;

    lt->head = node;
    lt->size += 1;
}

/* Извлекаем элемент из начала списка */
void * list_pop(list_t *lt)
{
    if(lt->size == 0){
        /* Список пуст */
        return NULL;
    }

    list_node_t *node = lt->head;
    void * ret_val_I = node->data;
    void * ret_val_II = node->name;
    void * ret_val_III = node->surname;

    lt->size -= 1;
    lt->head = node->next;

    free(node);

    if(lt->size == 0){
        /* Это был последний элемент */
        lt->head = NULL;
        lt->tail = NULL;
    }

    return ret_val_I, ret_val_II, ret_val_III;
}

/* Добавляем элемент в конец списка */
void list_push_back(list_t *lt, void * data, void * name, void * surname)
{
    list_node_t * node = malloc(sizeof(list_node_t));
    node->data = data;
    node->name = name;
    node->surname = surname;   
    if(lt->tail != NULL)
        lt->tail->next = node;
    else {
        lt->head = node;
    }

    lt->tail = node;
    lt->size += 1;
}

void * list_print(list_t *lt)
{
  list_node_t *node;
  int i=0;
      for(node = lt->head; node!=NULL; node = node->next)
      {
    printf("%4d) %s %10s %4d \n", i, node->name, node->surname, node->data);
    i++;
  }
  printf("\n");
}

void * delete_after(list_node_t * node) {
  list_node_t * p;
  p = node -> next;
  node -> next = node -> next -> next;
  free(p);
}

void * search(list_t *lt, int x){	
  list_node_t * p;
  for(p = lt->head; p != NULL; p = p -> next)
    if(x == p -> data) return p;
    return NULL;
}

void * search_by_indeks(list_t *lt, int dl){	
  list_node_t * p;
  p = lt -> head;
    for(p != NULL; dl>0; dl--)
    { 
    p = p -> next;
  }
  return p;
}


int main()
{
  
  list_t *st = create_list();

char c='N';	
printf("Czy dodajemy osobe do listy? Odpowiedz Y/N: "); 
///printf("Добавляем человека в список? Ответ Y/N: "); 
fflush(stdout);
scanf("%c", &c);
if(c=='Y')
{
  int data, *ptr_D;
  char name, *ptr_N;
  char surname, *ptr_S;
  printf("Podaj imie: ");
  scanf("%s", &name);
  printf("\n");
  printf("Podaj nazwisko: ");
  scanf("%s", &surname);
  printf("\n");
  printf("Podaj wiek: ");
  scanf("%s", &data);
  printf("\n");
  
  /* Добавляем в начало списка */
  list_push(st, data, name, surname); // I.
  
  list_print(st);
  printf("\n");
}
else list_print(st);
  

/* Извлекаем из начала списка*/
//ptr = (int *)list_pop(st);

  
  getchar();getchar();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-01-28
@vt4a2h

where did not finish my materiel?

In terms of working with memory: allocating and clearing memory, stack and heap, the difference between an array and a single element, the lifetime of objects.
For example, you are reading data into an element, which is a single character allocated on the stack. You also need a buffer of sufficient size, best of all allocated dynamically, because you then are going to store it in some data structure.
I recommend that you carefully read K&R and follow all the examples. This is a very small book that will close all your gaps in terms of the C language.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question