-1

I have an assignment on memory blocks. We have a struct Record that I need to save in these memory blocks. Each block holds 5 Records, which i memcpy one after another in a list of blocks and an int CountOfRecords.

We also have a DeleteRecord function that deletes (duh) a particular record from the memory block. Now, other than reducing the Count and shifting all next Records forward as to practically delete that Record, is there any way to ACTUALLY delete what is written with memcpy? Like writing something like a NULL as a struct instance? Memmove does not seem to offer such an application.

EDIT: I write the records as such

//block is the pointer to block,int is for the Count, and record is placed
memcpy(block+sizeof(int)+sizeof(Record),&record,sizeof(Record));
  • 1
    You need to show some code. It's hard to guess what you mean. – Jonathan Leffler Dec 16 '20 at 19:59
  • 4
    Memory stores values. There is no way to “delete” or “erase” memory so that it does not contain a value. You can set the memory to all zero bytes or to other patterns that would indicate there is no valid record there. `memset` can be used to set it to all zeros, or to all bytes of one value. What do you actually want to do? – Eric Postpischil Dec 16 '20 at 20:00
  • The goal is to not have a reference to that Record any longer. The patterns you suggest are one way of setting the Record to be not valid – JamesTheProg Dec 16 '20 at 20:04
  • 2
    As suggested in first comment, some code (preferrably a [mcve] ) is really needed to fully understand what you are talking about. – ryyker Dec 16 '20 at 20:07
  • What do you mean *"not have a reference to that Record any longer"*? What kind of "reference" are you talking about? Your use of that term in this context is highly confusing (and arguably not really applicable to C code) – UnholySheep Dec 16 '20 at 21:44
  • @JamesTheProg: Objects in an array do not have separate references to them like objects in a linked list or various other data structures. In a linked list, each object is part of the list only because the previous object is pointing to it. You can easily remove an object from the list by changing that previous object’s pointer to point somewhere else or to null and then freeing the memory of the object (which releases it from this use; it does not erase or delete it). An array in C is a contiguously allocated sequence of objects. There is no way to remove an object from its middle. – Eric Postpischil Dec 16 '20 at 23:33

1 Answers1

1

You basically don't want to move data around but instead set the memory to zero, how about memset?

memset(block+sizeof(int)+sizeof(Record), 0, sizeof(Record));

But you somehow have to remember, that the record at this point is zeroed out (not used), best by some property.

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11