The memset of the data structure base---the conversion and byte alignment of the memset thrown int and Char

Source: Internet
Author: User
Tags usleep

Tonight, in the filter algorithm, the use of a lot of float and int and char type cast, after filtering to find some areas of the image block, there is over-exposure of white light, I followed, thought it was char character digital data overflow problem, add 0-255 judgment, and then print, Found that after the cast of the int type data there are many negative numbers, very strange, after writing a test program, slowly the problem came out:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int test (int wid, int hei, int *buffer)
{
int i,j;
int tmpval;
int *pbuffer = buffer;
memset (pbuffer,0,wid*hei*sizeof (buffer));
for (i =0; i<wid*hei; i+=4)
{
Tmpval = (int) * (pbuffer+i);
printf ("%d\n", tmpval);
Usleep (200);
}
return 0;
}
int main ()
{
int wid,hei,framesize;
int i,j;
unsigned char tmpvalue = 0;
WID = 4;
HEI = 5;
Framesize =wid*hei*sizeof (int);

int *buffer = (int*) malloc (wid*hei*sizeof (int));
memset (buffer,2,wid*hei*sizeof (int));
for (i =0; i<wid*hei; i++)
{
Tmpvalue = * (buffer +i);
printf ("%d\n", tmpvalue);
Usleep (200);
}
printf ("test:\n");
Test (Wid,hei,buffer);
return 0;
}

The output is obvious:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
Test
33686018
33686018
33686018
33686018
33686018

The main function with the char type of temporary variable tmpvalue unchanged, is 2, however, the sub-function test with an int type of data missing 33686018, how does this number come?

Let's first analyze the Memset function from the data store.

Function: fills a given value in a memory, noting that padding is filled in byte order, not by element.
This method is an effective way to clear 0 operations on large structures and arrays.
function form: memset (void *buffer,int c,size_t N)
Buffer is the start address of the memory that needs to be set, C is the expected padding value, and n is the number of bytes that need to be populated.

So here's the main function to put the buffer "i" padding four bytes 0x02 0x02 0x02 0x02 storage High first byte assignment brother char type Tmpvalue 0x02

If the 2 here is changed to 257, the situation is as follows:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Test
16843009
16843009
16843009
16843009
16843009

Similarly 257 = 0x1 (hex), then 0x01010101 (hex) = 16843009 (inter), natural char goes to high one byte 0x01 = 1, General memset operation on string, int value assigned to char variable, only reserved Its lowest 8 bits, the high part discards.

Http://www.360doc.com/content/11/0120/19/1317564_87917268.shtml

http://blog.csdn.net/lida2003/article/details/6973469 the difference between int and char conversions

Http://baike.baidu.com/view/3975627.htm

Http://www.gfsoso.com/?q=+int+%E5%92%8Cchar+%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2%E7%9A%84%E9%97%AE%E9%A2%98

The memset of the data structure base---the conversion and byte alignment of the memset thrown int and Char

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.