GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2005, 10:25
racermike1967 racermike1967 is offline
New Member
 
Join Date: Nov 2005
Posts: 5
racermike1967 is on a distinguished road

How do you find the return address in an array of integers?


How do you find the return address in an array of integers?
  #2  
Old 07-Nov-2005, 10:51
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: How do you find the return address in an array of integers?


Please give a full explanation of your problem. Start with the Guidelines you missed reading.
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
  #3  
Old 07-Nov-2005, 11:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How do you find the return address in an array of integers?


Quote:
Originally Posted by racermike1967
How do you find the return address in an array of integers?

What do you mean by "return address"? Do you mean the address of the array?

The address of an array of anything is the address of the first element. The programmer doesn't need to know the actual value of the address for normal programs, but sometimes it's helpful in debugging. Anyhow, if you use the name of the array without the [brackets], the compiler treats it as if you had used the address operator on the first element of the array. That is, it is treated as a const pointer to int in this case.

You can print out the hexadecimal value of the address with printf("%p"), as you can see from the following:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  void pr(int *, int);
  int iarray[10] = {0}; /* inialize all to zero */

  printf("iarray     = %p\n", iarray);
  printf("&iarray[0] = %p\n", &iarray[0]);
  printf("&iarray    = %p\n\n", &iarray);

  iarray[7] = 42;
  printf("in main():  iarray[%d] = %d\n\n", 7, iarray[7]);

  pr(iarray, 7);

  return 0;
}


void pr(int *ia, int n)
{
  printf("in function pr: ia    = %p\n", ia);
  printf("                ia[%d] = %d\n", n, ia[n]);
}

Here is a run. Note that the actual addresses shown may be quite different, depending on your compiler and other things, but they will agree for any given compiler for this program on your system.

Code:
iarray = 0x22eec0 &iarray[0] = 0x22eec0 &iarray = 0x22eec0 in main(): iarray[7] = 42 in function pr: ia = 0x22eec0 ia[7] = 42

So, knowing the address of the array is not really useful (except for debugging).

By the way, you do know that the addresses printed out by this program aren't actually physical addresses of real memory, right? (Except possibly for some embedded systems that don't have or don't use memory mapping for their processors.)

For our PCs running Windows XP or Linux, the addresses are "virtual addresses" that our program owns while it is running, and they are mapped to actual physical addresses by the operating system each time the program is loaded.

That is, you might see the same numbers in the printout of this program each time, but they don't necessarily resolve to the same physical address each time.

Regards,

Dave
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
Problem with one variable bretter C++ Forum 1 16-May-2005 07:20
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
C++ file I/O CronoX C++ Forum 36 09-Mar-2004 18:28

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:19.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.