473,408 Members | 2,813 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,408 software developers and data experts.

how to write data on the memory

7
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
Nov 23 '07 #1
8 9407
AHMEDYO
112 100+
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.

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. }
  14.  
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
vmax
7
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
Nov 24 '07 #3
AHMEDYO
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. }
  14.  
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;
  8.  
  9.  
  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. }
  30.  
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
vmax
7
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,
vmax.
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.     }
  10.  
  11. void main ()
  12.     {
  13.     char s* = func1();
  14.     printf("%s", s); // 99.99% it will crash here.
  15.     }
  16. output:
  17.  Hello
  18.  
  19. expected output:
  20.  Hello
  21.  Hello
  22.  
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.     }
  12.  
  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
  26.  
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.

Thanks
Nov 26 '07 #6
vmax
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.
Nov 27 '07 #7
oler1s
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
vmax
7
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
Nov 28 '07 #9

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

Similar topics

1
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...
4
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...
18
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...
16
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...
27
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...
3
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...
2
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,...
6
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,...
3
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...
8
blazedaces
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.