Learn how to use memory byte for pointer Variables

Source: Internet
Author: User

When learning how to set the data sending and receiving buffer, do the following experiment:

void main (){        LPBYTE m_data=new BYTE[16];    memcpy(m_data,"example",sizeof(m_data));    string str="   ";    str[0]=m_data[3];    cout<<str<<endl;}

The memcpy (m_data, "example", sizeof (m_data) function copies the binary bytes of "example" in the memory directly to the buffer pointed to by m_data, the copy size is the third parameter;

Sizeof (data type)
The return value is the number of bytes occupied by the data type.

Sizeof (variable name)
The return value is the number of bytes occupied by the variable.

I don't know why m_data [k] When k> 3, the output is always? (M_data is a pointer variable)

You can use an integer instead of sizeof (m_data) to obtain the ideal result.

Lab

1 # include <stdio. h> 2 int main (void) 3 {4 char CH = 'a'; 5 Int I = 99; 6 double X = 66.6; 7 char * P = & Ch; 8 int * q = & I; 9 double * r = & X; 10 printf ("% d \ n", sizeof (P), sizeof (Q ), sizeof (R); // specify the number of bytes of pointer variables of different types. 11 return 0; 12}

The running result is 4 4 4.

Therefore, it is found that pointer variables of any type occupy 4 bytes. We also know that sizeof of any char (signed, unsigned, or normal) type is 1! Whether the storage space of char is actually a byte or not.

In this way, we can understand that sizeof (pointer variable) is not the size of the area to which the Pointer Points, but a value of 4, that is, sizeof (pointer variable) occupies 4 memory and does not care about the size of the area to which it points.

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.