P
P
Pinkman2020-11-09 14:49:11
C++ / C#
Pinkman, 2020-11-09 14:49:11

How to move data to an address?

Good afternoon! The question is: how to move data from one memory cell?
To make it clearer, I have a singly linked list like this:

typedef struct s_list
{
    void* data;
     struct s_list* next;
} t_list;

And a function that removes an element from the list Is it possible to somehow move the pointer to Next to the cell where the pointer to the sheet is stored? Because I want to remove a list item without breaking it (the list) and without running through the list from the beginning.
void delone(t_list** lst);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CityCat4, 2020-11-09
@CityCat4

Hm. I'm not sure that I understood correctly, but moving any data from anywhere to anywhere and as much as you like (within the limits of available memory, of course) is done through memmove (). The thing is universal, but dangerous - the whole calculation of pointers, as well as the length of the movable section, is the programmer's concern, the call will simply crash the program via SIGSEGV if something is passed incorrectly.

W
Wataru, 2020-11-09
@wataru

I didn’t quite understand the question, but if you mean rewriting the pointer to the element to be deleted, then there are 2 approaches.
1) If the function is recursive, then let it return a new head of the list. If you don't want to delete the current element, then rewrite next with the result of calling next from this and return the current entry. If you need to delete, then clear the memory and return next.
2) If the function works in a loop, which is preferable, then you can just remember the previous element of the list. Move 2 pointers in parallel.

prev = cur;
cur = cur->next;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question