[英]Assigning a value from pointer indexing
I am writing a program that needs optimization. 我正在编写一个需要优化的程序。 I have a pointer of n integers and need to save them in different arrays but sharing the memory.
我有n个整数的指针,需要将它们保存在不同的数组中,但要共享内存。 My question is:
我的问题是:
Is this 这是
int* ptr = (int*)malloc(sizeof(int)*10);
int** ptr2 = (int**)malloc(sizeof(int*)*10);
for (int i = 0; i < 10; ++i) {
ptr[i] = i;
ptr2[i] = &ptr[i];
}
equivalent to this? 相当于这个?
int* ptr = (int*)malloc(sizeof(int)*10);
int* ptr2 = (int*)malloc(sizeof(int)*10);
for (int i = 0; i < 10; ++i) {
ptr[i] = i;
ptr2[i] = ptr[i];
}
I mean, in the second case the value of *(ptr[i])
is copied into *(ptr2[i])
or ptr[i]
and ptr2[i]
point to the same memory address? 我的意思是,在第二种情况下,
*(ptr[i])
被复制到*(ptr2[i])
或ptr[i]
和ptr2[i]
指向相同的内存地址?
-- EDIT -- -编辑-
The reason I ask is I have a png image saved in an uint8_t* data
with R, G and B pixels contiguous, so for example, for the first pixel (R, G, B) = (data[0], data[1], data[2])
, for the second pixel, (R, G, B) = (data[3], data[4], data[5])
and so on. 我问的原因是我有一个保存在
uint8_t* data
中的png图像,其中R,G和B像素是连续的,因此,例如,对于第一个像素(R, G, B) = (data[0], data[1], data[2])
表示第二个像素(R, G, B) = (data[3], data[4], data[5])
,依此类推。 So I need to separate the three channels by storing the values in three different arrays, for obvious reasons I want to share the memory of the values used in the data
array and the ones used in the r_channel
, g_channel
and b_channel
. 因此,我需要通过将值存储在三个不同的数组中来分隔三个通道,出于明显的原因,我想共享
data
数组中使用的值以及r_channel
, g_channel
和b_channel
使用的值的内存。 So I don't know if I should declare each channel as uint8_t*
or uint8_t**
and assign its pixels like shown in the code snippets I posted as examples. 所以我不知道是否应该将每个通道都声明为
uint8_t*
或uint8_t**
并分配其像素,如我作为示例发布的代码片段所示。
In the following code ptr
is an array of integers(dynamically created) and is storing the values from 0 to 10, ptr2
is a integer pointer array where each element ( int *
) points to an element ( int
) of ptr. 在下面的代码中,
ptr
是一个整数数组(动态创建),并且存储从0到10的值, ptr2
是一个整数指针数组,其中每个元素( int *
)指向ptr的元素( int
)。
int* ptr = (int*)malloc(sizeof(int)*10); int** ptr2 = (int**)malloc(sizeof(int*)*10); for (int i = 0; i < 10; ++i) { ptr[i] = i; ptr2[i] = &ptr[i]; }
The following code creates a copy of ptr in ptr2. 以下代码在ptr2中创建ptr的副本。
int* ptr = (int*)malloc(sizeof(int)*10); int** ptr2 = (int**)malloc(sizeof(int*)*10); for (int i = 0; i < 10; ++i) { ptr[i] = i; ptr2[i] = &ptr[i]; }
So in example 2 if the value of an element in ptr changes it won't get reflected in ptr
whereas in example 1 if value in ptr
changes it would automatically get reflected in prt2
since elements in ptr2
are pointing directly to ptr
elements. 因此,在示例2中,如果ptr中元素的值发生更改,它将不会反映在
ptr
而在示例1中,如果ptr
值发生变化,它将自动反映在prt2
因为ptr2
中的元素直接指向了ptr
元素。
If you just want to access the data you can do that using pointer arithmetics eg for nth pixel R = data[3*n+0]; G = data[3*n+1]; B = data[3*n+2];
如果您只想访问数据,则可以使用指针算法进行操作,例如对于第n个像素
R = data[3*n+0]; G = data[3*n+1]; B = data[3*n+2];
R = data[3*n+0]; G = data[3*n+1]; B = data[3*n+2];
But if you intend to store it in memory creating a copy seems more logical as it would require less space, since sizeof(uint8_t) <= sizeof(uint8_t *)
但是,如果您打算将其存储在内存中,则创建副本似乎更合乎逻辑,因为它需要更少的空间,因为
sizeof(uint8_t) <= sizeof(uint8_t *)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.