#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3

    NULL terminator: general question


    If i have an array:
    Code:
    char arr[20];
    And i write into it, character after character 'h' 'e' 'l' 'l' 'o' '\0' <---including NULL

    And then i use the SAME array to type in 'h' 'e' '\0' <---Also null

    Does putting NULL in the new array automatically delete the 'l' 'l' 'o' '\0' which were present in the first string and that exceed the length of the second array ?

    Or do these characters stay in the memory, but they are just not printed when the new array ( 'h' 'e' '\0') is passed into a print ?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    General Rule: The contents of memory do not change until you change them.

    So in your example:
    arr contains 'h' 'e' 'l' 'l' 'o' '\0' <garbage or '\0'>*14
    After you enter "he", arr contains
    'h' 'e' '\0' 'l' 'o' '\0' <garbage or '\0'>*14
    It doesn't matter what follows the new null-terminator, because that is not part of the string anymore. When you write a longer string to arr, then that new string will overwrite the locations that it needs to occupy.

    I refer to the remaining 14 characters of arr as containing either garbage or '\0' because if arr is global, then it would have been filled with zeros as your program is preparing to start execution, but if it's local to a function then it just doesn't get initialized and those locations contain whatever garbage is there from before. Either way, it doesn't matter what they contain because they're not part of the string stored in arr.

    A similar situation existed in the MS-DOS file system -- I don't know if it's still the same since MS-DOS was replaced by Windows 95.

    The disk maintained a File Allocation Table (FAT) that kept track of which allocation units (AU) were used by the files. The file's directory would have an entry for that file with information about the file, including its first AU. To access the file, the OS would look up its first AU and refer to the FAT to see where that AU was located physically. Since one AU is not always enough for a file, each FAT entry was a link to the next AU. The FAT also maintained a "Free List" which was a chain of AUs that didn't belong to any files and hence were available to be allocated to new files or to files that needed to grow. When you created a new file, the FAT would create an entry for it in its file directory and request its first AU. As the file would outgrow that AU, the next available AU on the free list would be allocated and added to that file's AU chain. When you deleted a file, its entry in the directory would be marked as deleted (the first character of the filename would be replaced by a special character) and its AU chain would be returned to the free list to be reused by the next file that needed it, as its directory entry would be reused by the next new file that needed it.

    What about the data contained within those newly freed AUs, what was done to it? Absolutely nothing. That data remains completely intact and the newly free AU entries in the FAT still link together. And when an AU gets allocated to another file, it gets all that old data. But that doesn't matter because the end-of-file mark keeps it from reading into that old data. Rather, the new file will overwrite that old data starting from the beginning of the AU. In the last AU of any file's chain and at the end of that AU past the end-of-file mark, it still contains the old data that had been placed there by that old file that had had that AU before. An analogy for how files are deleted in MS-DOS would be removing a book from the library simply by removing its card from the card catalog (ask your parents or any other older relative about card catalogs). The book is still on its shelf in the library; you just don't know where it is.

    The IBM PC came out around 1980. In the 1980's, the book on every MS-DOS programmer's bookshelf was Peter Norton's Inside the IBM PC. In that book, he mapped out DOS' use of memory, the system call interrupts, and the file system. He also either included or sold separately his Norton's Utilities that included DISKEDIT with which you could navigate through the file system and make changes, which was handy for fixing some problems, though also very dangerous if you don't know what you're doing. He also had a utility, UNDELETE (I think), in which you could recover a file that you had just deleted; of course, successful recovery depended on how many of its AUs had already been recycled to new files.

    Norton's Utilities are still around as Norton Commander, owned now by Symantec. A popular shareware clone is Midnight Commander. Look them up on Wikipedia for more information.

    Computer forensics also make use of the fact that deleted files don't really go away. And security-minded folk, such as government agencies working with sensitive information and now any business that works with personal information (eg, social security numbers, account numbers), have devised special utilities that will delete a file by first overwriting it completely and repeatedly; that process is sometimes referred to as "wiping".

    Just keep in mind: data does not go away unless you make it go away.
    Last edited by dwise1_aol; August 17th, 2013 at 12:31 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Thanks :) I'll keep those things in mind.

    So here it's all about putting the stopper in the right place. And the next step is to think deeper and realise you need not forget the used memory you left behind. Just like cleaning the garbage after you finished working on something. If you forget to do that it will turn into a mountain of garbage, and you will have less space to work with in the future.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Well, yeah. Same reason you shred papers with personal information before they go into the trash. If there's anything you don't want others to see, then dispose of it properly.

    Terminology correction: the earlier and more common term for an allocation unit is a cluster (http://en.wikipedia.org/wiki/Cluster_(file_system)). After all, I hadn't revisited this part of the technology since before 1990.

    I just thought about that left-over data from old files being left at the unused end of the file's last cluster and whether they go along for the ride with you share that file with others, such as providing it for download. I don't think so, since the copy or upload utility should only copy blocks of data up to the end-of-file and not beyond, rather than copying entire clusters. The only real concern would be if CIS were to confiscate your computer as part of their investigation. Or if your computer/laptop gets stolen and falls into the hands of an identity thief. Or you sell or give away or dispose of your old computer and it falls into the wrong hands. There are means to wipe all the data from your disks, including bulk erasers (a big electro-magnet). They once experimented on The Screen Savers with using thermite, but I wouldn't recommend that.

    And OBTW, clusters come in different sizes depending on the media. Clusters in low capacity media such as floppies could be 512 bytes, while clusters on high-capacity media like modern hard disks could be 64 KB. Just to contribute to the paranoia.

    However, when you download a file, I don't think that the
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    Thanks :)

IMN logo majestic logo threadwatch logo seochat tools logo