简体   繁体   English

我可以在没有 malloc 的情况下释放指针地址吗?

[英]Can I free pointer address without malloc?

Let's suppose this code:让我们假设这段代码:

int i,j=0;
char* block = (char*) (0x9000);
char table[4]= {0x01,0x02,0x03,0x04};
for (i=0; i< 45567; i++) {
    *(block +i)= table[j]; 
    j++; 
    if (j==4)
        j=0;
}

I would like to ask:我想问一下:

  1. Is the memory for block allocated in the stack or in the heap? block的内存是分配在栈中还是堆中?
  2. What problems could happen with this code?这段代码会出现什么问题?
  3. Can I use free(block) at the end of this code?我可以在这段代码的末尾使用free(block)吗?

You didn't actually allocate any memory, neither on the stack nor on the heap.您实际上没有分配任何内存,无论是在堆栈上还是在堆上。 You're just pointing your variable at an address and then pretend that the memory there belongs to you.您只是将变量指向一个地址,然后假装那里的内存属于您。

This is not legal in either C or C++ and will generally not work .这在 C 或 C++ 中都是不合法的,并且通常不起作用 And “not work” can really mean anything here. “不工作”在这里真的可以意味着任何事情。 And since the code is illegal, the question of whether the pointer can be free d is moot.并且由于代码是非法的,指针是否可以free d 的问题没有实际意义。

(In very specific settings, if the compiler and the hardware supports it, this is used to write to specific hardware addresses. But this isn't the case here.) (在非常具体的设置中,如果编译器和硬件支持它,这用于写入特定的硬件地址。但这里不是这种情况。)

does the block memory allocated in the stack or in the heap?块内存分配在堆栈中还是堆中?

Neither.两者都不。 It is not allocated at all: you assigned an arbitrary number to the pointer, so any attempt to read or write from block is undefined behavior.它根本没有分配:您为指针分配了一个任意数字,因此任何从block读取或写入的尝试都是未定义的行为。

Can I use free(block) at the end of this code?我可以在这段代码的末尾使用 free(block) 吗?

That would be undefined behavior as well.那也是未定义的行为。 Pointers that you are allowed to pass to free must come from malloc , calloc , or realloc .允许传递给free指针必须来自malloccallocrealloc

Note: Undefined Behavior is the official name for what happens when your program does something that the standard does not allow, and the compiler does not catch.注意:未定义行为是当您的程序执行标准不允许的操作并且编译器未捕获时所发生的情况的正式名称。 In many instances undefined behavior leads to a crash, but, unfortunately, in some cases your program appears to work.在许多情况下,未定义的行为会导致崩溃,但不幸的是,在某些情况下,您的程序似乎可以工作。

you dont need to free anything you didnt ask to get space for , everything you declare without realloc malloc or calloc dont need free , and they store on static storage or the stack .你不需要释放任何你没有要求获得空间的东西,你在没有 realloc malloc 或 calloc 的情况下声明的一切都不需要 free,它们存储在静态存储或堆栈中。

there are also strdup and some other function you need to free but simple declare dont require free还有strdup和其他一些您需要免费的功能,但简单的声明不需要free

You should never call free on a pointer value that wasn't returned from one of malloc , calloc , or realloc .永远不要对不是从malloccallocrealloc之一返回的指针值调用free

Do you know if 0x9000 is a valid, writable address on your platform?知道0x9000在您的平台上是否是有效的可写地址吗?

The array table is likely on the stack.数组table很可能在堆栈上。 The pointer block , it is to be hoped, is in some otherwise unoccupied memory, maybe it was allocated, and 0x9000 is some special address, there isn't enough code to determine context.希望指针block在一些其他未被占用的内存中,也许它已被分配,并且 0x9000 是一些特殊地址,没有足够的代码来确定上下文。 Depending on the architecture, writing blindly to an arbitrary address may or may not be allowed.根据体系结构,可能允许也可能不允许盲目写入任意地址。

So to answer your questions:所以回答你的问题:

  • you should not free without malloc/calloc/realloc, results are undefined;没有 malloc/calloc/realloc,你不应该释放,结果是不确定的; exceptions do exist, for example if a space is allocated by the system prior to your use, but then typically if the system allocates the memory, it will free it itself例外确实存在,例如,如果系统在您使用之前分配了空间,但通常如果系统分配内存,它会自行释放

  • all kinds of problems can happen with this code, but depends on the architecture;这段代码可能会出现各种问题,但这取决于架构; I've written on platforms where this was perfectly legal, others will throw all kinds of exceptions;我在完全合法的平台上写过文章,其他人会抛出各种异常; it might be that it fills 45567 bytes starting at 0x9000 with repeated sequences of 1 to 4, or it might crash your machine;可能是它用 1 到 4 的重复序列填充了从 0x9000 开始的 45567 个字节,或者它可能会使您的机器崩溃; C will happily let you shoot yourself in the foot C会很乐意让你用脚射自己

你所做的很危险。你不应该选择一个随机地址并将它分配给一个指针来将数据复制到内存中。这就像在地球上的一个随机地方倾倒核废料。相反,你应该使用 malloc 或 new 来分配内存安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM