`
frenchmay
  • 浏览: 234350 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

系统级编程2 Allocation and Reference

阅读更多
#include <stdio.h>

void Initialize (char * a, char * b)
{
        a[0] = 'T'; a[1] = 'h'; a[2] = 'i';
        a[3] = 's'; a[4] = ' '; a[5] = 'i';
        a[6] = 's'; a[7] = ' '; a[8] = 'A';
        a[9] = '\0';
        b = a;
        b[8] = 'B';
}

#define ARRAY_SIZE 10
char a[ARRAY_SIZE];
char b[ARRAY_SIZE];

int main(int argc, char * argv[])
{
        Initialize(a, b);
        printf("%s\n%s\n", a, b);
        return 0;
}
 

When you run this program, only "This is B" is printed. To see why, place a

breakpoint in the line b[8] = 'B' and rerun the program. Take a look at the values 

of a and b. They are the same, as they should be, because we assigned a to b in the

 previous line. What is the same, however, is not the  contents  of the arrays but 

the two arrays have the same address after the assignment b = a. The contents are 

not only equal, they  are  the same. Modifying one will also modify the other. The 

assignment b[8] = 'B' will modify the bits of memory that also contain a[8], and

 the contents of the original b will remain untouched. The two original arrays 

still get printed, but the first one will now contain "This is B" and the second

 will be empty because it was never touched. The address of the second one, which 

was allocated by the compiler when it was declared, was overwritten and discarded

 by the line b = a before its contents were modified.

分享到:
评论

相关推荐

    文件系统APIReference-PrFILE2-en_file.pdf

    综上所述,这份手册对于嵌入式系统开发人员来说是宝贵的技术资料,详细说明了PrFILE2 FAT文件系统提供的API,包括文件操作、磁盘管理以及系统配置等功能,确保开发者能够有效地操作FAT文件系统,进行系统级编程和...

    SCSI Commands Reference Manual-seagate

    手册中可能涉及的技术概念和术语需要用户具备一定的计算机系统结构、外设接口和硬件编程方面的知识储备。在处理实际的硬件接口问题和开发与SCSI相关的软件时,这份手册将是开发人员的重要工具之一。

    SAP屠夫作品汇总

    例2 BCS和ECCS等合并系统简单对比 163 第五节 子分配和细分类别 164 SAP会计科目自动分配配置大全 170 1 Maintain Accounting Configuration(概览) 170 2 Automatic Posting Configuration (MM Module) 171 例1:如何...

    操作系统(内存管理)

    free:该函数获得指向由 malloc 分配的内存片段的指针,并将其释放,以便以后的程序或操作系统使用(实际上,一些 malloc 实现只能将内存归还给程序,而无法将内存归还给操作系统)。 物理内存和虚拟内存 要理解...

    [详细完整版]操作系统术语.ppt

    4. **Application Programming Interface (API)**:API是一组预先定义的函数、类和接口,允许开发者通过编程与操作系统进行交互。 5. **Asynchronous Operation**:异步操作指在执行过程中,不等待某个操作完成就...

    嵌入式+芯片手册相关单词表

    - **二进制 (Binary)**:一种基于2的计数系统。 - **八进制 (Octal)**:一种基于8的计数系统。 - **十进制 (Decimal)**:我们日常生活中最常用的计数系统,基于10。 - **十六进制 (Hexadecimal)**:一种基于16的计数...

    内存管理内存管理内存管理

    /* Okay, we're initialized and ready to go */ has_initialized = 1; } 现在,为了完全地管理内存,我们需要能够追踪要分配和回收哪些内存。在对内存块进行了 free 调用之后,我们需要做的是诸如将它们...

    Design Pattern(设计模式)讲义

    - **池分配(Pooled Allocation)**:通过预先分配固定大小的对象并将它们存储在一个池中来提高性能。 - **智能指针(Smart Pointer)**:一种用于管理指针资源的模式,能够在不再需要指针时自动释放资源。 - **基于...

Global site tag (gtag.js) - Google Analytics