Complete.Org: Mailing Lists: Archives: linux-help: April 2001:
[linux-help] Re: C HELP(out of list-subject)
Home

[linux-help] Re: C HELP(out of list-subject)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: "Elewitz, Dovid" <Dovid.Elewitz@xxxxxxxxxx>, linux-help@xxxxxxxxx
Subject: [linux-help] Re: C HELP(out of list-subject)
From: Weqaar Ali Janjua <wxjanjua@xxxxxxxxxxx>
Date: Sun, 29 Apr 2001 15:26:50 -0700
Reply-to: linux-help@xxxxxxxxx

Thanks a lot for the help!:)  and to John Reinke 
<jmreinke@xxxxxxxxxxxxxxxxxxx>!

  Sorry 4 replying late as I was sick N could'nt check my email!!:) 
anyways, Well I understand what u stated clearly, the following program 
does help, now what confuses me a lot is when I hold a pointer equal to a 
memory address like:

char *hackPtr;
hackPtr=0xFFF0;

Now according to a howto on a wesbite, when u = a pointer to a memory 
address like the above statement it starts pointing to that address, but 
does the pointer really starts pointing to that address like that???
Now according to TOM HULL <thull@xxxxxxxxxxx>, If it looks like
I'm getting persistent storage when I exit my program and get the same data 
from the same addresses when I run my program again, that's an illusion, 
just luck.!! I GET THAT, thats right, I tried it in linux I get different 
addresses most of the times BUT BUT BUT when I run my following program to 
modify/read a value in a certain location (what I tried to do was I 
executed my first program and after inputting the values into the mem. 
locations and getting the addresses I did'nt exit my prog. instead keep it 
running in a nonsense loop so the data remains in those mem. addresses), 
suppose I get a mem. location of 0x0004 :

#include <stdio.h>
#define HACK_ADDR 0x0004
int main(void)
{
int *hackPtr;
hackPtr=HACK_ADDR;
scanf("%d",hackPtr);    // Yeah gets() is 
dangerous                             //<jmreinke@xxxxxxxxxxxxxxxxxxx>
printf("\nvalue is %d and address is %p\n",hackPtr,hackPtr);
return 0;
}

Now the strange thing(to me:) happens is if I input supposing a 
integer(tried to use int values instead of char!!) "3" the mem. location I 
get is always 0x0003 and if I input "9" the mem. locations I get is 0x0009 
....anyways still stuck on HOW TO MODIFY DATA IN A MEMORY LOCATION!!

I'm still trying, lets try try again!, I'll get it one day!!
Thanks to all ya guyz,
Weqaar

At 03:39 PM 4/26/2001 -0500, you wrote:
>This is really a long and complicated subject. I will try (but not too
>hard). Keep in mind that my terms may not be correct (e.g. the term 'type'
>or 'member' may be wrong).
>
>#include <stdio.h>
>#include <stdlib.h>
>//Structure Definition
>struct weqaar {
>char *fish;
>char *sick;
>};
>main(void){
>struct weqaar a; //initializing variable of type 'struct weqaar'
>//This says that we are creating a struct called 'a' of type weqaar. Struct
>a has 2 elements. Each element hold an address (because it is *fish and not
>fish). (This is a very important point!) The address has to point to a
>variable of type 'char'.
>struct weqaar *ptr;
>//This creates a variable called ptr. ptr is NOT a struct! ptr holds an
>address. The address has to point to a struct of type weqaar.
>//Now,
>ptr=&a;
>//is valid. It assigns the address of the 'beginning' of 'a' to ptr. So, the
>address in ptr points to a. a has 2 addresses (fish and sick) each pointing
>to 'an unnamed' char variable.
>printf("Address in ptr (address of (beginning of) a) %p\n",ptr);
>//This gives the address of 'a'.
>printf("Address in a.fish: %p\n",a.fish);
>printf("Address in a.sick: %p\n",a.sick);
>//These show the contents of a.fish and a.sick. These are NOT real! Nothing
>was put in a.fish or a.sick so they have nothing to point to!
>//Now,
>char char1;
>char1='A';
>//Create char1 and put 'A'  in it.
>a.fish=&char1;
>//a.fish is made to hold an address of a 'char'. We give it the address of
>char1.
>printf("Address in a.fish (address of char1): %p\n\t (Notice that addresses
>are assigned from top to bottom.)\n",a.fish);
>printf("Address in a.sick: %p\n",a.sick);
>printf("Address of char1: %p. (Same as a.fish.)\n",&char1);
>//Now a.fish has a real value; The address of char1. a.sick is still the
>same as before.
>//You could do:
>char *(*ptr_char);
>//Create a pointer (ptr_char) to a pointer (&a.fish see next line)
>//which points to a char value.
>ptr_char=&a.fish;
>printf("ptr_char (a.fish)= %p\n",ptr_char);
>//Notice that ptr_char==ptr. ptr_char points to the address of the first
>member of the
>//struct, ptr points to the beginning (first member) of the struct.
>//If you do:
>ptr_char=&a.sick;
>printf("ptr_char (a.sick)= %p\n",ptr_char);
>//ptr_char is the address of the 2nd member of the structure.
>//Because an address is 4 bytes, ptr_char is 4 more than ptr.
>return 0;
>}
>
>I hope this helps. Basically you have to keep in mind what is going on the
>right side of a variable, an address or value. (Even) If it is an address,
>it has to be of the same type as the value in that address.
>
>Dovid
>
>
>-----Original Message-----
>From: Weqaar Ali Janjua [mailto:wxjanjua@xxxxxxxxxxx]
>Sent: Tuesday, April 24, 2001 5:49 PM
>To: Elewitz, Dovid
>Subject: RE: C HELP(out of list-subject)
>
>
>Hi,
>    Well before program execution therez nothing in b.fish and b.sick, but
>after I run the program, store some values in them and exit it, the values
>are still in those memory locations due to the fact(what I think)
>structures are of static storage class and are defined in the program body
>outside main function(), what makes me think that is when I run the program
>again and first reads the values stored in the variables(before inputting
>them again) I get the same values I entered the last time I executed the
>program, ur statement is right "Static is just while this program is
>running." but I used pointers to char "char *fish; char *sick;" so the
>values remain in the memory locations, just trying to figure out how to
>modify those values using direct memory addressing.
>Thanx,
>Weqaar Ali Janjua
>
>At 01:50 PM 4/24/2001 -0500, you wrote:
> >I am not a programmer, but I have dabbled with C. What may be happening is
> >that you are not putting anything in b.fish and b.sick. They point to
>memory
> >though and there may be data there. By reading before writing, you are
> >reading whatever some other program left over. Being that you just ran the
> >same program, it would make sense that your program left it and you are now
> >reading the garbage sitting in memory from the program's previous lifetime.
> >I don't think static has anything to do with it; Static is just while this
> >program is running.
> >By the way, OS designers know that this is a security issue. To get C2
> >certification for example, you have to wipe memory before allocating it so
> >that nothing is left from some old program.
> >
> >Dovid
> >
> >-----Original Message-----
> >From: Weqaar Ali Janjua [mailto:wxjanjua@xxxxxxxxxxx]
> >Sent: Tuesday, April 24, 2001 4:41 AM
> >To: ipchains-list@xxxxxxxxxxxxxxx
> >Subject: C HELP(out of list-subject)
> >
> >
> >
> > >Hello guyz,
> >
> >      This question is kinda out of the list discussion but any help will
>be
> >greatly appreciated. Can anybody help me out with the following C
> >programming problem:
> >
> >I make a structure with some char type variables:
> >-------------------START OF CODE------------------------------
> >/*POINTER TEST*/
> >#include <stdio.h>
> >
> >//Structure Definition
> >struct weqaar {
> >char *fish;
> >char *sick;
> >};
> >
> >int main(void) {
> >struct weqaar a; //initializing variable of type 'struct weqaar'
> >struct weqaar b; //initializing variable of type 'struct weqaar'
> >struct weqaar *ptr;
> >ptr=&a;
> >
> >printf("\nenter value of a.fish:");
> >gets(a.fish);
> >printf("\nenter value of a.sick:");
> >gets(a.sick);
> >printf("%s\t%s",a.fish,a.sick);
> >printf("\n");
> >//checking the values stored in mem.
> >printf("b.fish is %s,\tb.sick is %s",b.fish,b.sick);
> >//
> >printf("\n");
> >printf("\nenter value of b.fish:");
> >gets(b.fish);
> >printf("\nenter value of b.sick:");
> >gets(b.sick);
> >printf("%s\t%s",b.fish,b.sick);
> >printf("\n");
> >printf("%s\t%s",a.fish,a.sick);
> >printf("\n");
> >printf("PTR values are: %s\t%s\n",ptr->fish,ptr->sick);
> >printf("Mem. location addresses of b.fish and b.sick are %p
> >%p",b.fish,b.sick);
> >return 0;
> >}
> >-------------------END OF CODE------------------------------
> >Now I execute the program and store some data in the variables suppose:
> >I enter 'ash' in 'a.fish' and 'dish' in 'a.sick' AND 'madonna' in 'b.fish'
> >and 'nicole' in 'b.sick' and then terminate the program.
> >When I run the program again  the commented line in my C prog. gives me the
> >output "madonna    nicole", that means the values are still
> >there(structures being of storage type static) and my last line prints out
> >the memory addresses '0C5B' and 'FFF0' of variables 'b.fish' and 'b.sick'
> >and the program terminates exit(1). Then I note these addresses and write
> >another piece of code:
> >-------------------START OF CODE------------------------------
> >#include <stdio.h>
> >
> >int main(void) {
> >char *hackPtr=0xFFF0; //note that line
> >printf("%s",hackPtr);
> >return 0;
> >}
> >-------------------END OF CODE------------------------------
> >now note the line 'char *hackPtr=0xFFF0;' , I have to specify 0x before the
> >address I got from the output of my first program, If I dont specify '0x'
> >the compiler takes it as a string value, my core question is how can I get
> >the accurate/in-the-form memory address as '0xFFF0'?? and how can I make my
> >other program modify the value in that location???can I read the string in
> >that location with my second program???or do I have to decode the value I
> >get(maybe HEX)???? AND how can I make structure of any non-static storage
> >type?????
> >Thanx in advance,
> >Weqaar Ali Janjua
> >BECE
> >Wichita State University
> >

-- This is the linux-help@xxxxxxxxx list.  To unsubscribe,
visit http://tmp2.complete.org/cgi-bin/listargate-aclug.cgi


[Prev in Thread] Current Thread [Next in Thread]