c - Iterate through memory using pointers -
i new c; have more background knowledge java. want try searching value in memory using pointer
#include <stdio.h> void main(){ int value = 10; find(value); } void find(int value){ // change return array of long or int depending on better int* intpointer; for(int = 40000; < 40400 ; i+= 32){ /* not sure if or way, * starting value, ending value , condition * testing purpose */ intpointer = (int*)i; if(*intpointer == value){ printf("found value"); //will storing array instead } } }
the find method gives me segmentation fault because address not store int value. there way can find out type data stored in memory address.
is there better way of achieving similar task? please explain , show. actual program not have int value = ??
instead find method used array of addresses contains value.
also best type store addresses int
or long
?
a couple things know right off bat:
- memory not guaranteed physically contiguous (even if memory model appears such).
- knowing segfault is helpful, segmented memory , memory protection in general
the find method gives me segmentation fault because address not store int value
from can tell trying loop through memory addresses , find specific value. technically it's possible, practically there's no reason except few rare cases.
unfortunately (for purposes) operating system of choice handles memory rather intelligently; instead of placing given in linear order, divides (or segments) memory own address spaces keep memory of process interfering memory of process b, , forth. keep track of processes in memory, mapping mechanism used.
you tried access memory outside of program's assigned segment. able seems want do, you're going need find way memory map os.
is there way can find out type data stored in memory address
no, @ least not in way want. a couple answers address this.
Comments
Post a Comment