Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing the memset function and my code is below, I am having a problem

void* memsetFun(void* pointer, int c, int size) {

  if ( pointer != NULL && size > 0 ) {

    unsigned char* pChar =  pointer;


    int i = 0;

      for ( i = 0; i < size; ++i) {

      unsigned char temp = (unsigned char) c;

      *pChar++ = temp; // or pChar[i] = temp (they both don't work)

    }
  }  
    return pointer;


}

I also tried pChar[i] = the value we want and still not working. It gives me some trash numbers that do not make any sense.

And I am calling it:

memsetFun(address, num, size);
printf("value at %p is %d
", address, *((int*) address));

Where I call the address (I just input the address)

For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )

0 0
0 4
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
316 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code looks fine to me and several people here have commented that it works on their system.

So the obvious thing to do is to debug it - that's a skill that will come in handy quite a bit in future :-) You should learn it now.

What does the following code output when you run it?

void* memsetFun(void* pointer, int c, int size) {
    printf("A %x %d %d
", pointer, c, size);
    if ( pointer != NULL && size > 0 ) {
        printf("B
");
        unsigned char* pChar =  pointer;
        int i = 0;
        for ( i = 0; i < size; ++i) {
            printf("C %d (%d)", i, *pChar);
            unsigned char temp = (unsigned char) c;
            *pChar++ = temp; // or pChar[i] = temp (they both don't work)
            printf(" -> (%d)", i, *(pChar-1));
        }
    }  
    printf("D
");
    return pointer;
}

From the output, it should be clear what paths the code is taking and what your parameters are (which will greatly assist the debugging process).

Update:

If you're filling your memory block with anything other than zeros and using this:

printf("value at %p is %d
", address, *((int*) address));

to print it out, you will get strange results.

You're basically asking for a number of those bytes to be interpreted as an integer. So, for example, if you filled it with 0x02 bytes and you have a 4-byte integer type, you will get the integer 0x02020202 (33686018), not 0x02 as you may expect. If you want to see what the first character value is, use:

printf("value at %p is %d
", address, *((char*) address));

And based on your latest question update:

For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )
0 0
0 4

If that's a single character and you're printing it as a character, there's probably nothing wrong at all. Many output streams will give you that for a control character (CTRL-D in this case, ASCII code 4). If you instead filled it with ASCII code 0x30 (48), you would see the character '0' or ASCII 0x41 (65) would give you 'A'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...