473,224 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 9308
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

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

Similar topics

by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into...
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
by: OUSoonerTaz | last post by:
We are randomly getting this error message on our development and staging machines: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.; at...
by: adypoly | last post by:
Hi guys... I am having a typical problem in using one of the native dll in C# I'll explain what am trying to do, I've a dll written in C language which i am trying to include in my C# project,...
by: Sugandh Jain | last post by:
Hi, I am getting the error message Attempted to read or write protected memory. This is often an indication that other memory is corrupt. It was not coming until yet, for around 2 months. Now,...
by: sriram347 | last post by:
Hi I am a newbie to ASP.NET. I developed a web page (project type is web application) and I keep getting this error. B]Error message : "System.AccessViolation Exception attempted to read or...
by: blazedaces | last post by:
So I have a program below which writes an excel file with multiple sheets based on inputs of sheet names, data, cell types, etc. It uses Apache POI, which is currently the only thing I found...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.