470,870 Members | 992 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

how to write data on the memory

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,
Nov 23 '07 #1
8 8238
112 100+

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.

Expand|Select|Wrap|Line Numbers
  1. #include <mem.h>
  2. #include <conio.h> 
  3. void main()
  4. {
  5. char* MyData;
  6. char Data2[]="MY DATA";
  7. clrscr();
  8. MyData=malloc(8);
  9. memcpy((void*)MyData,(void*)Data2,7);
  10. MyData[7]=0;
  11. printf("%s",MyData);
  12. getche();
  13. }
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
Nov 23 '07 #2
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,
Nov 24 '07 #3
112 100+
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

Expand|Select|Wrap|Line Numbers
  1.  #include <mem.h>
  2. #include <conio.h> 
  3. void main()
  4. {
  5. void* MyData;
  6. char Data2[]="MY DATA";
  7. clrscr();
  8. MyData=malloc(8);
  9. memcpy(MyData,(void*)Data2,7);
  10. ((char*)MyData)[7]=0; /*you must tell the complier about void* type , just void* have no meaning except it have address for memory location*/
  11. printf("%s",(char*)MyData);
  12. getche();
  13. }
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

Expand|Select|Wrap|Line Numbers
  1.  #include <mem.h>
  2. #include <conio.h> 
  3. typedef struct POINTS
  4. {
  5. int x;
  6. int y;
  7. }POINTST;
  10. void main()
  11. {
  12. void* MemData;
  13. POINTST Data2,Data3;
  14. POINTST** Tempptr=0,**Dataptr=0;
  15. Data2.x=30;
  16. Data2.y=10;
  17. Data3.x=20;
  18. Data3.y=40;
  19. MemData=malloc(sizeof(POINTST*)*2);
  20. Tempptr=&Data2;
  21. memcpy(MemData,&Tempptr,sizeof(POINTST*));
  22. Tempptr=&Data3;
  23. memcpy((((unsigned int)MemData)+sizeof(POINTST*)),&Tempptr,sizeof(POINTST*) );
  24. Dataptr=(POINTST**)MemData;
  25. clrscr();
  26. printf("%s %u %c %u %c","(X0,Y0) is :",Dataptr[0]->x,',',Dataptr[0]->y,'\n');
  27. printf("%s %u %c %u %c","(X1,Y1) is :",Dataptr[1]->x,',',Dataptr[1]->y,'\n');
  28. getche();
  29. }
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
Nov 24 '07 #4
hello sir,
i have tried your code. but it is not according to my requirements.first 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.still you have doubt in my requirements let me know.

with tthanks and regars,
Nov 26 '07 #5
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
Expand|Select|Wrap|Line Numbers
  1. char * func1()
  2.     {
  3.     char s[10];
  4.     memset(s, '\0', 10);
  5.     char c[] = "Hello";
  6.     memcpy(s,c, 5); // 5 chars is Hello.
  7.     printf("%s", s);
  8.     return s;
  9.     }
  11. void main ()
  12.     {
  13.     char s* = func1();
  14.     printf("%s", s); // 99.99% it will crash here.
  15.     }
  16. output:
  17.  Hello
  19. expected output:
  20.  Hello
  21.  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

Expand|Select|Wrap|Line Numbers
  1. char * func1()
  2.     {
  3. //  ... Following allocate 10 consecutive bytes of memory in heap and return
  4. //  ... a void pointer to the first byte. So it needs a cast.
  5.     char *s = (char*)malloc(sizeof(char) * 10); 
  6.     memset(s, '\0', 10);
  7.     char c[] = "Hello";
  8.     memcpy(s,c, 5); // 5 chars is Hello.
  9.     printf("%s", s);
  10.     return s;
  11.     }
  13. void main ()
  14.     {
  15.     char *n = func1();
  16.     printf("%s", n); // 99.99% it will crash here.
  17. //  ... Don't forget to free the memory once you no more need it.
  18.     free(n);
  19.     }
  20. output:
  21.  Hello
  22.  Hello
  23. expected output:
  24.  Hello
  25.  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(segments 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.

Nov 26 '07 #6
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,
Nov 27 '07 #7
671 Expert 512MB
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.
Nov 27 '07 #8
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,
Nov 28 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ellixis | last post: by
4 posts views Thread by Stephan Tobies | last post: by
18 posts views Thread by jacob navia | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.