C语言中如何显示某些变量的起始地址?

2024-02-13

#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);
}

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


In 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- 不允许打印字符串文字的地址。

In 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"- 不允许获取字符串文字的地址

In f2:

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

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言中如何显示某些变量的起始地址? 的相关文章

  • 将复选框添加到 UniformGrid

    我正在尝试将复选框动态添加到 wpf 中的统一网格中 但看起来网格没有为它们分配足够的空间 所以它们都有点互相重叠 这就是我将它们添加到后面的代码中的方法 foreach string folder in subfolders PathCh
  • 无法使用已与其底层 RCW 分离的 COM 对象。在 oledb 中

    我收到此错误 但我不知道我做错了什么 下面的代码在backrgroundworker中 将异常详细信息复制到剪贴板 System Runtime InteropServices InvalidComObjectException 未处理 通
  • C# 和 Javascript SHA256 哈希的代码示例

    我有一个在服务器端运行的 C 算法 它对 Base64 编码的字符串进行哈希处理 byte salt Convert FromBase64String serverSalt Step 1 SHA256Managed sha256 new S
  • 当我使用“control-c”关闭发送对等方的套接字时,为什么接收对等方的套接字不断接收“”

    我是套接字编程的新手 我知道使用 control c 关闭套接字是一个坏习惯 但是为什么在我使用 control c 关闭发送进程后 接收方上的套接字不断接收 在 control c 退出进程后 发送方的套接字不应该关闭吗 谢谢 我知道使用
  • 从父类调用子类方法

    a doStuff 方法是否可以在不编辑 A 类的情况下打印 B did stuff 如果是这样 我该怎么做 class Program static void Main string args A a new A B b new B a
  • linux perf:如何解释和查找热点

    我尝试了linux perf https perf wiki kernel org index php Main Page今天很实用 但在解释其结果时遇到了困难 我习惯了 valgrind 的 callgrind 这当然是与基于采样的 pe
  • 为什么#pragma optimize("", off)

    我正在审查一个 C MFC 项目 在某些文件的开头有这样一行 pragma optimize off 我知道这会关闭所有以下功能的优化 但这样做的动机通常是什么 我专门使用它来在一组特定代码中获得更好的调试信息 并在优化的情况下编译应用程序
  • Web API - 访问 DbContext 类中的 HttpContext

    在我的 C Web API 应用程序中 我添加了CreatedDate and CreatedBy所有表中的列 现在 每当在任何表中添加新记录时 我想填充这些列 为此目的我已经覆盖SaveChanges and SaveChangesAsy
  • 使用 System.Text.Json 即时格式化 JSON 流

    我有一个未缩进的 Json 字符串 例如 hash 123 id 456 我想缩进字符串并将其序列化为 JSON 文件 天真地 我可以使用缩进字符串Newtonsoft如下 using Newtonsoft Json Linq JToken
  • 在 ASP.NET Core 3.1 中使用包含“System.Web.HttpContext”的旧项目

    我们有一些用 Net Framework编写的遗留项目 应该由由ASP NET Core3 1编写的API项目使用 问题是这些遗留项目正在使用 System Web HttpContext 您知道它不再存在于 net core 中 现在我们
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • 从路径中获取文件夹名称

    我有一些路c server folderName1 another name something another folder 我如何从那里提取最后一个文件夹名称 我尝试了几件事 但没有成功 我只是不想寻找最后的 然后就去休息了 Thank
  • 将自定义元数据添加到 jpeg 文件

    我正在开发一个图像处理项目 C 我需要在处理完成后将自定义元数据写入 jpeg 文件 我怎样才能做到这一点 有没有可用的图书馆可以做到这一点 如果您正在谈论 EXIF 元数据 您可能需要查看exiv2 http www exiv2 org
  • Discord.net 无法在 Linux 上运行

    我正在尝试让在 Linux VPS 上运行的 Discord net 中编码的不和谐机器人 我通过单声道运行 但我不断收到此错误 Unhandled Exception System Exception Connection lost at
  • C++ 复制初始化和直接初始化,奇怪的情况

    在继续阅读本文之前 请阅读在 C 中 复制初始化和直接初始化之间有区别吗 https stackoverflow com questions 1051379 is there a difference in c between copy i
  • 如何让Gtk+窗口背景透明?

    我想让 Gtk 窗口的背景透明 以便只有窗口中的小部件可见 我找到了一些教程 http mikehearn wordpress com 2006 03 26 gtk windows with alpha channels https web
  • 将文本叠加在图像背景上并转换为 PDF

    使用 NET 我想以编程方式创建一个 PDF 它仅包含一个背景图像 其上有两个具有不同字体和位置的标签 我已阅读过有关现有 PDF 库的信息 但不知道 如果适用 哪一个对于如此简单的任务来说最简单 有人愿意指导我吗 P D 我不想使用生成的
  • mysql-connector-c++ - “get_driver_instance”不是“sql::mysql”的成员

    我是 C 的初学者 我认为学习的唯一方法就是接触一些代码 我正在尝试构建一个连接到 mysql 数据库的程序 我在 Linux 上使用 g 没有想法 我运行 make 这是我的错误 hello cpp 38 error get driver
  • 如何在 C++ BOOST 中像图形一样加载 TIFF 图像

    我想要加载一个 tiff 图像 带有带有浮点值的像素的 GEOTIFF 例如 boost C 中的图形 我是 C 的新手 我的目标是使用从源 A 到目标 B 的双向 Dijkstra 来获得更高的性能 Boost GIL load tiif
  • 使用按位运算符相乘

    我想知道如何使用按位运算符将一系列二进制位相乘 但是 我有兴趣这样做来查找二进制值的十进制小数值 这是我正在尝试做的一个例子 假设 1010010 我想使用每个单独的位 以便将其计算为 1 2 1 0 2 2 1 2 3 0 2 4 虽然我

随机推荐