-2

I am new to C; I have more background knowledge with Java. I want to try searching for value in memory using pointer

#include <stdio.h>

void main(){

    int value = 10;
    find(value);
}

void find(int value){ // will change this to return array of long or int depending on which is better

int* intPointer;

    for(int i = 40000; i <  40400  ; i+= 32){ /* not sure if i do it like this or another way,
                                               * the starting value, ending value and condition
                                               * is just for testing purpose */

        intPointer = (int*)i;

        if(*intPointer == value){
           printf("found value"); //will actually be storing it to an array instead
        }
    }
}

The find method gives me a segmentation fault most likely because the address does not store int value. Is there a way I can find out what type data is stored in the memory address.

Is there a better way of achieving a similar task? Please explain and show. The actual program will not have int value = ?? instead only the find method will be used to get a array of addresses which contains this value.

Also what is the best type to store addresses int or long?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dev_anon101
  • 107
  • 2
  • 11
  • You may invoke *undefined behavior*. The result will be dependent on the system and if the address is prohivited to access by the OS, accessing there will lead to have your program killed by the OS. You wrote `void main()`, so you may want to execute this code in freestanding environment such as microcontrollers (but `printf()` may not be supported there). Note that using undeclared function is bad anyway. – MikeCAT Jul 25 '16 at 16:34
  • 1
    You're accessing unallocated memory: it's undefined behavior in C/C++, so you can't check memory in that way. By the way, the size of int is implementation dependent: you should use `sizeof(int)*8` instead of 32 to increment your loop. Why do you want to do that in the first time? Maybe we could propose you an other way – rocambille Jul 25 '16 at 16:42
  • The best type for storing addresses is a pointer (to the appropriate type). `int` and `long` may or may not have compatible representations and adequate size. – Dmitri Jul 25 '16 at 16:49
  • There's so much wrong with your question, I don't even know where to start. That's just not how memory works. You got a seg fault because you accessed memory you don't have permission to access. – Rob K Jul 25 '16 at 16:50
  • @MikeCAT i didn't know that void or returning 0 had a huge different, i am completely new to C and i was just trying a simple application with pointers. – Dev_anon101 Jul 25 '16 at 16:51
  • @wasthishelpful i am open to different implementation me and one of my friend did a similar task to this in c++ but i forgot what he did – Dev_anon101 Jul 25 '16 at 16:53
  • @Dev_anon101 - Which kind of system are you using? – Support Ukraine Jul 25 '16 at 16:54
  • @4386427 i'm using windows 10 64 bit, although this was run on online compiler i will retry on actual console based application. – Dev_anon101 Jul 25 '16 at 16:58
  • On windows you can't do what you are trying. Memory isn't handled that simple on advanced platforms. – Support Ukraine Jul 25 '16 at 17:00
  • On modern PCs (among others), The memory addresses your program sees are virtual addresses, not the actual physical memory addresses; the system maps them to the real addresses, but your program can't (normally) see what those are or access physical memory that isn't mapped into its virtual address space. And the system also keeps track of how your program is allowed to use different areas of memory and will not allow it to access them differently. C certainly doesn't guarantee that you can use pointers to arbitrary parts of memory, and neither does C++. – Dmitri Jul 25 '16 at 17:15
  • Maybe you'll find this interesting http://stackoverflow.com/questions/8403610/how-do-you-read-directly-from-physical-memory – Support Ukraine Jul 25 '16 at 17:28

3 Answers3

2

A couple things to know right off the bat:

  1. Memory is not always guaranteed to be physically contiguous (even if the memory model appears to be as such).
  2. Knowing what a segfault is would be helpful, as well as what segmented memory and memory protection are in general

The find method gives me a segmentation fault most likely because the address does not store int value

From what I can tell you are trying to loop through memory addresses and find a specific value. Technically it's possible, but practically there's no reason to do it except a few rare cases.

Unfortunately (for your purposes) your operating system of choice handles memory rather intelligently; instead of just placing everything it is given in a linear order, it divides (or segments) memory into their own address spaces to keep memory of process A from interfering with memory of process B, and so forth. To keep track of which processes are where in memory, a mapping mechanism is used.

You tried to access memory outside of your program's assigned segment. To be able to do what it seems you want to do, you're going to need to find a way to get the memory map from the OS.

is there a way I can find out what type data is stored in the memory address

No, at least not in the way you want. A couple SO answers address this.

Community
  • 1
  • 1
galois
  • 825
  • 2
  • 11
  • 31
1

There is no way to do what you trying in a generic portable way. The code generates undefined behavior as you are accessing memory not allocated to you.

On simple embedded systems it may be possible. To start with, it will require that you have full insight in the systems memory layout and how the compiler works. For instance, you must know whether int is always 4 byte aligned in memory, start/end address of RAM. If an MMU is present and active, you also need to know how it works.

is there a way I can find out what type data is stored in the memory address.

No

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You are faking a pointer with a temporary var inside the loop. The correct way to search for a value would be to have two arguments: the value to search for, and a collection (array, for example) containing some values to be searched.