Trying to read random memoy causes error

Pages: 12
A long time ago, 5 years, I made a simple C++ program that would allow you to enter a number and it would use an array to browse through the computers memory. Not change it, just take a look at the values stored inside. It worked. I'm brushing up on C++ now, I've barely made anything in these past 4 years, and I don't remember the source code. Anyways, I made this program, I never really understood pointers very well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"
#include <iostream>
using namespace System;

void main() {
	int myarray[1];
	int *integer, count, memory;
	for (count = 0; count != 10; count+=1) {
	std::cout << "Enter a memory location: \n ";
	std::cin >> memory;
	*integer = memory;
	std::cout << myarray[*integer]; /*I just realized while writing up this post that using a pointer to index a array indice doesn't do what I wanted. Its supposed to just go out of the arrays range.*/
	}
	return;
}


When I run this an enter something into the variable memory, I get a rather serious looking error that says I may be corrupting memory. Sounds pretty bad if you ask me. Should I try to use an array to browse the memory? By using an array, I mean using index out of range to sift through the memory.

Can this error hurt my computer? Is it permanent? Will windows fix it? Will restarting windows fix it?

I'm going to try using an array instead of a pointer, and not run this program again...

Edit: I edited the program to use an array and variable, and its not giving me that error. Can you tell me why the numbers I get out of the memory are so huge and random? I would expect something simlple like 2000, or 100, but none of these make any sense!


Another edit: I don't think you can tell a pointer what it should point to can you? like &i = 25?
Last edited on
Reading of a memory location that does not exist or that the OS doesn't think you should have access to is forbidden.

http://en.wikipedia.org/wiki/Segmentation_fault

Another edit: I don't think you can tell a pointer what it should point to can you? like &i = 25?


You can set a pointer to whatever value you like, and thus by dereferencing it, you can attempt to read/write to any memory location you like. Whether or not it works is a different matter (see link above). It's standard in writing code that has to work with actual physical hardware to directly set pointer values. For example,

1
2
3
int* p_LED;
p_LED = 0xFEEDFACE; // A permanent, hard-wired special memory location that affects the hardware
*p_LED = 1; // Set the value 1 in memory location 0xFEEDFACE 


Can you tell me why the numbers I get out of the memory are so huge and random?


If you're reading memory that you've never set with a value, it will have some garbage data in it. Why would it be something simple? It'll be whatever value was last put into that particular piece of memory. Could be anything.
Last edited on
closed account (z05DSL3A)
Moschops wrote:
...
p_LED = 0xFEEDFACE; // A permanent, hard-wired special memory location that affects the hardware
...

really? A value of type int assigned to an int*, without an error?
May need casting, depending on your compiler. A lot1 of compilers for embedded systems have extensions to let this happen without it, as it's so common and useful, so I am now in the habit of doing this and fixing it if the compiler whinges.

[1] Well ,I say "a lot". I mean "many of the ones I have used". I've not done a thorough analysis of every compiler built for embedded code. I mostly worked with TigerSHARC, SHARC and Blackfin.
Last edited on
closed account (z05DSL3A)
so I am now in the habit of doing this and fixing it if the compiler whinges.

I tend to be the other way round and cast when I see a type mismatch (not C style though).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

//void main()
int main()
{
    int x = 10;
    int y = reinterpret_cast<int>(&x);
    int * ptr;
    ptr = reinterpret_cast<int*>(y);

    std::cout << "Address of x is 0x" << &x << "  and its value is " << x << std::endl;
    std::cout << "Value of y is     " << std::hex << y << std::endl;
    std::cout << "ptr points to   0x" << ptr << " and that has a value of " << std::dec << *ptr << std::endl;

    return 0;
}

Last edited on
void main()

I do still always go with int main (), though ;)
closed account (z05DSL3A)
Oops, where did that come from. (>.<)
Why's that, moschops? The void main () trick is something I learned from a class mate, though I don't really understand how the main function can return something. I know it has something to do with returning error codes, but I am unaware of the details.
Because it's not C++. The C++ standard makes it clear that main returns an int, and a C++ compliant compiler will reject anything else.

By all means, take advantage of lax compilers to abuse C++ to your own ends, but know when you're writing something that isn't C++.


While I'm here, what's this about?

using namespace System;

Isn't the System namespace a C# thing?


Last edited on
using namespace System;
I think it was autogenerated by the compiler.

By all means, take advantage of lax compilers to abuse C++ to your own ends, but know when you're writing something that isn't C++.


Sorry, have we got a problem?
I think it was autogenerated by the compiler.


The compiler doesn't generate text. It takes in text and turns out binary objects.

Sorry, have we got a problem?

The problem with writing code that isn't actually C++ is that if you ever have to change compiler, there's a good chance that your code will behave in a different way. Also, if you ever have to share code with someone else, or jointly work on code, it will probably be expected that your code is C++, rather than a rough approximation of C++.

If you don't want this sort of thing pointed out, that's fine; just say so.
By autogenerated, I meant it was generated when I started the project (a template). MS Visual Studio Express has more than your standard empty project.

So when this was created it put that in there, I thought I erased it but I must have missed it. Also I found some things wrong other than that with that code and changed it here and in my file, but for some reason this site isn't working correctly with my browser (firefox, and I can't edit posts).

I didn't know that wasn't C++, like I said it was a trick I learned from a classmate. We were newbies back then, high school. He never told me why it works, or if it was another language or something. After all, a C++ function can return nothing, with a VOID type identifier, shouldn't the main function be able to? I don't know how main works, or why it has to return a value (probably something to tell the OS that the program failed or succeeded). A function which returns nothing returns NOTHING, but now I think the main function must return something so changing that would probably be a bad thing...

I want HELP, IE, point out anything that's wrong and help me understand it, not be a dick about it. By all means point something out if its wrong.
Last edited on
Also, I never really had much practice with C++, I just learned the different identifiers and how they are put together.
evilpickles wrote:
probably something to tell the OS that the program failed or succeeded

That's about right. See:
http://en.wikipedia.org/wiki/Exit_status

A more immediate reason is that the C++ standard simply says so:
3.6.1.2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. [...]


Note that you don't actually have to explicity return 0 at the end of main due to this part:
3.6.1.5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;



From wikipedia:

The specific set of codes returned is unique to the program that sets it. Typically it indicates success or failure. The value of the code returned by the function or program may indicate a specific cause of failure.


And that's because of try...catch blocks or something similar in each language, which tell the os or a program what to do in each case of error, correct?

I also don't understand these lines in the sites tutorial:

char* pchar; pchar=(char*)data; ++(*pchar);

I get that it declares a char pointer called pchar, but not the second or third statements.

Last edited on
DANGN IT WHY ISNT EDIY WORKING!?


I also don't understand these lines in the sites tutorial:

char* pchar; pchar=(char*)data; ++(*pchar);

I get that it declares a char pointer called pchar, but not the second or third statements.


The following assigns pchar to data, as a char *
The following (char *) is casting data to a char *

pchar=(char*)data;

This takes what pchar is pointing to and adds 1 to it.

++(*pchar);
if you want to read values from memory... this code will work...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main ()
{
   char nothing[80];
   cin >> nothing;
   for (int i;;i++)
   {
      cout << nothing[i];
      if (i > 16000)
      {
         nothing = &nothing[i];
         i = 0;
      )
   }
   return 0;
}


an array is a pointer...
so char nothing[80] is a location in memory with exactly 79 spot of room for the char type...
so anything 80 or more if placed in nothing[x] will be memory that you were not given
the if then set the location of nothing to to location of nothing[i] and sets i to zero that way i does not become bigger than it can be...
Last edited on
I want HELP, IE, point out anything that's wrong and help me understand it, not be a dick about it.


Was I being unpleasant about it? Reading back over my posts, I don't think I typed anything that could be construed as a personal attack. If you want me to sugarcoat it a bit more I can say things like "That's not completely correct in all cases" rather than just "it's not C++."

C++ is defined very carefully in a big document (actually a couple of different version, hence C++ 98 is the C+ defined in the document made in 1998, and C++ 03 is the C++ defined in the document made in 2003). Anything that conflicts with the document is not C++. The document makes it clear that main returns an int. Thus, any main that does not return an int is in conflict with the document and thus is not C++. Some compilers will allow this; they are thus compiling something that is not C++.

If a different compiler looks at the same code, it may refuse to compile it on the grounds that it is not C++.

If a different programmer has to use the code, that different programmer may be confused because he expected C++ code and he's been given something that is not C++ code.

Hope that's all nice and clear now.
Last edited on
closed account (z05DSL3A)
evilpickles wrote:
DANGN IT WHY ISNT EDIY WORKING!?

It probably is. There is currently a slight hitch in the forum, you have to do a page refresh after your edit to see the changes.

Edit:
If using namespace System; was auto generated then you have likely selected the wrong project (CLR projects use C++/CLI a different language to C++).
Last edited on
Pages: 12