简体   繁体   English

我需要为初始化的指针分配内存吗?

[英]do I need to allocate memory to an initialized pointer?

int a = 10;    
int *p = &a; 

*p = 20;  /* Is this a valid statement? */

I understand that if I do int *p; 我了解如果我做int *p; and then if I do *p = 10 , it is invalid because I have not assigned any memory to p . 然后如果我做*p = 10 ,那是无效的,因为我没有为p分配任何内存。 However, I was wondering if initializing a pointer to some address allocates memory to that pointer or not? 但是,我想知道是否初始化指向某个地址的指针是否为该指针分配了内存?

I was wondering if initializing a pointer to some address allocates memory to it or not? 我想知道是否初始化指向某个地址的指针是否为其分配了内存?

It does NOT allocate any memory. 它不分配任何内存。 Because p a points to an already allocated memory 因为p a 指向已经分配的内存

int a = 10;    // a is statically allocated and value 10 is assigned.
int *p = &a;  // p is pointed to address of a. 
*p = 20;  // at this point p points to that statically allocated memory
int a = 10;    
int *p = &a; 

*p = 20;  /* Is this a valid statement? */

The answer is YES in this particular case. 在这种特殊情况下,答案是YESYES

But when you do like this : 但是当您这样做时:

int *ptr;  //declaration of pointer variable
*ptr = 20;  // It means you are assigning value 20 to the variable where `ptr` points to.

But actually ptr is not pointing to anywhere, basically it means it has indeterminate value . 但是实际上ptr并没有指向任何地方,基本上意味着它具有indeterminate value

so doing *ptr = 20 will put the value 20 to the memory address pointed to by ptr . 因此,执行*ptr = 20会将值20放入ptr指向的内存地址。 So it's called Undefined bahavior 所以它被称为Undefined bahavior

In you case it's valid because &a is valid memory location and p started pointing to that variable when we do p= &a . 在您的情况下,这是有效的,因为&a是有效的内存位置,并且当我们执行p= &ap开始指向该变量。

so *p = 20 means actually changing or assigning the value of a using the pointer p . 因此*p = 20表示实际上a使用指针p更改或分配a的值。

int a = 10;    
int *p = &a; 

I was wondering if initializing a pointer to some address allocates memory to that pointer or not? 我想知道是否初始化指向某个地址的指针是否将内存分配给该指针?

No. The a and p variables need to be allocated on the stack, or if they're outside a function they'd be allocated memory as global variables. 号的ap变量需要栈上分配,或者如果他们的功能之外,他们会被分配的内存为全局变量。 The compiler does this for you automatically, even if you just have... 编译器会自动为您执行此操作,即使您只有...

int a;    
int* p; 

...without the = <expression> to specify their initial value. ...无需= <expression>即可指定其初始值。 What happens is basically that at some address in memory, a number of bytes (likely 4 or 8) will be reserved to store the value you're identifying as a , and at another address further memory (likely 4 or 8 bytes) will be reserved to store the value you call p . 会发生什么情况基本上是在存储器中的某些地址,字节数(可能是4或8)将被保留以存储你识别作为值的a ,并且在另一地址进一步存储器(可能的4或8个字节)将是保留以存储您调用的值p

The initialisation of p with &a simply copies the numeric address of a into the memory for p ... it does not cause the allocation of either[1], nor move their memory. 的初始化p&a简单的拷贝的数字地址a成记忆p ...它不会引起任何[1],也将他们的内存分配。

It may help to visual it like this, using some made-up addresses... 使用一些虚构的地址可能会有助于这样可视化...

MEMORY-ADDRESS            CONTENTS           NAME
1000                      10                 a
2000                      1000               p

Here, p "points" to a because the contents of p 's memory hold a 's address. 在这里, p “指向” a因为p的内存内容包含a地址。 But the addresses of a and p are chosen by the compiler irrespective of any initialisation or other changes to their value. 但是编译器会选择ap的地址,而不考虑其值的任何初始化或其他更改。

I think what's confusing you here is that we do often allocate memory when using pointers... something like: 我认为让您感到困惑的是,我们经常在使用指针时分配内存……

p = new int;

What this does is find some memory for an int dynamically at runtime, loading the address of that memory into p so we can start referring to it as p and using it to store int values. 这样做是在运行时动态地为int找到一些内存,将该内存的地址加载到p以便我们可以开始将其称为p并使用它来存储int值。 When we're finished with it we can delete p to return the memory to the system, such that it may be recycled and used when another new is done. 完成后,我们可以delete p以将内存返回给系统,以便在执行另一个new操作时可以将其回收和使用。 This type of allocation needs to be explicitly performed in your code (or in the code of some library function you call). 这种分配类型需要在您的代码(或您调用的某些库函数的代码)中明确执行。

[1] - an optimiser may choose not to assign actual memory for a and/or p - using a CPU register instead, but that won't affect the functional behaviour of your program (it may affect the preformance). [1]-优化器可以选择不使用CPU寄存器为a和/或p分配实际的内存,但这不会影响程序的功能行为(可能会影响性能)。

A pointer is just a number . 指针只是一个数字 It has a special type to make sure it can represent the right range of numbers, and to aid program understanding, but basically it's just a number. 它具有特殊的类型,以确保可以代表正确的数字范围,并有助于程序理解,但是基本上它只是一个数字。

The number represents a memory address . 数字代表一个内存地址 You can't just make those up: you need to get a meaningful address from somewhere. 您不能仅仅弥补这些不足:您需要从某个地方获得有意义的地址。

You can: 您可以:

  • Take the address of an exiting variable: p = &a ; 取一个退出变量的地址: p = &a ; or 要么
  • Allocate some memory from the "heap": p = malloc(size) 从“堆”分配一些内存: p = malloc(size)

That's it, once you have the address of some memory, you're done! 就是这样,一旦有了一些内存的地址,就完成了! You can now dereference the pointer using the * operator anywhere you like: *p = somedata . 现在,您可以在任何喜欢的地方使用*运算符取消引用指针: *p = somedata

No, it doesn't allocate memory. 不,它不分配内存。 Your p actually points to the same location as &a and you modify the same memory in *p = 20; 您的p实际上指向与&a相同的位置,并且您在*p = 20;修改了相同的内存*p = 20; . This doesn't crash because you refer to the allocated memory (for a ). 这不会崩溃,因为你指的是分配的内存(对于a )。 If it's what you really want depends on you only. 如果这是您真正想要的,则仅取决于您。

However, I was wondering if initializing a pointer to some address allocates memory to it or not? 但是,我想知道是否初始化指向某个地址的指针是否为其分配了内存?

What do you mean by "allocates memory to it"? 您“为其分配内存”是什么意思? When you create a pointer you are allocating (statically) its memory, or, in other words, the memory needed for it to keep existing. 创建指针时,您正在(静态地)分配其内存,或者换句话说,为其保留内存所需的内存。

Apart from that, there is the memory where the pointer points to. 除此之外,还有指针指向的内存。 When you say int a = 10 you are creating an int and allocating a memory space large enough to contain it. 当您说int a = 10您正在创建一个int并分配足够大的内存空间来容纳它。
Saying then p = &a means: "my pointer must point to the memory address where a is stored". 那么说p = &a表示:“我的指针必须指向存储a的内存地址”。 Obviously you already allocated a , so you can safely use *p , but in general this is not true and you may have an uninitialized pointer pointing to some illegal address. 显然,您已经分配a ,因此可以安全地使用*p ,但是通常这不是正确的,并且您可能有一个未初始化的指针,该指针指向某个非法地址。

No, not needed. 不,不需要。 Because your pointer points to the address of a variable. 因为你的指针指向的地址a变量。 So, when you use *p = 20 , the value of a will change. 所以,当你使用*p = 20 ,值了a会改变。

So, the answer of your question is YES . 因此,您的问题的答案YES :) :)

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

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