Tips for printing memory

Source: Internet
Author: User

When learning C ++, the compiler has done a lot of things with us, so let's look at the advanced data structures at the Assembly level and in the memory, efficient writingCodeVery helpful.
Below is a small function that helps you print the content in the memory. If Microsoft compiler is used, byte guard may exist between memory objects, that is, the compiler inserts a blank bytes between the allocated data objects to facilitate detection and destruction of adjacent objects, so it's a little different from the continuous memory allocation in textbooks. Just pay attention to it.

The following code is used to add a test:

# Include <cstdio>

struct test {
int A;
char B;
};
void showbytes (void * s, int N)
{< br> unsigned char * Start = (unsigned char *) s;
printf ("[offset] address: value/n ");
for (INT I = 0; I {< br> printf ("[%. 4d] %. 8x: %. 2x/N ", I, start + I, * (start + I);
If (I + 1) % 4 = 0)
{< br> printf ("----------------------/N");
}< BR >}// for
}

int main ()
{< br> test * t;
Test A = {12, 'A' };< br> T = &;
showbytes (T, 8);
getchar ();
return 0 0;
}

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.