GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Mar-2012, 13:25
alimujtaba786 alimujtaba786 is offline
New Member
 
Join Date: Mar 2012
Posts: 1
alimujtaba786 is on a distinguished road

Help in Identifying Memory Segments


Write a program that

a. Prints starting address of the program alongwith the contents of each byte in ASCII with increment of 1 up to 65535 (in a file).

b. Identfy program segments.

c. Save the file on HDD.

=================================================
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int f=10;
main()
{    
     char a='a';
     char* ptr=&a;
     printf ("fffff %d",ptr);
    // long address=0x0022ff74;
     
     FILE *file; 
file = fopen("file.txt","a+"); /* apend file (add text to 
a file or create a file if it does not exist.*/ 

int i=1;
while (i<=65535)
{
fprintf(file,"the address is 0x%08x or %d (in decimal) and the value at this address is %c\n",ptr,ptr,*ptr);
ptr=ptr-1;
i++;
}
fclose(file); /*done!*/ 
getchar(); /* pause and wait for key */ 
return 0; 
     

getch();
}

I have done till the part that 65535 lines with addresses and what's store in that address is stored in a file, How can I Identify in which part of memory each address is stored ( Is it in Stack, Heap , Data Segment, code segment )
I want to show where each address belongs separately in the same Printf line.
  #2  
Old 07-Mar-2012, 04:14
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 686
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Help in Identifying Memory Segments


Quote:
Originally Posted by alimujtaba786
Write a program that

a. Prints starting address of the program alongwith the contents of each byte in ASCII with increment of 1 up to 65535 (in a file).

b. Identfy program segments.

c. Save the file on HDD.

=================================================
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int f=10;
main()
{    
     char a='a';
     char* ptr=&a;
     printf ("fffff %d",ptr);
    // long address=0x0022ff74;
     
     FILE *file; 
file = fopen("file.txt","a+"); /* apend file (add text to 
a file or create a file if it does not exist.*/ 

int i=1;
while (i<=65535)
{
fprintf(file,"the address is 0x%08x or %d (in decimal) and the value at this address is %c\n",ptr,ptr,*ptr);
ptr=ptr-1;
i++;
}
fclose(file); /*done!*/ 
getchar(); /* pause and wait for key */ 
return 0; 
     

getch();
}

I have done till the part that 65535 lines with addresses and what's store in that address is stored in a file, How can I Identify in which part of memory each address is stored ( Is it in Stack, Heap , Data Segment, code segment )
I want to show where each address belongs separately in the same Printf line.

I'm not sure what this noise is, but it doesn't sound very much like the requirements you've listed. I don't think that it wants you to print some random value of the addresses you're using. If we suggest that the "starting point" of the program is main, why not print the address of main? Is asks for the BYTE value of the PROGRAM in ASCII (weird, since a lot of bytes won't nicely fit into 7-bit ASCII)...UP TO 16K. If your program is less than 16K, then you can write the complete contents into a file.

Here is what I *think* it wants....open the file, which is the program itself. Print the "entry point" (starting address) of it and then a binary translation from binary to ASCII of each byte of the contents of the program. At some point, you're going to have to look at the structure of the program and determine what type of object file format is used. Based on the kind of object format, you will then be able to determine how to "decode" the format into the respective load segments.

Since you didn't mention your environment, we're left with only guessing as to what it might be. That's not a very productive activity. Since you didn't use system("pause"); we may assume that you're using Linux or some other non-windoze world...but that's not necessarily accurate. Under Linux, the default executable object type is ELF. There are plenty of ways to understand what ELF contains, including a library for reading/writing ELF files that you can download, include and link against that will do much of the "grunt work" for you.

If you're using a different platform, the object file type is likely different as is the organization of it. As for determining the linkage and which sections are used, an easy thing to do is create a map file for the program. It will output the contents of the program's symbols and data into the various sections used during linking. This can all be "decoded" from the program itself, because the "loader" must do it in order to run the program and therefore the program must contain this information in order for the loader to effectively work. External shared libraries that must be loaded, the offset of the starting/entry point into the program, any uninitialized data, stack information, etc. will be found in the object file contents.

What you're doing is randomly throwing out information from inside your program that has nothing at all to do with the real contents of your program. What you need to do is to rethink what you're doing completely, which is to first OPEN the file that is your program and "read" it to determine the contents of it. At least...this is how I interpret the requirements as stated. Your whole while loop is badly off the mark as far as I can tell. At best, it does absolutely nothing in terms of meeting the requirements. The same can be said for printing the address of "a." It is neither the starting point of your program or representative of any of the requirements, but is merely a random address that can possibly suggest where in the memory map your stack is located...and depending on your system, it may likely be a virtual address and not a physical address.

In other words, I think that you will want to seek greater clarification as to what is expected from you, but I suspect that it is closer to what I'm saying than what you are doing. At the BARE MINIMUM you will want to open TWO files. One is the program, which you're going to read from and the other is the output file to which you'll write the contents per the requirements.

I can see how the requirements may be misleading from the "a" requirement, but the "b" requirement suggests that you know something intrinsic about the "program" that is based on its file/object structure and not something from within the program itself. You may easily print out the address of main by simply using the address of main in an fprintf statement that writes to your output file. However, the "b" requirement suggests that you must decode the object format such that it tells you of the layout (in memory) of the program and that would also include the starting address (entry point) of the program...so, you should probably just decode the file and write to the output from the contents of the file based on what you decode. The "2nd part of the 'a' requirement" is a bit weird as stated, maybe you should write the byte value expressed as a hexadecimal value written in ASCII? I'd seek clarification and, lacking that, write a "hex dump" of the program contents. If you're running a programmer's operating system like Unix/Linux, all of this is rather easy and straight-forward. There are similar tools for Windoze, but they're not as readily available or as conveniently accessible as Unix provides. You can, at least, use such tools to "sanity check" your output and see that what you're reading and writing is (at least) consistent with the output from other tools designed to do similar work.

As you may have determined by now, the program contents are going to be "platform specific" so that there isn't much that I could code to show you without knowing what platform you're using.

Does any of this make sense to you?

Here is what I recommend:

Open a PROGRAM file either as an argument passed into the execution of your program (argv[0] is the file name of your program) or taken as user input from somewhere inside of your program. If Windoze, use binary mode to open it. Open an "output" file. Use binary mode if you're using the horribly clunky and dysfunctional Microsoft "world."

Start with simply writing to your output file the translated (from binary to ASCII) bytes of the file. Hint: Use fprintf using the FILE* of your output file and a hex "%x" or "%X" format specifier.

...close your file after reading the entire contents of the input file and then compare the hex values with a hex editor/lister that runs on your platform. If using Linux, use xxd.

Something like:

CPP / C++ / C Code:
#include <stdio.h>

int main(int argc, char* argv[]) {
    FILE* fp_program;
    FILE* fp_output;
    char byte;
    size_t done;
    int count = 0;

    fp_program = fopen(argv[0], "rb"); /* b = broken windoze */
    if (fp_program != NULL) {
        fp_output = fopen("output.txt", "w+");
        if (fp_output != NULL) {
            do {
                done = fread(&byte, 1, 1, fp_program);
                if (done > 0) {
                    fprintf(fp_output, "%.2X ", byte);
                    if (++count == 10) {
                        fprintf(fp_output, "\n");
                        count = 0;
                    }
                }
            } while (done != 0);
            fprintf(fp_output, "\n");
            fclose(fp_output);
        }
        fclose(fp_program);
    }
    return 0;
}


...may help you understand what I *think* is intended by the requirements you've posted. You can add another "byte counter" that ensures that you don't go over the 16K limit imposed by the requirements to break out of the do/while loop that read/writes the bytes to ASCII (hex chars).


MxB
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GuardHosts.com - Unmetered 100 Mbps VPS from $4/Month (FR) Lokesh C Managed / Dedicated Servers 0 17-Dec-2011 11:07
How to get the amount of memory allocated from a pointer? cpit C Programming Language 2 11-Aug-2011 17:46
How to see unfreed memory? cpit C Programming Language 3 31-May-2011 07:22
Memory Management with C nkhambal C Programming Language 4 26-Sep-2004 22:40
Why seg fault in char array? nusstu C Programming Language 11 24-Aug-2004 16:10

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:55.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.