简体   繁体   English

C-将struct内部的指针重新分配给struct外部的指针

[英]C - Reassign pointer inside of struct to pointer outside of struct

I have two pointers, each pointing to a different array. 我有两个指针,每个指针指向一个不同的数组。 One of the pointers is inside of a struct, as follows: 指针之一在结构内部,如下所示:

typedef struct
{
    int     N;    /* Number of Elements in array */
    double *c;    /* Pointer to an array         */
    /* Other members....  */
} CS;

The struct was initialized by: 该结构通过以下方式初始化:

CS->N = n; /* n is an integer of an initially unknown size */
CS->c = (double *)malloc(n * sizeof(double));

The pointer in the struct, CS->C , contains data that I no longer care about. 结构中的指针CS->C包含我不再关心的数据。

My other pointer was defined as follows: 我的其他指针定义如下:

double *alpha;
alpha = (double *)malloc(CS->N * sizeof(double));

I need to replace the contents of CS->C with alpha . 我需要用alpha替换CS->C的内容。 I know I can do something naive like: 我知道我可以做一些天真的事情,例如:

for (i=0;i<CS->N;i++) /* i is an integer */
    CS->c[i] = alpha[i];

I could also use memcpy, like this: 我也可以这样使用memcpy:

memcpy(CS->c,alpha,CS->N * sizeof(double));

My understanding is that both of these methods will copy the contents from memory located at alpha to the memory occupied by CS->C . 我的理解是,这两种方法都会将内容从位于alpha的内存复制到CS->C占用的内存。 That being a very expensive operation, it would make more sense to simply change the assignment of CS->C to alpha . 这是一个非常昂贵的操作,将CS->C的分配简单更改为alpha会更有意义。

How can I do this? 我怎样才能做到这一点?

I've tried to reassign the pointer by doing like CS->C = &alpha , but this gives me the following warning "assignment from incompatible pointer type". 我试图通过像CS->C = &alpha这样来重新分配指针,但这给了我以下警告“从不兼容的指针类型分配”。

Note: This is using ANSI C89 under full compliance, ie the compiler options are: -Wall -pedantic -ansi 注意:这是在完全符合要求的情况下使用ANSI C89,即编译器选项为: -Wall -pedantic -ansi

Edit 1 编辑1
Freeing CS->c and assigning it to alpha by doing: 释放CS->c并将其分配给alpha是:

free(CS->c);
CS->c = alpha;

does not work. 不起作用。 It causes every entry in CS->c to become equal to 0.0 and it results in my program seg faulting. 它导致CS->c每个条目都等于0.0 ,并导致我的程序段错误。

Edit 2 编辑2
I think I realized why the method suggested in my first edit did not work. 我想我知道为什么我第一次编辑中建议的方法不起作用。 alpha is a temporary pointer, created and initialized inside of a function, so once that function is exited, the memory occupied by alpha is "freed". alpha是在函数内部创建和初始化的临时指针,因此一旦退出该函数, alpha占用的内存将被“释放”。 Since CS->c points to that memory, it is also freed. 由于CS->c指向该内存,因此它也被释放。 Upon this discovery, I think I will rework my code, such that alpha and CS-c are initially swapped, such that when they are switched again, the end order will be correct. 基于这一发现,我想我将重新编写代码,以便最初交换alphaCS-c ,以便当再次切换它们时,结束顺序将是正确的。 Thank you all for you valuable input. 谢谢大家的宝贵意见。

Just copy the pointer 只需复制指针

CS->C = alpha;

Alpha is a pointer to double, CS->C too, types matches, you simply change the pointer. Alpha是指向double的指针,CS-> C也是,类型匹配,您只需更改指针即可。

/!\\ Don't forgot to free the old CS->C ... /!\\不要忘记释放旧的CS-> C ...

Free the old pointer and assing the new directly. 释放旧指针,然后直接连接新指针。 (Not the adress of the new pointer) (不是新指针的地址)

The pointer in the struct, CS->c , contains data that I no longer care about. 结构体中的指针CS->c包含我不再关心的数据。

My other pointer was defined as follows: 我的其他指针定义如下:

double *alpha;
alpha = malloc(CS->N * sizeof(double));

... ...

free (CS->c);
CS->c = alpha ; alpha=NULL;

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

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