"The memory could not be written" error

This is a discussion on "The memory could not be written" error within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I need some help here. I executed my visual c++ program for several thousand iterations and at a ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    "The memory could not be written" error

    Hi guys,

    I need some help here.

    I executed my visual c++ program for several thousand iterations and at a point, my program halted and this error message appeared:

    The instruction at "0x0040628e" referenced memory at "0x00000004". The memory could not be "written".

    Does anyone know what went wrong?

    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Your program performed an invalid operation with a pointer that had the result of attempting to write to an invalid area of memory. The error message is the way windows tells you it has detected and refused to allow the invalid operation.

    If your code used arrays (rather than pointers) your code may have fallen off the end of an array. For example, if array x had 10 elements, your code attempted to modify x[n] where n >= 10.

    There are many ways code can do this sort of thing: you will need to work out what the instruction is, and what line(s) of source code it corresponds to.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by grumpy View Post
    Your program performed an invalid operation with a pointer that had the result of attempting to write to an invalid area of memory. The error message is the way windows tells you it has detected and refused to allow the invalid operation.

    If your code used arrays (rather than pointers) your code may have fallen off the end of an array. For example, if array x had 10 elements, your code attempted to modify x[n] where n >= 10.

    There are many ways code can do this sort of thing: you will need to work out what the instruction is, and what line(s) of source code it corresponds to.
    Actually, the memory address "0x00000004" indicates that it is likely that he tried to write to an element of a pointer that is uninitialized or set to NULL.

    Your code is likely to look something like this:
    Code:
    int *p;
    p = NULL; /* Might not be set, the compiler might have done this automatically */
    ...
    p[1] = something;
    It doesn't have to be the first index that is written to; if the type is a char it was probably the 4th element.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Hi,

    Thanks for the replies. Could be due to memory leakage as well?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by elmvin_s View Post
    Hi,

    Thanks for the replies. Could be due to memory leakage as well?
    ... No.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Dear EVOEx,

    I am very new to C++. Could you further explain the problem?

    The program could run fine for the first 2,000 iterations. It is only after that the error message appeared. If the problem was writting an element of a pointer which is uninitialised, wouldn't the program crash at the very beginning of execution, i.e. the first iteration?

    Thanks for your help. Really appreciate it.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    It wouldn't be directly caused by a memory leak, but what might happen is that you might be using new no_throw, or even malloc (which most of the time shouldn't appear in C++ code) and the request might be failing and return NULL. Then your code is probably not checking for this error and is continuing on blindly with a NULL pointer.

    Well that's one way at least. Do you know that you have a memory leak?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by EVOEx View Post
    Actually, the memory address "0x00000004" indicates that it is likely that he tried to write to an element of a pointer that is uninitialized or set to NULL.
    That is actually an invalid pointer operation, as I described in my previous post.
    Quote Originally Posted by EVOEx View Post
    Your code is likely to look something like this:
    Code:
    int *p;
    p = NULL; /* Might not be set, the compiler might have done this automatically */
    ...
    p[1] = something;
    It doesn't have to be the first index that is written to; if the type is a char it was probably the 4th element.
    That's only one of many possible scenarios by which code might write to an address with a value of 4. I've also seen such things come about through errors where the programmer thought he was doing this;
    Code:
         int *p = some_valid_address();
         *p += result_of_long_series_of_computations();
    but left out the asterix in the second line. While compilers often warn about such things (eg visual C++ complains about a suspicious pointer conversion, IIRC) programmers often ignore the warnings.

    In a program that fails after many iterations, this type of scenario is more likely than code explicitly incrementing or molesting a NULL pointer.

    However, there are many, many, other possibilities.
    Last edited by grumpy; 12-12-2009 at 03:33 PM.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by grumpy View Post
    That is actually an invalid pointer operation, as I described in my previous post.

    That's only one of many possible scenarios by which code might write to an address with a value of 4. I've also seen such things come about through errors where the programmer thought he was doing this;
    Code:
         int *p = some_valid_address();
         *p += result_of_long_series_of_computations();
    but left out the asterix in the second line. While compilers often warn about such things (eg visual C++ complains about a suspicious pointer conversion, IIRC) programmers often ignore the warnings.

    In a program that fails after many iterations, this type of scenario is more likely than code explicitly incrementing or molesting a NULL pointer.

    However, there are many, many, other possibilities.
    My money is on the second scenario or a derivation thereof where a ptr is being incremented and overflows to zero....

    Jeff

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    A debugger is good for spotting the cause of this sort of thing. Use it to find exactly where the error occurs while stepping through your code. Watch expressions and break points will help you.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21