how to write data on the memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vmax
    New Member
    • Nov 2007
    • 7

    how to write data on the memory

    hello,
    i want to write data on memory using turbo C. how can i write data to specific memory location ? and how can i retrive back those data? i want to do this things in turbo C.someone told me to do it using pointer.but i don't get it.


    can any one help me regarding this?

    wiht thanks and regards,
    vmax
  • AHMEDYO
    New Member
    • Nov 2007
    • 112

    #2
    HI...

    if i understand your question, this is a simple code that allocate new string and copy 8 bytes from special memory location to another location and then print it.

    [CODE=cpp]
    #include <mem.h>
    #include <conio.h>
    void main()
    {
    char* MyData;
    char Data2[]="MY DATA";
    clrscr();
    MyData=malloc(8 );
    memcpy((void*)M yData,(void*)Da ta2,7);
    MyData[7]=0;
    printf("%s",MyD ata);
    getche();
    }
    [/CODE]

    also you can use void* type to define your memory location not just char* type can be used, and you can use memcpy to transfer data between memort locations with any other type.

    Kind Regards

    Comment

    • vmax
      New Member
      • Nov 2007
      • 7

      #3
      hello,
      Thank you very much for giving me reply.
      i understood your code. i have tried your code. but i want to write data on a specific memeory location let say FF02B000. you have mentioned to do this thing using void*type. but i am not getting it. can you suggest me or can you give some reference coding for that?

      with thanks and regards,
      vmax

      Comment

      • AHMEDYO
        New Member
        • Nov 2007
        • 112

        #4
        HI vmax..

        yes you can do that with void type, i will show you two examples

        first example show the same function i was descript but with void* type

        [CODE=cpp] #include <mem.h>
        #include <conio.h>
        void main()
        {
        void* MyData;
        char Data2[]="MY DATA";
        clrscr();
        MyData=malloc(8 );
        memcpy(MyData,( void*)Data2,7);
        ((char*)MyData)[7]=0; /*you must tell the complier about void* type , just void* have no meaning except it have address for memory location*/
        printf("%s",(ch ar*)MyData);
        getche();
        }
        [/CODE]

        Second Example: show how to use structures and user defined type with memcpy function, it create two POINTS structure and then copy addresses for these two variables to new memory location, and then use this variable as array of pointers of POINTS structure

        [CODE=cpp] #include <mem.h>
        #include <conio.h>
        typedef struct POINTS
        {
        int x;
        int y;
        }POINTST;


        void main()
        {
        void* MemData;
        POINTST Data2,Data3;
        POINTST** Tempptr=0,**Dat aptr=0;
        Data2.x=30;
        Data2.y=10;
        Data3.x=20;
        Data3.y=40;
        MemData=malloc( sizeof(POINTST* )*2);
        Tempptr=&Data2;
        memcpy(MemData, &Tempptr,sizeof (POINTST*));
        Tempptr=&Data3;
        memcpy((((unsig ned int)MemData)+si zeof(POINTST*)) ,&Tempptr,sizeo f(POINTST*) );
        Dataptr=(POINTS T**)MemData;
        clrscr();
        printf("%s %u %c %u %c","(X0,Y0) is :",Dataptr[0]->x,',',Datapt r[0]->y,'\n');
        printf("%s %u %c %u %c","(X1,Y1) is :",Dataptr[1]->x,',',Datapt r[1]->y,'\n');
        getche();
        }
        [/CODE]

        i hope this help you!, and if this code is not meet your requirements , then im waiting your reply no problem, we will got it :D

        Kind Regards

        Comment

        • vmax
          New Member
          • Nov 2007
          • 7

          #5
          hello sir,
          i have tried your code. but it is not according to my requirements.fi rst example code is running successfully but it is printing my data only. how can i know that my data is placed at specific location?
          in second example i am getting lot many erreor.although , i had included stdlib.h and stdio.h.
          i want to write data on memory location say ff02d000. i need code in which i can write this address.i need to do this thing because my usb port is in memory range of ff02b00 to ff02bfff.and i want to access usb port by doing a programming in C.
          i hope you understood my requrements.sti ll you have doubt in my requirements let me know.

          with tthanks and regars,
          vmax.

          Comment

          • mohammadazim
            New Member
            • Aug 2007
            • 28

            #6
            Hi friend,

            You can't do that. I think I understood your requirement. You want to write data on ram from say 0x00fadced some addrss to some range. This not possible.
            First thing is a programer can never tells the machine to write at a specific address unless the address is of a hardware port(That are fixed).
            To write to memory you first need to allocate memory. There are two memory areas HEAP and STACK from where mempry can be allocated. when you declare variables in a function, the memory is allcated by from stack. Stack provides a temprary memory space to a function variable and as soon as you return from the function the memory is lost(reused by some other). Take follwoing example
            Code:
            char * func1()
                {
                char s[10];
                memset(s, '\0', 10);
                char c[] = "Hello";
                memcpy(s,c, 5); // 5 chars is Hello.
                printf("%s", s);
                return s;
                }
            
            void main ()
                {
                char s* = func1();
                printf("%s", s); // 99.99% it will crash here.
                }
            output:
             Hello
            
            expected output:
             Hello
             Hello
            You can see in above example that in finc1() you can do whatever you want with s. But if you try to use it outside func1 you can't. reason is, it is a stack variable and memory allocated to it is collected back by memory management process of kernel(os) as soon as function returns.

            To have memory which can can be used throughout the program, you can allocate it from heap. In C you can use malloc and in C++ you can use new operator. See this example with memory allocation from heap


            Code:
            char * func1()
                {
            //  ... Following allocate 10 consecutive bytes of memory in heap and return
            //  ... a void pointer to the first byte. So it needs a cast.
                char *s = (char*)malloc(sizeof(char) * 10); 
                memset(s, '\0', 10);
                char c[] = "Hello";
                memcpy(s,c, 5); // 5 chars is Hello.
                printf("%s", s);
                return s;
                }
            
            void main ()
                {
                char *n = func1();
                printf("%s", n); // 99.99% it will crash here.
            //  ... Don't forget to free the memory once you no more need it.
                free(n);
                }
            output:
             Hello
             Hello
            expected output:
             Hello
             Hello
            Above code will give expected outout.

            But the moral of the story is that we first request memory management process to allocate some space for us. It gives us the start address and we use that. We can never use any address of our own. Program will crash.
            Secondly don't think the memory pointed by the pointer variable is the real memory on ram. You have logical address of mem and the real is decided based on/by the processor(segme nts and ...). You must have read microprocessors (8086). But you should never bother about these things and you will never need such thing.
            Third is that the story don't end here. Even memory allocated on heap can't be used between two different processes(two programs running simultaneously) . Because each process has its own memory space provided by process management of OS. For such purpose of data transfer we use other means knwon as inter process communication.
            For basic concepts of memory and poiters you can read different books on C/C++. They are good enough. But if you have interest and you probably will have after some time, you can read Network programming by Richard Steven and Device drivers by O'really(spel is wrong may be) publications.

            Thanks

            Comment

            • vmax
              New Member
              • Nov 2007
              • 7

              #7
              hello sr,
              thanks for your very useful reply.
              now my application is to send the data to USB 2.0. is there any option available to me? can i do programming for that in VC++? if i want to send data to USB port, is it possible in other ways?

              with thanks and regards,
              vmax.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                now my application is to send the data to USB 2.0.
                That’s rather difficult. USB 2.0 happens to be a standard, so I’m not sure how you would send data to a standard. I realize you mean something else, but you need to get the specifics down. You’ll find that in programming, you need to be precise.
                can i do programming for that in VC++? if i want to send data to USB port, is it possible in other ways?
                You can program USB communication, certainly. Have you done any Googling and reading? If you haven’t, then there’s no point in us saying anything as we will just be repeating a 5 second Google search.

                Comment

                • vmax
                  New Member
                  • Nov 2007
                  • 7

                  #9
                  hello sir,
                  Thanks for giving me reply.
                  yes, i understood that i can't send data to standards.
                  i have done googling on programming using VC++. one thing i can understand for all searching is that i have to write my driver in any of this language C/C++/VC++.
                  i have not too much knowledge about C++/VC++. i need some basic information regarding to writing a driver for USB device.


                  with thanks and regards,
                  vmax

                  Comment

                  Working...