Thread: c - cheak readable bytes

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    7

    Post c - cheak readable bytes

    i just wanna to cheak if the byte is readable or not, i have search for sulution but not find
    hope you will help me
    i have this code i need if tag that cheak if byte is readable
    Code:
    #include <windows.h>
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    void main()
    {
       float ramsize;
       char *ch;
       unsigned int j=128,readbyte;
       long i;
       MEMORYSTATUSEX statex;
       statex.dwLength = sizeof (statex);
       GlobalMemoryStatusEx (&statex);
       ramsize = statex.ullTotalPhys;
       for(i=0;i<ramsize;i = i+1)
       {
           ch = (char*) i;
           readbyte = *ch;
           // if readbyte is readable
           printf("you have readable byte in address: %x , that contain in Binary:",&readbyte);
           for(i=0;i<8;i++)
           {
               if(readbyte&j)
                   printf("1");
               else
                   printf("0");
               j=j>>1;
           }
           putchar('\n');
           // if readbyte is not readable
           printf("Sorry: you cant read this byte: %x",&readbyte);
       }
    }
    Last edited by esmaelmmd; 07-22-2013 at 03:47 AM.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    This program does nothing, and there is no simple test to tell if a byte of memory is readable or not. You are working in C which is abstracted from the operating system and the hardware. If you can't read a byte of memory, the program won't report it, it will just stop working and crash. As far as C is concerned, memory reads ALWAYS return successful. As far as your OS is concerned, trying to read from memory that isn't part of the program doing the reading itself is reason enough to cause that program to be terminated immediately (a security measure).

    If you want to write a memory tester, then you're going to need to be more clever than that - integrating into the OS itself (because almost all memory on the computer will be "unreadable" to C anyway, because it will be restricted by what the operating system allows you to do), or other techniques to determine if a memory read was valid, or running on a system that gives you complete access to the hardware so you can do these kinds of things. This is why things like memtest86 are boot disks (so they have complete control of the PC when they run) and usually try this sort of thing using hardware-specific programming so they know when a byte of RAM is actually unreadable (Machine Check Errors, etc.).

    Either way, it has nothing to do with C other than that being the language you choose to try it in. If readbyte is not readable (because of DEP, etc.) then your program will just be terminated. You might be able to catch a signal and do something with it, but - again - we're out of the realm of C and into machine-specific programming.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    7
    thank you ledow, but i have a home work from college thats read all the bytes from RAM to find a string and print the string address, as how u say that i will not / cant do my home thats impossible?!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This seems like an extension of your other thread.
    Help: need c code that get my memory size

    > but i have a home work from college thats read all the bytes from RAM to find a string and print the string address
    In a userland process, you can only do this on the virtual memory given to your program by the OS. You can't use this technique (for example) to run a userland process to go looking through the memory of another userland process (it's called security).

    A process can inspect itself using Managing Virtual Memory (specifically using the VirtualQueryEx function).

    Inspecting the real physical RAM on a machine with a virtualised OS is way beyond a weekly student assignment.

    Also, GlobalMemoryStatusEx only tells you about the amount of memory, it doesn't provide any address information at all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by esmaelmmd View Post
    i have a home work from college thats read all the bytes from RAM to find a string and print the string address
    That is only possible if you have a non-virtual-memory machine -- something like an old MS-DOS machine or a microcontroller -- or from the kernel side of your operating system. In all cases it's very hardware/OS-specific, and has little to do with C.

    If the course is supposed to be a C programming course, not specific to some old/niche hardware/software, then your instructor is an idiot who is just wasting your time.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also posted here.

    ...then your instructor is an idiot who is just wasting your time.
    Kind of makes you wonder if it's the instructor, the student, or both - considering the C++ headers atop C code, and the "void main()".

  7. #7
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by esmaelmmd View Post
    i just wanna to cheak if the byte is readable or not, i have search for sulution but not find
    hope you will help me
    i have this code i need if tag that cheak if byte is readable
    Code:
    #include <windows.h>
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    void main()
    {
       float ramsize;
       char *ch;
       unsigned int j=128,readbyte;
       long i;
       MEMORYSTATUSEX statex;
       statex.dwLength = sizeof (statex);
       GlobalMemoryStatusEx (&statex);
       ramsize = statex.ullTotalPhys;
       for(i=0;i<ramsize;i = i+1)
       {
           ch = (char*) i;
           readbyte = *ch;
           // if readbyte is readable
           printf("you have readable byte in address: %x , that contain in Binary:",&readbyte);
           for(i=0;i<8;i++)
           {
               if(readbyte&j)
                   printf("1");
               else
                   printf("0");
               j=j>>1;
           }
           putchar('\n');
           // if readbyte is not readable
           printf("Sorry: you cant read this byte: %x",&readbyte);
       }
    }
    I have a hard time believing you're in college. Either you're from another country, or you're some kid trying to do something you're not ready for. All of your posts don't have very flowing sentences, and are hard to follow. You misspell words a lot too. Unless you had a horrible English teacher or something, I don't understand how you're in college.

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    7
    maybe i bad in english, because i am study in another langauge but this not a problem if u can help type what you know about my problem if not please dont replay with something out of my subject, i think my rquest is concept, even if u just look at code you will understand what exactly i want i add this "// if readbyte is not readable" to explain what missing in my code

  9. #9
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by esmaelmmd View Post
    maybe i bad in english, because i am study in another langauge but this not a problem if u can help type what you know about my problem if not please dont replay with something out of my subject, i think my rquest is concept, even if u just look at code you will understand what exactly i want i add this "// if readbyte is not readable" to explain what missing in my code
    Other people talked about your "college" on this post too, so I'm just as bad as them I guess. I'm not trying to insult you, I'm just giving my honest opinion. You don't need to explain to me what your code does, I can read it just fine. If I understood your sentence right, you are bad in English because you study other languages. Well, I took a Spanish class and that didn't make me any worse in English, actually it helped me understand languages better.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by HelpfulPerson View Post
    Other people talked about your "college" on this post too, so I'm just as bad as them I guess. I'm not trying to insult you, I'm just giving my honest opinion. You don't need to explain to me what your code does, I can read it just fine. If I understood your sentence right, you are bad in English because you study other languages. Well, I took a Spanish class and that didn't make me any worse in English, actually it helped me understand languages better.
    Clearly, English is not the OPs first language. You seem to think they're saying that it is, but learning other languages has corroded their first language. Do you realize how ridiculous that sounds?

    By the way, I'm curious to know how you settled on the user name you selected for this forum.

  11. #11
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by Matticus View Post
    Clearly, English is not the OPs first language. You seem to think they're saying that it is, but learning other languages has corroded their first language. Do you realize how ridiculous that sounds?

    By the way, I'm curious to know how you settled on the user name you selected for this forum.

    Matticus, it sounded bad to me too, but when he said "maybe i bad in english, i am study in another language", that makes it sounds like he studied another language so he is bad in English. Which I incorrectly took as English was his home language, but I usually have trouble reading people's posts and paying attention to them closely. Which ends up meaning I make lots of mistakes when I comment.

    My point is, any time I see a post about accessing memory outside your program's memory, I automatically get suspicious. Who knows maybe this was really for a college and for totally non-nefarious purposes. Yet again, there are many kids nowadays trying to search through memory in games and other programs purely for their own uses. That means when I see someone with horrible grammar asking about how to access memory in different sections of their computer, I get suspicious. I shouldn't try to stereotype the OP into this category, but from what I've seen that's what I automatically jump to.

    As for my username, I wanted to help people on this forum, so I decided to choose HelpfulPerson for obvious reasons. Clearly, that is not working out. A lot of times, I make mistakes when I try helping people because I don't read anything very carefully. It's rare that I understand a post fully and am able to answer it completely correct. My idea when I joined was to help people and eventually help my self learn C even more than I have at the current moment, but I end up needing help more than I can help others. Ironic, isn't it?

  12. #12
    Registered User
    Join Date
    Jul 2013
    Posts
    7
    thank you HelpfulPerson, but i am really a student and my lecturer give as a work that get from user an address if it readable print the contain of 2 bytes, I have trying many times to solve this, i have reading books and articles but its really hard to me:S, but finally i get this code but still not work if u can give some help :

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    int main()
    {
     short *ptr,n1=10,f=1,f2=1;
     long i=0,add,j;
     while(f2)
     {
      printf("enter an address:\n");
         scanf("%lu",&add);
      while(f&&f2)
      {
       ptr=(short*)malloc(i*sizeof(short));
       if(ptr)
       {
        if(ptr==(short*)add)
        {
               printf("found:%x\t%x\n",ptr,*ptr+i);
         f2=0;
        }
       }
       else
        f=0;
       i++;
      }
      if(!f)
       puts("Sorry, the address that you intered is not readable");
      for(;i!=0;i=i-1)
          free(ptr+i);
      puts("enter 1 if u want to continue, to exit press 0");
      scanf("%d",&f2);
     }
     return 0;
    }

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by esmaelmmd View Post
    thank you HelpfulPerson, but i am really a student and my lecturer give as a work that get from user an address if it readable print the contain of 2 bytes,
    If address is not readable and you still try to read it - your program should crash.
    Are you sure this is the assignment you've got?

    How do you suppose to recognize that address is not readable and not cause a crash?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I seem to be as lost as everyone else here. Based on your code, it appears you might have to:

    - Allocate memory to a pointer
    - Read a "memory address" from the user (presumably in hex)
    - See if that address is covered in the range of memory allocated
    - If so, dereference the user entered memory address and print the result

    I just wrote a program that does just this, and it seems to work, but it's certainly nothing like I've done before.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The code, indented for sanity.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
      short *ptr, n1 = 10, f = 1, f2 = 1;
      long i = 0, add, j;
      while (f2) {
        printf("enter an address:\n");
        scanf("%lu", &add);
        while (f && f2) {
          ptr = (short *) malloc(i * sizeof(short));
          if (ptr) {
            if (ptr == (short *) add) {
              printf("found:%x\t%x\n", ptr, *ptr + i);
              f2 = 0;
            }
          } else
            f = 0;
          i++;
        }
        if (!f)
          puts("Sorry, the address that you intered is not readable");
        for (; i != 0; i = i - 1)
          free(ptr + i);
        puts("enter 1 if u want to continue, to exit press 0");
        scanf("%d", &f2);
      }
      return 0;
    }
    The only obvious problem here is your use of malloc() and free().

    Inside the loop, you have only one variable (ptr), and MULTIPLE calls to malloc.
    This my friend is a massive memory leak.

    Outside the loop, you have multiple calls to free, but only ONE valid pointer.

    The best thing you could do is something like
    Code:
        while (f && f2) {
          ptr = malloc(i * sizeof(short));  // snipped the cast, it does nothing useful in a C program
          // some other code
          free(ptr);
        }
    which would (if nothing else) at least balance up your malloc and free calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. working set, virtual bytes and private bytes
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2008, 02:39 AM
  2. how to make my code more readable ?
    By jabka in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2007, 06:23 PM
  3. File IO, .ini readable?
    By Zeusbwr in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2004, 12:33 AM
  4. dictionary in readable database
    By jverkoey in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2004, 11:21 PM
  5. readable filemode from st_mode
    By iain in forum Linux Programming
    Replies: 1
    Last Post: 12-04-2002, 06:30 PM

Tags for this Thread