linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* memory address represented as a string
@ 2009-07-26  7:39 Saurabh Sehgal
  2009-07-26  7:59 ` Aneesh Bhasin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Saurabh Sehgal @ 2009-07-26  7:39 UTC (permalink / raw)
  To: linux-c-programming

Hi all,

I had a quick question:

Let's say I design a function with the signature:

void * foo( char * addr ) ; ,

where addr is a string that represents a valid memory address  ...
so the way someone can call this function is ...

char * addr = "0xae456778" // assume this is a valid memory address on
the machine
foo( addr ) ;

Is it possible to take this address in string form, and assign it to
an actual pointer of void * type ?
I want the function "foo" to return a pointer pointing to the memory
location as indicated
by the string passed in.

Thank you !

Saurabh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: memory address represented as a string
  2009-07-26  7:39 memory address represented as a string Saurabh Sehgal
@ 2009-07-26  7:59 ` Aneesh Bhasin
  2009-07-26  8:06   ` Manish Katiyar
       [not found] ` <b21328ed0907260114w547eb3fax61927111cdcf9d3c@mail.gmail.com>
  2009-07-26 16:23 ` Glynn Clements
  2 siblings, 1 reply; 6+ messages in thread
From: Aneesh Bhasin @ 2009-07-26  7:59 UTC (permalink / raw)
  To: Saurabh Sehgal; +Cc: linux-c-programming

Hi Saurabh,

On Sun, Jul 26, 2009 at 1:09 PM, Saurabh Sehgal <saurabh.r.s@gmail.com> wrote:
>
> Hi all,
>
> I had a quick question:
>
> Let's say I design a function with the signature:
>
> void * foo( char * addr ) ; ,
>
> where addr is a string that represents a valid memory address  ...
> so the way someone can call this function is ...
>
> char * addr = "0xae456778" // assume this is a valid memory address on
> the machine

This means that addr points to a memory location where the string
stored is "0xae456778"


>
> foo( addr ) ;
>
> Is it possible to take this address in string form, and assign it to
> an actual pointer of void * type ?

Yes, you can parse each individual character of the hex-format string
and convert it to its integral equivalent (you can find many samples
of this on internet...) and assing this value to a void * and then
return it back..

Hope that helps..
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: memory address represented as a string
  2009-07-26  7:59 ` Aneesh Bhasin
@ 2009-07-26  8:06   ` Manish Katiyar
  0 siblings, 0 replies; 6+ messages in thread
From: Manish Katiyar @ 2009-07-26  8:06 UTC (permalink / raw)
  To: Aneesh Bhasin; +Cc: Saurabh Sehgal, linux-c-programming

On Sun, Jul 26, 2009 at 1:29 PM, Aneesh Bhasin<contact.aneesh@gmail.com> wrote:
> Hi Saurabh,
>
> On Sun, Jul 26, 2009 at 1:09 PM, Saurabh Sehgal <saurabh.r.s@gmail.com> wrote:
>>
>> Hi all,
>>
>> I had a quick question:
>>
>> Let's say I design a function with the signature:
>>
>> void * foo( char * addr ) ; ,
>>
>> where addr is a string that represents a valid memory address  ...
>> so the way someone can call this function is ...
>>
>> char * addr = "0xae456778" // assume this is a valid memory address on
>> the machine
>
> This means that addr points to a memory location where the string
> stored is "0xae456778"
>
>
>>
>> foo( addr ) ;
>>
>> Is it possible to take this address in string form, and assign it to
>> an actual pointer of void * type ?

/tmp> cat test.c
#include<stdio.h>

int *addr(char *address)
{
  int ptr;
  ptr=strtoul(address, 0 , 16);
  if (ptr==-1)
    {
      perror("strtoul failed:");
    }
  return (int *)ptr;
}

int *foo()
{
  char c[10];
  int *ptr=malloc(sizeof(int));
  sprintf(c,"%p",ptr);
  *ptr=100;
  return addr(c);

}

int main()
{
  int *p=foo();
  printf("%d\n",*p);
  return 0;
}
/tmp> gcc test.c
test.c: In function ‘foo’:
test.c:17: warning: incompatible implicit declaration of built-in
function ‘malloc’
/tmp> ./a.out
100

Thanks -
Manish


>
> Yes, you can parse each individual character of the hex-format string
> and convert it to its integral equivalent (you can find many samples
> of this on internet...) and assing this value to a void * and then
> return it back..
>
> Hope that helps..
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>



-- 
Thanks -
Manish
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: memory address represented as a string
       [not found] ` <b21328ed0907260114w547eb3fax61927111cdcf9d3c@mail.gmail.com>
@ 2009-07-26  8:26   ` Saurabh Sehgal
  2009-07-28 10:09     ` Rahul K Patel
  0 siblings, 1 reply; 6+ messages in thread
From: Saurabh Sehgal @ 2009-07-26  8:26 UTC (permalink / raw)
  To: Eric Polino; +Cc: linux-c-programming

Hi all,

Thanks for the reply !

I was able to write this function successfully.

This would lead me to my next question.

Is there anyway to test if a memory address is valid or not without causing a
segmentation fault and catching this maybe in a signal handler ?

Is there a safe way in the same function " void * foo (char * addr)"
to check that
if the address contained in the string represented by "char * addr"  is valid
before returning it.

Thank you !

Saurabh

On Sun, Jul 26, 2009 at 4:14 AM, Eric Polino<aluink@gmail.com> wrote:
> sure, you can just write a function that declares a void * and uses it as a
> regular numeric type such as int or whatnot and parses the string
> representation as a numeric value into that variable.  You can do stuff the
> same as you would with any other integer type, "x += 10; x -= 18;".  So the
> function would look something like
>
> void *foo(const char *addr){
>   void *parsed_value = 0;
>   ....
>   /* Parse addr into parsed_value */
>   ....
>   /* parsed_value == value_described_by(addr) */
>
>   return parsed_value;
> }
>
> Parsing addr into parsed_value is left as an exercise to the reader ;)
>
> "None are more hopelessly enslaved than those who falsely believe they are
> free."
>                                      --Goethe
>
> "Freedom is living without government coercion."
>                   --Ron Paul (www.ronpaul2008.com)
>
>
> On Sun, Jul 26, 2009 at 03:39, Saurabh Sehgal <saurabh.r.s@gmail.com> wrote:
>>
>> Hi all,
>>
>> I had a quick question:
>>
>> Let's say I design a function with the signature:
>>
>> void * foo( char * addr ) ; ,
>>
>> where addr is a string that represents a valid memory address  ...
>> so the way someone can call this function is ...
>>
>> char * addr = "0xae456778" // assume this is a valid memory address on
>> the machine
>> foo( addr ) ;
>>
>> Is it possible to take this address in string form, and assign it to
>> an actual pointer of void * type ?
>> I want the function "foo" to return a pointer pointing to the memory
>> location as indicated
>> by the string passed in.
>>
>> Thank you !
>>
>> Saurabh
>> --
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>



-- 
Saurabh Sehgal
E-mail:     saurabh.r.s@gmail.com
Phone:     647-831-5621
LinkedIn: http://www.linkedin.com/pub/1/7a3/436
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: memory address represented as a string
  2009-07-26  7:39 memory address represented as a string Saurabh Sehgal
  2009-07-26  7:59 ` Aneesh Bhasin
       [not found] ` <b21328ed0907260114w547eb3fax61927111cdcf9d3c@mail.gmail.com>
@ 2009-07-26 16:23 ` Glynn Clements
  2 siblings, 0 replies; 6+ messages in thread
From: Glynn Clements @ 2009-07-26 16:23 UTC (permalink / raw)
  To: Saurabh Sehgal; +Cc: linux-c-programming


Saurabh Sehgal wrote:

> I had a quick question:
> 
> Let's say I design a function with the signature:
> 
> void * foo( char * addr ) ; ,
> 
> where addr is a string that represents a valid memory address  ...
> so the way someone can call this function is ...
> 
> char * addr = "0xae456778" // assume this is a valid memory address on
> the machine
> foo( addr ) ;
> 
> Is it possible to take this address in string form, and assign it to
> an actual pointer of void * type ?
> I want the function "foo" to return a pointer pointing to the memory
> location as indicated
> by the string passed in.

Yes:

	const char *addr = "0xae456778";
	void *ptr;

	sscanf(addr, "%p", &ptr);

The string needs to be in the format used by printf("%p"), which is
platform-specific.

Alternatively:

	ptr = (void *) strtoull(addr, NULL, 16);

However, strtoull() isn't in C89, although it's in C99 and POSIX.

Using strtoul() will work if sizeof(long) >= sizeof(void *). AFAIK,
this is true on all versions of Linux, but may not be true on some
other 64-bit platforms. On particular, it isn't true on Win64, where
"long" is only 32 bits.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: memory address represented as a string
  2009-07-26  8:26   ` Saurabh Sehgal
@ 2009-07-28 10:09     ` Rahul K Patel
  0 siblings, 0 replies; 6+ messages in thread
From: Rahul K Patel @ 2009-07-28 10:09 UTC (permalink / raw)
  To: Saurabh Sehgal; +Cc: Eric Polino, linux-c-programming

Hi,

Saurabh Sehgal wrote:
> Hi all,
>
> Thanks for the reply !
>
> I was able to write this function successfully.
>
> This would lead me to my next question.
>
> Is there anyway to test if a memory address is valid or not without causing a
> segmentation fault and catching this maybe in a signal handler ?
>
> Is there a safe way in the same function " void * foo (char * addr)"
> to check that
> if the address contained in the string represented by "char * addr"  is valid
> before returning it.
>   

static inline int is_invalid_addr( const void *p )
{
       int dev_null_fd = open("/dev/null", O_RDWR);
       if(dev_null_fd < 0)
          return -1;
   
        /* HACK: on some systems we can not write to check for pointer 
validity */
        size_t ret = fcntl( dev_null_fd, F_GETLK, p );

        ret = ( errno == EFAULT );
        errno = 0;
        return ret;
}

You can use this function to check whether given address is accessible.

-- Rahul Patel.
> Thank you !
>
> Saurabh
>
> On Sun, Jul 26, 2009 at 4:14 AM, Eric Polino<aluink@gmail.com> wrote:
>   
>> sure, you can just write a function that declares a void * and uses it as a
>> regular numeric type such as int or whatnot and parses the string
>> representation as a numeric value into that variable.  You can do stuff the
>> same as you would with any other integer type, "x += 10; x -= 18;".  So the
>> function would look something like
>>
>> void *foo(const char *addr){
>>   void *parsed_value = 0;
>>   ....
>>   /* Parse addr into parsed_value */
>>   ....
>>   /* parsed_value == value_described_by(addr) */
>>
>>   return parsed_value;
>> }
>>
>> Parsing addr into parsed_value is left as an exercise to the reader ;)
>>
>> "None are more hopelessly enslaved than those who falsely believe they are
>> free."
>>                                      --Goethe
>>
>> "Freedom is living without government coercion."
>>                   --Ron Paul (www.ronpaul2008.com)
>>
>>
>> On Sun, Jul 26, 2009 at 03:39, Saurabh Sehgal <saurabh.r.s@gmail.com> wrote:
>>     
>>> Hi all,
>>>
>>> I had a quick question:
>>>
>>> Let's say I design a function with the signature:
>>>
>>> void * foo( char * addr ) ; ,
>>>
>>> where addr is a string that represents a valid memory address  ...
>>> so the way someone can call this function is ...
>>>
>>> char * addr = "0xae456778" // assume this is a valid memory address on
>>> the machine
>>> foo( addr ) ;
>>>
>>> Is it possible to take this address in string form, and assign it to
>>> an actual pointer of void * type ?
>>> I want the function "foo" to return a pointer pointing to the memory
>>> location as indicated
>>> by the string passed in.
>>>
>>> Thank you !
>>>
>>> Saurabh
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe
>>> linux-c-programming" in
>>> the body of a message to majordomo@vger.kernel.org
>>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>>       
>>     
>
>
>
>   
-- 
_____________________________________________________________________
Disclaimer: This e-mail message and all attachments transmitted with it
are intended solely for the use of the addressee and may contain legally
privileged and confidential information. If the reader of this message
is not the intended recipient, or an employee or agent responsible for
delivering this message to the intended recipient, you are hereby
notified that any dissemination, distribution, copying, or other use of
this message or its attachments is strictly prohibited. If you have
received this message in error, please notify the sender immediately by
replying to this message and please delete it from your computer. Any
views expressed in this message are those of the individual sender
unless otherwise stated.Company has taken enough precautions to prevent
the spread of viruses. However the company accepts no liability for any
damage caused by any virus transmitted by this email.
_____________________________________________________________________
 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-07-28 10:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-26  7:39 memory address represented as a string Saurabh Sehgal
2009-07-26  7:59 ` Aneesh Bhasin
2009-07-26  8:06   ` Manish Katiyar
     [not found] ` <b21328ed0907260114w547eb3fax61927111cdcf9d3c@mail.gmail.com>
2009-07-26  8:26   ` Saurabh Sehgal
2009-07-28 10:09     ` Rahul K Patel
2009-07-26 16:23 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).