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

How to handle the errors memory cannot be read or written

Why does these 2 errors occur:

1.Memory could not be read
2.Memory could not be written

If it occurs how to handle it
Oct 7 '07 #1
7 1906
weaknessforcats
9,208 Expert Mod 8TB
Memory cannot be read if the address does not belong to your process space.

Memory cannot be written if the address does not belong to your process space.

Use correct addresses.
Oct 7 '07 #2
JosAH
11,448 Expert 8TB
Memory cannot be written if the address does not belong to your process space.

Use correct addresses.
Memory also can't be written if a pointer points straight into rom space; emulators
and debuggers are quite smart when it comes to signalling that.

kind regards,

Jos
Oct 7 '07 #3
Memory also can't be written if a pointer points straight into rom space; emulators
and debuggers are quite smart when it comes to signalling that.

kind regards,

Jos
But even for simple input statement i am getting these error..

For eg:
int num;scanf("%d",num);
For this i am getting the same error what should i do to get rid of this..Please help me....Because i can't do any of the programms
Oct 8 '07 #4
mattmao
121 100+
But even for simple input statement i am getting these error..

For eg:
int num;scanf("%d",num);
For this i am getting the same error what should i do to get rid of this..Please help me....Because i can't do any of the programms
Hello man, keep an eye on the correct syntax for the scanf statement!

That is:

Expand|Select|Wrap|Line Numbers
  1. int num;
  2. scanf("%d", &num);
  3.  
Note the "&" symbol before the variable name "num". Your lecture notes/reference book would tell you why.

But if you want to print it out:
Expand|Select|Wrap|Line Numbers
  1. printf("%d", num);
Oct 8 '07 #5
Hello man, keep an eye on the correct syntax for the scanf statement!

That is:

Expand|Select|Wrap|Line Numbers
  1. int num;
  2. scanf("%d", &num);
  3.  
Note the "&" symbol before the variable name "num". Your lecture notes/reference book would tell you why.

But if you want to print it out:
Expand|Select|Wrap|Line Numbers
  1. printf("%d", num);
Yes I know '&' specify the address to where the int should be stored. Thus we specify the address-of operator (more on this when we get to pointers). Therefore,
scanf("%d", &num);
will put an int into num correctly

But at times
scanf("%d",num);
this also work smoothly without any error....Why is that?
Oct 8 '07 #6
mattmao
121 100+
Yes I know '&' specify the address to where the int should be stored. Thus we specify the address-of operator (more on this when we get to pointers). Therefore,
scanf("%d", &num);
will put an int into num correctly

But at times
scanf("%d",num);
this also work smoothly without any error....Why is that?
It would be ALWAYS compiled with no error simply because it doesn't violate any syntax check, but this doesn't mean your code is CORRECT.

Expand|Select|Wrap|Line Numbers
  1. scanf("%d",num);
This reads: Find the memory address assigned to the "num" pointer variable and then put the value of an integer taken from standard input into the content starts from that position.

Is this what you want to do? I guess not...

AND because you haven't declare the pointer num, its address in memory is not allocated to your program. That is why you got this Segmentation Fault.

So just use
Expand|Select|Wrap|Line Numbers
  1. scanf("%d", &num);
scanf statement takes an ADDRESS, not a VARIABLE.




Plus, this is correct:

Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int * num = &a;
  3.  
  4. scanf("%d", num);
  5. printf("%d", *num);
Why? Because num now is an ADDRESS.
Oct 8 '07 #7
It would be ALWAYS compiled with no error simply because it doesn't violate any syntax check, but this doesn't mean your code is CORRECT.

Expand|Select|Wrap|Line Numbers
  1. scanf("%d",num);
This reads: Find the memory address assigned to the "num" pointer variable and then put the value of an integer taken from standard input into the content of that position.

Is this what you want to do? I guess not...

So just use
Expand|Select|Wrap|Line Numbers
  1. scanf("%d", &num);
Thanks a lot for your reply....I really learnt an important thing in C.....
Oct 8 '07 #8

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
5
by: Eddie | last post by:
I have a MySQL-server running Innodb. We have installed ~ 2GB of memory in the server. In spite of this MySQL keeps crashing due to out-of-memory errors. The server is a dual xeon i686 running...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
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...
2
by: Rob | last post by:
I wanted to pass ByVal a Me.Handle() to a CStyle DLL. The code for the DLL is as follows: void __stdcall WriteWindowHandle(HWND wHandle) { DATASTRUCT ds; DWORD dwBytesWritten; ds.wndHandle...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.