Wikipedia:Reference desk/Archives/Computing/2017 December 31

= December 31 =

C programming — how do I view the 8, 32, or 64 bits of binary information at a known memory address?

 * "Main memory or RAM is arranged as an array of "sets of electronic on/off switches" or locations beginning at address 0. Each location can store usually 8, 16, 32 or 64 bits."
 * "Memory can be thought of as an array of bytes where each address is one index in the array and holds 1 byte."
 * My final attempt of getting help before coming here to the ref desk! Ԅ

I'm an absolute beginner. I just joined codeacademy last month and more recently I've enrolled in Edx's premiere computer science class CS50! I just learned through wikipedia how to find the memory addresses of any data type just by typing "%p, &shark inside the printf function and now I'm like a giraffe who discovered the stepladder!

Is there a clean, fool-proof way for me view the 8, 16, 32, or 64 bits of ANY memory location just by having that memory location's address? (e.g. 0x7ffed73897d8) In other words—if I know the address—is there a program or line of code which can "print for me the 8, 16, 32, or 64 bits of binary information" at the known address? 67.233.34.199 (talk) 03:03, 31 December 2017 (UTC)
 * Here's the thing: all data in any typical computer is binary. Any time you're printing output, you are "printing binary information". Your code just usually tells the computer to interpret said data in a particular way, such as an integer value within a certain range. This is a thing a lot of beginning programmers have trouble wrapping their heads around. Computers are dumb. They don't know what a "number" is. They just process data according to the instructions they're given. If you tell the computer to interpret a value as a 32-bit signed integer, it will do that. If you tell it to interpret it as a name in a database, or a pixel in an image, it will do that. Now a second thing: on most modern computers, you can't arbitrarily access memory addresses in a program. The reasons take a while to explain in detail, but they have to do with virtual memory and operating system access controls. You usually can view a process's memory contents with a debugger, and sometimes other ways as well, like through the proc filesystem on Linux. (Sometimes this requires administrator privileges.) If you do want to play around in an environment where you can access arbitrary memory, try an emulator for a platform without memory protection, such as Dosbox.
 * Anyway, to sort of answer your question, C gives you types for accessing fixed bit-width values, in . Note this requires C99, but if you're trying to learn in an environment without C99 support, throw it out and use one that does. Note you will see a lot of stuff online telling you to do things like use a  to read a single byte. This is wrong in plain C because the size of types like   is platform-dependent. Using   as a synonym for "8-bit byte" was common in pre-standard C, and old habits die hard. With that said, POSIX does mandate   be exactly eight bits, so if you're writing explicitly for POSIX platforms you can assume that. Here's an example for looking at a 32-bit chunk of memory. As I said, on modern platforms you can't access arbitrary memory addresses, so instead we're just going with whatever the operating system gives us off the heap with malloc.


 * This prints the value and address pointed to in hexadecimal. C has no built-in support for printing values in binary because it's rarely useful; hexadecimal is what programmers usually use for displaying "raw" data. As a teaching exercise, you can try using this macro for binary output. For pedagogical value, I've done things right by checking for  failure, and  ing the  ed memory after using it. Obviously this isn't necessary in a toy program, but in "real" programming you should always do these. Note as well that forgetting to assign something to a  ed pointer is a common mistake. Here it isn't a mistake because we want to see whatever's in the memory location already. It's possible that you might always get a value of 0, because these days it's increasingly common for systems to zero out memory when it's allocated by a process. --47.157.122.192 (talk) 10:48, 31 December 2017 (UTC)
 * Oh, and here's a fairly good Wikibooks page on stdint.h. --47.157.122.192 (talk) 11:24, 31 December 2017 (UTC)
 * In addition to the reliable types defined in stdint, inttypes.h defines reliable printf format specifiers for the same specific sizes. So:


 * -- Finlay McWalter··–·Talk 11:09, 31 December 2017 (UTC)


 * In fairness I should note that inttypes.h is a C99 feature, and MSVC's support for C99 has been patchy. It seems they added support for this in the 2013 version (but I've not personally tested this on that platform). -- Finlay McWalter··–·Talk 14:59, 31 December 2017 (UTC)
 * Yes, that's the right way to do it. For some reason I was thinking printf did some magic with the width of its arguments. Moral of the story: 1 AM is not the best time to program. --47.157.122.192 (talk) 21:37, 31 December 2017 (UTC)


 * I think the bigger problem here is that, given memory protection, memory virtualization, address randomization and other such things, there may not even be an address "0x7ffed73897d8" from which you can read. If you absolutely, positively need to read from an address given as a literal, you cast that literal into a desired pointer type and use the pointer to obtain the value stored at that address (i.e. dereference it), like this:
 * This is it, as far as C is concerned, but see above. Your program will most likely crash. In simpler systems running on simpler hardware, such as DOS running in real mode, this was, however, a totally "legit" way to access memory at known addresses, for example in order to write to the video memory or to read the BIOS data area that contained various useful info about the computer (such as the number of floppy drives or COM ports and their respective addresses.) 78.50.124.31 (talk) 20:38, 31 December 2017 (UTC)
 * I take it your misconception about memory stems from the phrasing "can be thought of as an array... at location 0." This is a simplification, but good for you thinking ahead, apparently you're the first participant of your course to do so 78.50.124.31 (talk) 20:59, 31 December 2017 (UTC)
 * I take it your misconception about memory stems from the phrasing "can be thought of as an array... at location 0." This is a simplification, but good for you thinking ahead, apparently you're the first participant of your course to do so 78.50.124.31 (talk) 20:59, 31 December 2017 (UTC)

C programming — "printf("%d\n",*(int*)0x7ffed73897d8)" worked and I'm elated!
I'm happier than a clam at high tide! Now I have a whole host of experiments I'm wishing to do because I want to test and verify for myself a lot of questions along the lines of "what will happen if I do x" and I needed to know this particular line of code. The other solutions worked when I ran them in codeblocks (although not in the cloud ide that CS50 uses) but I didn't fully understand what I was seeing nor how to tinker & substitute my own memory addresses into the code, which is fine for now.

The other great thing I really like about computer programming is the absolute freedom you have to write and instruct the computer to do anything you want — no questioning, no second-guessing, no backtalk — especially if you are the type of person who gets joy at telling the computer to perform purposeless tasks. Thanks again, everyone! 67.233.34.199 (talk) 11:57, 1 January 2018 (UTC)

Who or what can fix corrupted and unplayable video files?
I don’t know if this is an appropriate place to post this or if I should frame the question differently. I’m going to go ahead and explain and ask. I recently went on a trip where I took a lot of videos. I have a USB drive that you can put in to an iPhone on one end and the computer on the other end. To free up some space on my iPhone to continue recording, I moved some of the video clips directly to the USB drive. However, when I returned home and started going over the videos I put in my USB drive, my computer and my iPhone were unable play the videos except for the first few. 9 of the videos won’t even copy and paste to my computer. Is such an issue fixable? Who are what would be able to fix corrupted/unplayable videos? Willminator (talk) 03:22, 31 December 2017 (UTC)
 * Did you check the file system on your USB drive for for errors? Ruslik_ Zero 16:41, 31 December 2017 (UTC)
 * I don’t know. Are such files recoverable? Willminator (talk) 21:27, 1 January 2018 (UTC)
 * Is the USB drive a brand name drive purchased from a reputable seller or a generic drive bought online at a seemingly bargain price? The reason I ask is that there is a long running scam involving both USB flash drives and SD cards where low capacity drives and cards are hacked and relabeled so that they appear to have a much higher but false capacity. (Briefly discussed in USB flash drive.) Products with only a few GB of real memory can be modified to show as much as 2TB of false capacity. Data may appear to write successfully to such drives (or cards), but past some point, the data is irretrievably corrupt when read back. -- Tom N  talk/contrib 04:27, 2 January 2018 (UTC)
 * Can these scam drives be modified to correctly report the true memory capacity? Graeme Bartlett (talk) 05:32, 3 January 2018 (UTC)
 * In theory, the Flash memory controller firmware can be restored to its original state by overwriting the hackers' Custom firmware with a copy of the genuine control program, but this is a specialist task and probably requires detailed knowledge and specialist equipment. I wouldn't know where to start.  Restoring the original control ware would not recover data that has already been overwritten,  but it would prevent overwriting in future.  It's probable that only a few bytes have been changed, just allowing the control program (hidden on the USB drive) to re-use memory locations that should have been protected because they already contain valid data.  If the USB drive was genuine, and files have not been overwritten, then sofware is available to recover files from a damaged file structure.  I have some that works well for pictures, but maybe not for videos.  Dbfirs  16:21, 3 January 2018 (UTC)
 * I'm not convinced it's anywhere near as difficult as you suggest. You use something like Chipgenius to work out what the controller and flash chip is, then you look and see if there's a leaked controller manufacturer tool which supports your flash chip. The later can be complicated, I don't know about other manufacturers but for SMI controllers at least, the latest tool doesn't necessarily support all controllers and flash chips supported by older versions, perhaps not that surprising since these aren't intended for end users. Still provided what you have isn't too obscure you might have hope.  There are a lot of guides and stuff online although a lot of the best stuff is in Russian. (I think there's also a fair amount of stuff in Chinese but it doesn't show up as much or get linked from the English guises etc.)     . Although this forum at least does seem to accept stuff in English .  Note that these guides aren't really intended for re-flashing a USB key with dodgy firmware that reports a false capacity, but instead to try and fix drives which start showing up as 'smi usb memory bar' or something like that. Still the way you do this is normally by flashing a stock firmware which should most likely solve such false capacity problems. I believe that this often also kills the bad blocks list of the stick, I can't remember offhand or perhaps I never found out how to fix this although it may simply entail writing and reading the whole disk a few times, or maybe the flash software does that.  Note as at least one of the guides warns, you should consider carefully what you are doing if you're trying to fix a drive with false capacity since who knows where they got the memory chip from and what its quality and condition are. (While this may apply to a drive that breaks, it does appear often these are mostly just cases where the firmware somehow got messed up and the flash chip itself isn't significantly damaged.)  Nil Einne (talk) 12:38, 5 January 2018 (UTC)
 * You are correct, it's not as difficult as I had anticipated if only you can find the correct virus-free software. I wouldn't recommend that the OP tries this, because it will wipe contents, not recover files. Here and here are some suggestions for file recovery software on the assumption that the USB drive was genuine.   Db<i style="color: #4fc;">f</i><i style="color: #6f6;">i</i><i style="color: #4e4;">r</i><i style="color: #4a4">s</i>  16:13, 5 January 2018 (UTC)

iPhone and Android smartphone
Apple's iPhone don't have memory card slot. It's RAM is not more than 1gb (5 or 4 model). It's screen size is smaller than Android phones. Inspite of all these factors people buy it at awesome prices! (Which are far far higher than Androids having all I have just mentioned).Why is that? No, it can't be just brand mania. Or is it? Jon Ascton   (talk)  07:02, 31 December 2017 (UTC)
 * It quite possibly is brand mania on the part of those few remaining iPhone users, given that iOS usage has dropped so far that Android and iOS now have shares of the smartphone market roughly equal to the shares that Windows and macOS have in the desktop market, respectively. Just like there're some people who'll always go Mac, there're some people who'll always go iPhone. Whoop whoop pull up Bitching Betty &#124; Averted crashes 10:51, 4 January 2018 (UTC)
 * One assumes people buy it because they like it better than competing products. As for why, you'd have to ask them. Please note the page header, which states that we don't accept requests for opinions, predictions, or debate. I will note that at least in the U.S., iPhones and top-end Android smartphones are priced comparably. For instance, the iPhone 8 Plus MSRP is $799, while that of the Samsung Galaxy S8+ is $824.99. --47.157.122.192 (talk) 11:00, 31 December 2017 (UTC)
 * Neither 5 nor 4 models are sold now. Ruslik_ Zero 16:46, 31 December 2017 (UTC)
 * Apple already had a giant "start" into "mobile electronic" with the iPod, which, like the iphone, was "first" of its category and also "best of art" for long enough to become famous and iconic. Of course its brand mania to some and a divine altar to others. I never understood why everyone wanted a Mercedes Benz - even Janis Joplin . I always only wanted a Porsche. --Kharon (talk) 17:44, 1 January 2018 (UTC)
 * You should consider Geländewagen. Ruslik_ Zero 19:13, 1 January 2018 (UTC)
 * Nah, i live in Berlin and we have Carsharing here. They have neither Porsche's nor Mercedes'es * but atleast i have a Samsung S7 android phone now. *(Oh, i forgot they do have Mercedes trucks :).--Kharon (talk) 04:55, 3 January 2018 (UTC)
 * Hardware specs are not all that people are interested in, and opinions on what is good differ. I like my iPhone 5s because it is quite small, not despite of it. If I need a big display, I have a 27" monitor. I've never suffered from RAM shortage on the phone, and despite being a computer nerd, I don't even know how much RAM my phone has - it's as with the question about horsepowers of a Rolls Royce: the answer is "enough".  Typical advantages of iPhones are excellent integration with other Apple products, long product support (including software updates - the 5s was released in 2013 and still runs the latest iOS), a clean and simple product line-up, and, usually, top CPU power and top or near-top cameras for their generation. Some Android phones have some advantages over iPhones, but, as mentioned above, the ones that come close in most aspects also come close in price. --Stephan Schulz (talk) 16:26, 2 January 2018 (UTC)