Thread: Memory Content in C Code.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Memory Content in C Code.

    Hi,
    I have a basic question, I have midterm exam, and attached question one of the example question for midterm. but I do not understand, finding address and its contents from C code which topic I have to look? is there any helpful youtube videos or article you know?
    I really be grateful.

    Thank you.

    Memory Content in C Code.-question-jpg

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,235
    This basically requires you to understand two things:
    • Pointers and pointer arithmetic. Drawing arrows and such to help you visualise where the pointers point to with respect to the boxes that represent the memory might help.
    • Integer conversions. Observe that 0xABBCCDEF is a four byte integer constant, but it was assigned to an int8_t, which only has one byte. Likewise, 0xDEADBEAF is a four byte integer constant, but it was assigned to a short int, which likely only has two bytes. Technically, because we're dealing with signed integer types, the result is implementation defined, but your instructor would have explained to you what is the typical conversion to expect, so you just need to revise it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    There are 3 problems with this question:

    First, probably the addresses assigned to the pointers aren't mapped to the linear address space of the process. Any attempts to dereference those pointers will result in "segmentation fault".

    For the exercise sake, let's assume using this addressing scheme works...

    Second, as you notice, the addresses 0x20000001 and 0x20000003 may have any bytes!

    Third, and more important: There are machines which the memory ordering is BIG ENDIAN (Intel is LITTLE ENDIAN). So, to write a short int (ASSUMING it is 16 bits long!) to an address, on BIG ENDIAN machines, the MSB is writen first: *p_s_int = 0x23456789will write 0x67 first, and *p_int = 0x12345678 will write 0x12 first... You can predict, for any machine, the order of writes using int8_t pointers, but not pointers for types larger then 1 byte...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a memory leak in my code?
    By JHugh in forum C Programming
    Replies: 5
    Last Post: 08-04-2017, 08:03 AM
  2. Adding C++ content into C code
    By lautarox in forum C Programming
    Replies: 1
    Last Post: 05-20-2009, 12:55 PM
  3. Simple Memory Content Question
    By azzuwan in forum C Programming
    Replies: 7
    Last Post: 09-18-2008, 04:23 AM
  4. Does this code has memory leakage?
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2008, 08:13 PM
  5. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM

Tags for this Thread