Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function pointer assignment and memset'ing - What's happening here?

Reply
Thread Tools

Function pointer assignment and memset'ing - What's happening here?

 
 
Shao Miller
Guest
Posts: n/a
 
      02-17-2013
On 2/11/2013 15:47, Testing Tester wrote:
>> What problem are you trying to solve?

>
> First thank you for the explanation. I'm on x86-32, and doing
> something that's very specific and so I'm almost sure there's no way
> to stay 'portable'. I'm loading an a.out file on memory, subjected to
> some constraints, and I do need to write the 4 byte-address of (say)
> puts function in a certain memory location. My code does work as
> expected using the following line:
>
> *((int *)(data+curr_reloc->r_address)) = puts;
>
> , but when I tried to replace it with a less ugly one and wasn't
> successful and I was kind of confused on how "*puts", "&puts" and
> "puts" produced the same result when trying to replace this line and
> specially on how I couldn't find an equivalent line using memcpy.
>


I'd suggest using a function pointer:

/* Function type definition, compatible with 'puts' */
typedef int f_puts(const char *);

/* Function pointer to point to 'puts' */
f_puts * puts_ptr = puts;

/* Copy the stored pointer value to the desired slot */
memcpy(data + curr_reloc->r_address, &puts_ptr, sizeof puts_ptr);

/* Or: Assign a pointer to 'puts' to the slot, directly */
*(f_puts **) (data + curr_reloc->r_address) = puts;

'(puts == &puts && puts == *puts)' is always true (given #include
<stdio.h>). This is because some conversions happen:

puts == &puts --> &puts == &puts
puts == *puts --> &puts == puts --> &puts == &puts

You can check that 'sizeof &puts == 4' before either the 'memcpy' or the
assignment.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      02-17-2013
On 2/17/2013 12:37, Shao Miller wrote:
>
> '(puts == &puts && puts == *puts)' is always true (given #include
> <stdio.h>). This is because some conversions happen:
>
> puts == &puts --> &puts == &puts
> puts == *puts --> &puts == puts --> &puts == &puts
>


Or, more accurately for the second one:

puts == *puts --> &puts == *&puts --> &puts == puts --> &puts == &puts

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function pointer and pointer to function prashant.khade1623@gmail.com C Programming 3 04-11-2008 07:30 AM
Hash assignment (what's happening?) Hector Quiroz Ruby 2 10-05-2007 02:25 PM
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments