C取消引用指针 [英] C Dereferencing pointer
问题描述
我开始学习C,但我不知道为什么该应用程序持续崩溃.
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[50];
int age;
}person;
void initPerson(person *aPerson, char name[50], int age)
{
strcpy(aPerson->name, name); // <- causes a crash
aPerson->age = age; // <- causes a crash
printf("%s", aPerson->name);
}
int main()
{
person *myPerson;
initPerson(myPerson, "Oscar", 45);
printf("%s, %i", myPerson->name, myPerson->age);
return 0;
}
我已标记出导致崩溃的行.有人可以告诉我是什么问题吗?
"...该应用不断崩溃.,...有人可以告诉我这是什么问题吗?"
是的,您的程序正在尝试写入它不拥有的存储位置.
如果必须使用指向person
的指针,请在使用它之前创建一些内存:
int main()
{
person *myPerson = calloc(1, sizeof(*myPerson));
if(!myPerson) return 0; //test if failed
initPerson(myPerson, "Oscar", 45);
printf("%s, %i", myPerson->name, myPerson->age);
free(myPerson);//free memory
return 0;
}
或者,您可以简单地通过person
的非指针实例的地址(使用&
)来获得相同的结果:
int main()
{
person myPerson = {0};
initPerson(&myPerson, "Oscar", 45);
printf("%s, %i", myPerson.name, myPerson.age);
return 0;
}
声明:
person *myPerson;
仅创建未初始化的指针,在创建时不指向内存中的任何特定位置.此时唯一使用的空间是指针本身sizeof(person *)
. (对于32位或64位目标,分别为4或8个字节.)在可以通过这种方式使用指针变量之前,必须通过调用_ 免费() ,当它不再需要时.通常,仅当在编译时不知道特定变量的内存要求时才建议使用此方法
但声明:
person myPerson;
静态(或自动,具体取决于创建时间/位置.)为myPerson
的立即可用实例分配具有sizeof(person)
个字节的内存在内存中,地址为:&myPerson
.以这种方式创建的内存称为堆栈内存.因为将myPerson
(&myPerson
)的地址作为参数传递给initPerson()
,并完成与动态分配的内存相同的操作(如上所述),所以这是一个简单得多的选项,因为它不需要任何操作.创建或释放内存.
i started to learn C, and i dont know why the app keeps crashing.
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[50];
int age;
}person;
void initPerson(person *aPerson, char name[50], int age)
{
strcpy(aPerson->name, name); // <- causes a crash
aPerson->age = age; // <- causes a crash
printf("%s", aPerson->name);
}
int main()
{
person *myPerson;
initPerson(myPerson, "Oscar", 45);
printf("%s, %i", myPerson->name, myPerson->age);
return 0;
}
I have marked the lines that are causing the crash. Can someone tell me whats the problem?
"...the app keeps crashing., ...Can someone tell me whats the problem?"
Yes, your program is attempting to write to a memory location it does not own.
If you must use a pointer to person
, create some memory before using it:
int main()
{
person *myPerson = calloc(1, sizeof(*myPerson));
if(!myPerson) return 0; //test if failed
initPerson(myPerson, "Oscar", 45);
printf("%s, %i", myPerson->name, myPerson->age);
free(myPerson);//free memory
return 0;
}
Or, you can simply pass the address of a non-pointer instance of person
(using &
) to get the same results:
int main()
{
person myPerson = {0};
initPerson(&myPerson, "Oscar", 45);
printf("%s, %i", myPerson.name, myPerson.age);
return 0;
}
The statement:
person *myPerson;
Creates only an uninitialized pointer, not pointing to any particular place in memory at the time of creation. The only space used at this point is for the pointer itself, sizeof(person *)
. (either 4 or 8 bytes for 32bit or 64 bit target respectively.) Before a pointer variable can be used in this way, space must be dynamically allocated by a call to _void *calloc(size_t nitems, size_t size)_ or family. Memory created in this fashion sets the address of the pointer to a memory location coincident with the first byte of a contiguous block of nitems*size
bytes set aside and dedicated for use with, in this case, myPerson
. Memory allocated in this fashion is referred to as heap memory and must be explicitly freed with a call to free() when it is no longer needed. Generally, this method is recommended only when memory requirements for a particular variable are not known at compile time
But the statement:
person myPerson;
Statically (Or automatically, depending on when/where it is created.) allocates memory with sizeof(person)
bytes for an immediately usable instance of myPerson
, located in memory at the address: &myPerson
. Memory created in this fashion is referred to as stack memory. Because passing the address of myPerson
( &myPerson
) as an argument to initPerson()
, and accomplish the same thing as was accomplished with dynamically allocated memory (discussed above), this is a much simpler option as it does not require any memory creation or freeing.
Stack and heap memory explained.
Discussion on automatic, static and dynamic memory allocation
这篇关于C取消引用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!