Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代码的分段错误 #包括 #包括 #包括 #包括 使用名称空间std; 联合型{ INTA; 字符b; int*p; char*s; int-arr[10]; }; 内部fn(活接头类型*exp){ exp->p=exp->p+1; cout(p); cout a=10; str->b='n'; str->p=&(str->a); cout(p); cout_C++ - Fatal编程技术网

C++ 代码的分段错误 #包括 #包括 #包括 #包括 使用名称空间std; 联合型{ INTA; 字符b; int*p; char*s; int-arr[10]; }; 内部fn(活接头类型*exp){ exp->p=exp->p+1; cout(p); cout a=10; str->b='n'; str->p=&(str->a); cout(p); cout

C++ 代码的分段错误 #包括 #包括 #包括 #包括 使用名称空间std; 联合型{ INTA; 字符b; int*p; char*s; int-arr[10]; }; 内部fn(活接头类型*exp){ exp->p=exp->p+1; cout(p); cout a=10; str->b='n'; str->p=&(str->a); cout(p); cout,c++,C++,您正在声明一个指向联合的指针,但该指针没有指向任何需要malloc/new的有效内存。它指向的是未定义的(垃圾指针).您正在声明一个指向联合的指针,但该指针没有指向任何需要malloc/new的有效内存。它指向的是未定义的(垃圾指针) 这段代码给了我分段错误。是因为我需要 使用malloc为联合显式分配内存 对。你的str指针没有指向有效的内存位置,它甚至没有初始化。因此,在写入str->a之前,你需要将str设置为某个值 这段代码给了我分段错误。是因为我需要 使用malloc为联合显式分配内存

您正在声明一个指向联合的指针,但该指针没有指向任何需要malloc/new的有效内存。它指向的是未定义的(垃圾指针).

您正在声明一个指向联合的指针,但该指针没有指向任何需要malloc/new的有效内存。它指向的是未定义的(垃圾指针)

这段代码给了我分段错误。是因为我需要 使用malloc为联合显式分配内存

对。你的
str
指针没有指向有效的内存位置,它甚至没有初始化。因此,在写入
str->a
之前,你需要将
str
设置为某个值

这段代码给了我分段错误。是因为我需要 使用malloc为联合显式分配内存


<> P>右。你的代码> STR < /Cube指针没有指向有效的内存位置,甚至没有初始化。所以,在编写<代码> STR> > <代码>之前,你需要设置<代码> STR 。

尝试用“<代码> MalOC/学习C++”,奇怪的、毫无意义的联盟不会让你走远。FAQ中有一个好的书列表。为什么str必须是一个指向某个类型的指针,而不仅仅是一个类型?如果你只是删除*并将every->更改为a.,一切都会正常工作。(在fn原型中,你可能想将*转换为a&而不是删除它。)此外,即使你使用malloc str(或用非指针替换它),fn(str)也会失败(但可能不是segfault,除非幸运)。将str->p设置为指向str本身的指针,然后将其递增,因此它现在指向sizeof(int)字节过去的STR,它要么是STR的中间,要么是内存中STR之后的任何对象,或者是未初始化的内存,没有一个是你想打印的东西。通过使用<代码> MalOC/学习C++,而奇怪的、毫无意义的联盟不会让你走远。FAQ中有一个好的书的列表。有什么理由吗?n str必须是指向一个类型的指针,而不仅仅是一个类型?如果您只是删除*并将every->更改为a.,一切都会工作。(在fn原型中,您可能希望将*转换为a&而不是删除它。)此外,即使您使用malloc str(或用非指针替换它),fn(str)也会失败(但可能不是segfault,除非幸运)。将str->p设置为指向str本身的指针,然后将其递增,因此它现在指向sizeof(int)str之后的字节,它要么是str的中间部分,要么是内存中str之后的任何对象,或者是未初始化的内存,这些都不是您可能想要打印为int的内容。我将这段代码添加到代码中,它可以工作:str=(union type*)malloc(sizeof(type));我将这段代码添加到代码中,它可以工作:str=(union type*)malloc(sizeof(类型));
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

using namespace std;

union type{
            int a;
            char b;
            int *p;
            char *s;
            int arr[10];
};

int fn(union type *exp){

    exp->p = exp->p+1;
    cout << *(exp->p);
    cout << "\n";
return 0;
}

int main(){

    union type *str;
    str->a = 10;
    str->b = 'n';
    str->p = &(str->a);
    cout << (str->p);
    cout << "\n";
    fn(str);
    cout << str->p;
    cout << "\n";
return 0;
}