2
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

extern char **environ;

int global_x = 10;                  // initialised global variable
int global_y;                       // un-initialised global variable
char global_array1[] = "Hello, world!";     // initialised global array and a string literal
char global_array2[10];             // un-initialised global array
char *global_pointer1 = "bye!";         // global pointer to a string literal 
char *global_pointer2;              // un-initialised global pointer 
float global_float = 100.1;         // initialised global variable
double global_double;               // un-initialised global variable

#define ONEGB  1073741824
#define ONEMB  1048576
#define ONEKB  1024

char *addr(unsigned long a)
{
    unsigned long r; // remainder

    r = (unsigned long) a;
    int gb = (int) ( r / ONEGB );

    r -=  gb * ONEGB;
    int mb = (int) ( r  / ONEMB );

    r -=  mb * ONEMB;
    int kb = (int) ( r  / ONEKB );

    r -=  kb * ONEKB;
    int b  = (int) ( r );

    char *p = malloc(64);

    sprintf(p, "%4dGB, %4dMB, %4dKB, %4d", gb, mb, kb, b);
    return p;
}

int f2(int x)
{
    char * f2_p;
    int f2_x = 21;

    f2_p = malloc(1000);         // dynamically allocated memory

    // print out the address of x
    // print out the addresses of f2_p, and f2_x
    // print out the starting address of the dynamically allocated memory
    .....

    L: f2_x = 10;
    return f2_x;
}

void f1(int x1, int x2, float x3, char x4, double x5, int x6)
{
    int f1_x = 10;
    int f1_y;
    char *f1_p1 = "This is inside f1";   // pointer to another string literal 
    char *f1_p2;

    f1_p2 = malloc(100);         // dynamically allocated memory

    // print out the addresses of x1, x2, x3, x4, x5, x6
    // print out the addresses of f1_x, f1_y, f1_p1, f1_p2
    // print out the address of the string literal "This is inside f1"
    .....

    f1_y = f2(10);
    return;
}

int main(int argc, char *argv[])
{

    // print out the addresses of argc, argv
    // print out the starting address and end address of the command line arguments of this process
    // print out the starting address and end address of the environment of this process
    // print out the starting addresses of function main, f1, and f2
    // print out the addresses of global_x, global_y, global_array1, global_array2, global_pointer1,
    //           global_pointer2, global_float, global_double
    // print out the addresses of string literals 10, "Hello, world!", "bye", 100.1

    ..... 

    // call function f1 with suitable arguments such as 12, -5, 33.7, 'A', 1.896e-10, 100 
    f1( .... );

    exit(0);
}

我试图在google上搜索,但找不到有用的东西,在这种情况下我只想弄清楚如何打印出动态分配的内存的起始地址;打印出本进程命令行参数的起始地址和结束地址;打印出本进程环境的起始地址和结束地址;打印函数main、f1、f2的起始地址。任何人都可以帮助我吗?..谢谢!

4

5 回答 5

0

f1 和 f2(没有 (parameter) 块)是函数的起始地址,以便更好地说明。f2(x); 调用函数 int f2(int x) 并传递参数 x。不带括号的“f2”是函数int f2(int x)的地址

于 2012-09-26T11:54:40.080 回答
0

main

打印出地址argc, argv ——printf ("%d, %d", &argc, argv);

打印出这个进程的命令行参数的起始地址和结束地址——printf ("%d", (void *)argv);

打印出这个进程的环境的起始地址和结束地址——printf ("%d", (void *)environ);

打印出函数的起始地址main, f1, and f2-printf ("%d %d %d", &main, &f1, &f2);

打印出地址global_x, global_y, global_array1, global_array2, global_pointer1, global_pointer2, global_float, global_double- 只需&在要打印其地址的每个变量前面使用运算符。

打印字符串文字的地址10, "Hello, world!", "bye", 100.1- 不允许打印字符串文字的地址。

f1

打印出 x1, x2, x3, x4, x5, x6 的地址 -printf ("%d %d %d %d %d %d", &x1, &x2, &x3, &x4, &x5);

打印出 f1_x, f1_y, f1_p1, f1_p2 的地址 -printf ("%d %d %d %d", &f1_x, &f1_y, f1_p1, f2_p2);

打印出字符串文字的地址"This is inside f1"- 不允许获取字符串文字的地址

f2

打印出 x 的地址 -printf ("%d", &x);

打印出 f2_p 和 f2_x 的地址 - printf("%d", f2_p, &f2_x)

打印出动态分配内存的起始地址——printf ("%d", f2_p);

于 2012-09-26T11:54:59.160 回答
0

根据我的讲师的指导声明一个字符串文字 -

char *p = "this is a string literal";

printf("address of variable p starts at %p\n",  &p));
printf("address of the string literal starts at %p\n", p);
于 2017-02-19T15:03:21.193 回答
-1

为此,您需要 & 运算符。它需要一个变量的地址

可以在此处找到有关指针运算符的更多信息

到常规变量的起始地址:

printf("%p", &variable);

这些变量并没有真正的结束地址,但我认为你想要的是这样的:

printf("%p", (&variable + 1));

打印下一个可用地址

于 2012-09-26T11:54:22.517 回答
-1

如果要查看变量的地址,则需要使用&运算符或直接指针(在动态分配内存的情况下)。

int main(int argc, char *argv[]){  
   int * arr;
   int i = 0;
   arr = malloc(100 * sizeof(int));
   printf("dynamic array starts at: %#x and ends at: %#x\n",arr,arr+100);
   printf("static i is at: %#x\n",&i);
}

输出:

mike@linux-4puc:~> ./a.out 
dynamic array starts at: 0x804b008 and ends at: 0x804b198
static i is at: 0xbfc1f6d8
于 2012-09-26T12:17:56.323 回答