通过强制转换取消引用 void 指针
dereferencing void pointers with casting
我在试验空指针时遇到了以下问题。当我编译下面的代码时一切正常但是当我 运行 它在我的机器上时它不会给我结果而是提示 The program has stopped working appeared(https://drive.google.com/file/d/0B1mLcnk8kTFUeEtmYnlOaWJ6T3c/view?usp=sharing) ,我不能找出幕后发生的事情,是否有关于取消引用 void
指针的任何问题。
此外,如果我使用注释代码而不是原始代码,它会起作用。
#include<stdio.h>
int main()
{
void* ptr;
int dupe = 5;
*(int* )ptr = dupe; // ptr = &dupe;
printf("The value at ptr is %3d",*(int* )ptr);
}
我在 CodeBlocks 上使用 gcc。
此处您试图取消引用指针而不为其分配内存。这导致 segmentation fault
或 undefined behaviour
.
int main(int argc, char *argv[])
{
void* ptr;
int dupe = 5;
ptr = malloc(sizeof(dupe)); /* alloc memory for ptr */
*(int* )ptr = dupe;
printf("The value at ptr is %3d",*(int* )ptr);
free(ptr);
return 0;
}
如果您不打算分配内存,那么只需在您的代码中将 *(int* )ptr = dupe;
替换为 ptr = &dupe
,因为现在 ptr
将指向变量 [=] 的有效内存位置16=]。
有关未定义行为的更多详细信息,请参阅 this
6. *(int* )ptr = dupe; // ptr = &dupe;
指针 ptr 未指向有效的内存位置。你在评论里写的,就是你应该做的。
语句ptr = &dupe
将使ptr指向变量dupe
的内存位置。
1. void* ptr; --->ptr is a void pointer that means it can hold an address of any data type, but currently it is not pointing to any address.
2. *(int* )ptr = dupe ---> ptr point to no where and you are trying to put a value which is not valid.
3. ptr = &dupe; --> in this case ptr will point to address of dump which is a valid one.
一个指针int *ptr
,通常是一个地址值,就像一个盒子的编号。这里的int
是盒子类型,暗示这个盒子是用来放一个int
的。当然,你也可以小心地转换它来容纳其他类型的东西,比如(otherKindType*)ptr
.
当尝试用 *ptr = someValue
引用一个指针时,比如将对象放在盒子里,你应该保证它指向某个准确的地方。
Code1(可以工作):
#include<stdio.h>
int main()
{
void* ptr; // ptr is a pointer, it should point to somewhere
int dupe = 5; // a box (named dupe) fitted for int which hold 5
ptr = &dupe; // ptr points to that box named dupe
printf("The value at ptr is %3d",*(int* )ptr); // get content inside the box
}
Code2(无效):
#include<stdio.h>
int main()
{
void* ptr;// pointer, point to a random place
int dupe = 5;
*(int* )ptr = dupe; // To put 5 to the box that pointed by ptr
// But now, ptr still points to a random place
// oops! To write to a random box is dangerous!
// It will cause error like `BUS error`
// Sometimes, to read a random box can output wired value.
printf("The value at ptr is %3d",*(int* )ptr);
}
Code3(可以工作):
#include<stdio.h>
int main()
{
void* ptr;
int dupe = 5;
int new_box;//added
ptr = &new_box;//added, now ptr points to new_box
*(int* )ptr = dupe; // To put 5(content of dupe) to the box that pointed by ptr
// That is to put 5 to new_box
printf("The value at ptr is %3d",*(int* )ptr); // get content inside the box
}
我在试验空指针时遇到了以下问题。当我编译下面的代码时一切正常但是当我 运行 它在我的机器上时它不会给我结果而是提示 The program has stopped working appeared(https://drive.google.com/file/d/0B1mLcnk8kTFUeEtmYnlOaWJ6T3c/view?usp=sharing) ,我不能找出幕后发生的事情,是否有关于取消引用 void
指针的任何问题。
此外,如果我使用注释代码而不是原始代码,它会起作用。
#include<stdio.h>
int main()
{
void* ptr;
int dupe = 5;
*(int* )ptr = dupe; // ptr = &dupe;
printf("The value at ptr is %3d",*(int* )ptr);
}
我在 CodeBlocks 上使用 gcc。
此处您试图取消引用指针而不为其分配内存。这导致 segmentation fault
或 undefined behaviour
.
int main(int argc, char *argv[])
{
void* ptr;
int dupe = 5;
ptr = malloc(sizeof(dupe)); /* alloc memory for ptr */
*(int* )ptr = dupe;
printf("The value at ptr is %3d",*(int* )ptr);
free(ptr);
return 0;
}
如果您不打算分配内存,那么只需在您的代码中将 *(int* )ptr = dupe;
替换为 ptr = &dupe
,因为现在 ptr
将指向变量 [=] 的有效内存位置16=]。
有关未定义行为的更多详细信息,请参阅 this
6. *(int* )ptr = dupe; // ptr = &dupe;
指针 ptr 未指向有效的内存位置。你在评论里写的,就是你应该做的。
语句ptr = &dupe
将使ptr指向变量dupe
的内存位置。
1. void* ptr; --->ptr is a void pointer that means it can hold an address of any data type, but currently it is not pointing to any address.
2. *(int* )ptr = dupe ---> ptr point to no where and you are trying to put a value which is not valid.
3. ptr = &dupe; --> in this case ptr will point to address of dump which is a valid one.
一个指针int *ptr
,通常是一个地址值,就像一个盒子的编号。这里的int
是盒子类型,暗示这个盒子是用来放一个int
的。当然,你也可以小心地转换它来容纳其他类型的东西,比如(otherKindType*)ptr
.
当尝试用 *ptr = someValue
引用一个指针时,比如将对象放在盒子里,你应该保证它指向某个准确的地方。
Code1(可以工作):
#include<stdio.h>
int main()
{
void* ptr; // ptr is a pointer, it should point to somewhere
int dupe = 5; // a box (named dupe) fitted for int which hold 5
ptr = &dupe; // ptr points to that box named dupe
printf("The value at ptr is %3d",*(int* )ptr); // get content inside the box
}
Code2(无效):
#include<stdio.h>
int main()
{
void* ptr;// pointer, point to a random place
int dupe = 5;
*(int* )ptr = dupe; // To put 5 to the box that pointed by ptr
// But now, ptr still points to a random place
// oops! To write to a random box is dangerous!
// It will cause error like `BUS error`
// Sometimes, to read a random box can output wired value.
printf("The value at ptr is %3d",*(int* )ptr);
}
Code3(可以工作):
#include<stdio.h>
int main()
{
void* ptr;
int dupe = 5;
int new_box;//added
ptr = &new_box;//added, now ptr points to new_box
*(int* )ptr = dupe; // To put 5(content of dupe) to the box that pointed by ptr
// That is to put 5 to new_box
printf("The value at ptr is %3d",*(int* )ptr); // get content inside the box
}