-3

tl;dr; How to reproduce memset with basic c++?

I am trying to figure out how memset works , and to see if I can reproduce it within normal c++ using pointers and address.

This is what I've got so far.

    void test_memset( void * origin, uint8_t val, size_t size ){
         if( size != 1 ){
             uint8_t* new_or = (uint8_t*) origin;  // casting to uint8_t*
             *new_or ^= (*new_or & 0xff); // clearing any remaining data
             *new_or ^= val;              // assigning the val
             new_or++;                    // incrementing pointer
             test_memset_alloc( new_or, val, size-1 ); // recursion
         }
    }   

So the question : What does memset look like in basic c++?

EDIT 1:

Answered here in similar manner, and here a tad bit more verbose. Both answers are better suited than my proposition.

But i would like to see some detailed solutions, that would be able to explain a Mort (the line-of-business developer) or someone with fractuated knowledge, what is happening with more clarification than :"This is frowned upon" or "not a c++ way".

Danilo
  • 1,017
  • 13
  • 32
  • Because of recursion limit ? – Danilo Aug 09 '19 at 12:17
  • 1
    @Danilo memset implemented in C++ code would look pretty much like https://stackoverflow.com/a/18851921/126769 , if you are introducing nosensical code (it would even be undefined behavior in some cases, as you could read an uninitialized value) like `*new_or ^= (*new_or & 0xff);` , you are pretty much guaranteed to confuse the "users" instead of teaching them something. – nos Aug 09 '19 at 13:00
  • 1
    Well, [I found a more verbose answer](https://stackoverflow.com/questions/13327155/memset-definition-and-use). – hegel5000 Aug 09 '19 at 13:35

2 Answers2

2

A basic, inefficient but correct memset implementation in C or C++ looks like this:

typedef unsigned char byte;

void* memset(void* s, int c, size_t sz) {
    byte* p = (byte*)s;

    while (sz--)
        *p++ = (byte)c;
    return s;
}

This uses a loop because the loop is the right tool for the job. Your insistence on avoiding loops makes no sense. "they don't exist from processors point of view" is not a valid reason to avoid them. It's not a true statement to begin with, and not even a meaningful one. Your further explanation betrays a serious lack of understanding of How Things Work™. If a loop is a black box, then everything is a black box. A function call is a black box. A cast is a black box. An if statement is a black box. An arithmetic expression like (x+y)*z is a black box. None of those is more or less transparent than a loop.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
1

memset looks likè the following in basic C++: memset.

Not kidding. It's such a basic function that for compilers the likely next step is target-specific assembly code.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I hope you understand that definition of something isn't the name itself. The question isn't meant to justify or replace the current function. Question is meant as an development of further understanding of memset. If someone tries to reconstruct a function, he/she can then understand better what messages from compiler means when they make mistake. I would strongly suggest if you could include more information in your question. – Danilo Aug 09 '19 at 12:31
  • 1
    @Danilo: That's why I say _in basic C++_. You can't go further in C++ than `memset`. Beyond that, there are two directions. You look at the C standard which specifies the behavior in English (C++ inherits it from C), or you look at the generated assembly instructions. – MSalters Aug 09 '19 at 12:34
  • Ok, i get it... i really do. But you must understand that *"Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char)"* doesn't actually show how it works. And when compiler returns an error on example, user is dumbfounded. That is why i opted for "if someone tries to reconstruct a function, he/she can then understand better what messages from compiler means when they make mistake." – Danilo Aug 09 '19 at 12:40
  • @Danilo: Of course the C standard does not directly define **how** it works. That is not the point of the C standard. The C standard describes what different implementations have in common. Exact implementation details differ. That is why I say the other direction is to look at the assembly generated by a particular compiler. – MSalters Aug 09 '19 at 12:46
  • So please, take the time... make an correlation between its assembly and c++ code. Explain it so newb can understand it so i can accept the answer for all new users. Let's work on this! Let us explain this in a way that new user doesn't have to learn 3 more languages to understand the compiler error. C'mon that is like 5 to 10 minutes of your time. – Danilo Aug 09 '19 at 12:48
  • @Danilo: Look at https://godbolt.org/z/E1Hluh. There might not even **be** assembly code for `memset` ! The newb description is literally what the name says: `memset` sets all bytes of a memory block to a certain value. _How_ is something you don't need to know. – MSalters Aug 09 '19 at 13:03