使用铸造空指针的问题 [英] Problem on using a casted void pointer

查看:63
本文介绍了使用铸造空指针的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无效指针,我在使用前将其转换为适当的已知类型

。我投下这个无效*的类型来自英特尔的开源计算机视觉库

。这是我的非工作代码:


CvMat * rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images [0] -


> depth,1));



// rowVect是指向CvMat类型的指针


cvReshape(images [0],rowVect,0,1) ; //矢量化图像; readrow

是一个1xN矩阵(向量)

void * ptr;


switch(images [0] - >深度)

{

case IPL_DEPTH_8U:

ptr =(unsigned char *)rowVect-> data.ptr; // ptr定义为

unsigned char指针

break; //

数据是''union''类型


case IPL_DEPTH_8S:

ptr =(signed char *)rowVect- > data.ptr;

break;


case IPL_DEPTH_16S:

ptr =(unsigned short *)rowVect-> data.ptr;

break;


case IPL_DEPTH_32S:

ptr =(int *)rowVect-> data.i ; //我被定义为一个int指针

break;


case IPL_DEPTH _32F:

ptr =(float *)rowVect - > data.fl; // fl定义为float

指针

break;


case IPL_DEPTH_64F:

ptr =(double *)rowVect-> data.db; // db是一个双$

休息;


}


int i = 0,k;

while(i< nsamples)

{

cvReshape(images [i],readrow,0,1);

for(k = 0; k
Data-> data.db [i *(Data-> width)+ k] =(double)ptr [k]; //错误发生

这里

//填写数据矩阵

i ++;

}


错误:1)''void *'':未知大小

2)''typecast'':无法从void转换为double

>
im抱歉,如果在这里指向第三方图书馆是违法的,但它还是用标准C写的
但我的问题是纯粹的C

语言我想的问题。我投了无效指针,但后来我无法使用

它...


问候

解决方案

9月23日晚上7:14,kkirtac< kadir.kir ... @ gmail.comwrote:


我有一个无效指针,我在使用前将其转换为适当的已知类型

。我投下这个无效*的类型来自英特尔的开源计算机视觉库

。这是我的非b / b $ b b工作代码:


CvMat * rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images [0] - > depth ,1));


// rowVect是指向CvMat类型的指针


cvReshape(images [0],rowVect,0,1 ); //矢量化图像; readrow

是一个1xN矩阵(向量)

void * ptr;


switch(images [0] - >深度)

{

case IPL_DEPTH_8U:

ptr =(unsigned char *)rowVect-> data.ptr; // ptr定义为

unsigned char指针

break; //

数据是''union''类型


case IPL_DEPTH_8S:

ptr =(signed char *)rowVect- > data.ptr;

break;


case IPL_DEPTH_16S:

ptr =(unsigned short *)rowVect-> data.ptr;

break;


case IPL_DEPTH_32S:

ptr =(int *)rowVect-> data.i ; //我被定义为一个int指针

break;


case IPL_DEPTH _32F:

ptr =(float *)rowVect - > data.fl; // fl定义为float

指针

break;


case IPL_DEPTH_64F:

ptr =(double *)rowVect-> data.db; // db是一个双$

休息;


}


int i = 0,k;

while(i< nsamples)

{

cvReshape(images [i],readrow,0,1);

for(k = 0; k
Data-> data.db [i *(Data-> width)+ k] =(double)ptr [k]; //错误发生

这里

//填写数据矩阵

i ++;

}


错误:1)''void *'':未知大小

2)''typecast'':无法从void转换为double

>
im抱歉,如果在这里指向第三方图书馆是违法的,但它还是用标准C写的
但我的问题是纯粹的C

语言我想的问题。我投了无效指针,但后来我无法使用

它...


问候



尝试重写(双)ptr [k]为((double *)ptr)[k]


问候,

B. br />


9月23日下午12:50,boroph ... @ gmail.com写道:


9月23日晚上7:14,kkirtac< kadir.kir ... @ gmail.comwrote:


我有一个无效指针和在使用之前,我将其转换为适当的已知类型

。我投下这个无效*的类型来自英特尔的开源计算机视觉库

。这是我的非工作代码:


CvMat * rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(图像[0] - >深度,1));


// rowVect是指向CvMat类型的指针


cvReshape (images [0],rowVect,0,1); //矢量化图像; readrow

是1xN矩阵(向量)


void * ptr;


switch(images [0] - > depth)

{

case IPL_DEPTH_8U :

ptr =(unsigned char *)rowVect-> data.ptr; // ptr定义为

unsigned char指针

break; //

数据是''union''类型


case IPL_DEPTH_8S:

ptr =(signed char *)rowVect-> data.ptr;

break;


case IPL_DEPTH_16S:

ptr =(unsigned short *)rowVect-> data.ptr;

休息;


case IPL_DEPTH_32S:

ptr =(int *)rowVect-> data.i; //我被定义为一个int指针

break;


case IPL_DEPTH _32F:

ptr =(float *)rowVect-> data.fl; // fl定义为float

指针

break;


case IPL_DEPTH_64F:

ptr =(double *)rowVect-> data.db; // db是双$
休息;


}


int i = 0,k;

while(i< nsamples)

{

cvReshape(images [i],readrow,0,1);

for(k = 0; k
Data-> data.db [i *(Data-> width)+ k] =(double)ptr [k]; //错误发生

这里

//填写数据矩阵

i ++;

}


错误:1)''void *'':未知大小

2)''typecast'':无法从void转换为double


im抱歉,如果将第三方库指向此处是违法的,那么

也是用标准编写的C.但我的问题是纯粹的C

语言。我想的问题。我投了一个无效指针,但后来我无法使用

它...


问候


尝试重写(双)ptr [k]为((double *)ptr)[k]


问候,

B.



嗯,谢谢你,但问题是,如果我们在

ptr前设置一个双*,那么它会读取对应于''double''类型的字节数。

我想在这里做的是决定字节数(

将在前面的类型) ''ptr'')在上面''switch-case''语句中

一次,并在每次迭代中读取该字节数,将其转换为

double类型。例如,如果switch-case决定ptr将是
一个unsigned char *,那么而不是while循环中的double *,$
想要unsigned char *放在那里,然后将结果转换为

像这样:(double)((unsigned char *)ptr)[k]。但问题是

i希望这会自动发生..我不想在while循环中运行

switch-case来决定那个......对不起,如果这个

太复杂了..

问候


kkirtac写道:
< blockquote class =post_quotes>
>

嗯,谢谢你,但问题是,如果我们在

ptr前设置一个双*,那么它读取对应于类型''double''的字节数。

我想要做的是决定字节数(

将来的类型)在''ptr'')前面的'switch-case''语句

一次,并在每次迭代中读取该字节数,将其转换为

双重型。例如,如果switch-case决定ptr将是
一个unsigned char *,那么而不是while循环中的double *,$
想要unsigned char *放在那里,然后将结果转换为

像这样:(double)((unsigned char *)ptr)[k]。



这没有意义,如果ptr是unsigned char *,ptr [k + 1]将是一个

字节来自ptr [k],所以它没有任何意义,并且可能会崩溃

诱导将这些任意字节序列转换为双倍。


-

Ian Collins。


Hi, i have a void pointer and i cast it to an appropriate known type
before using. The types which i cast this void* to are, from the
Intel''s open source computer vision library. Here is my piece of non-
working code:

CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images[0]-

>depth,1));

//rowVect is a pointer to CvMat type

cvReshape( images[0], rowVect, 0, 1 ); //vectorize the image; readrow
is a 1xN matrix(vector)

void* ptr;

switch (images[0]->depth)
{
case IPL_DEPTH_8U :
ptr = (unsigned char *) rowVect->data.ptr ; //ptr is defined as an
unsigned char pointer
break; //
data is ''union'' type

case IPL_DEPTH_8S :
ptr = (signed char *) rowVect->data.ptr ;
break;

case IPL_DEPTH_16S :
ptr = (unsigned short *) rowVect->data.ptr ;
break;

case IPL_DEPTH_32S :
ptr = (int *) rowVect->data.i ; // i is defined as an int pointer
break;

case IPL_DEPTH _32F :
ptr = (float *) rowVect->data.fl ; // fl is defined as float
pointer
break;

case IPL_DEPTH_64F :
ptr = (double *) rowVect->data.db ; // db is a double
break;

}

int i=0, k;
while(i<nsamples)
{
cvReshape( images[i], readrow, 0, 1 );
for(k=0; k<nfeatures; k++)
Data->data.db[i*(Data->width)+k] = (double) ptr[k]; // errors occur
here
//fill the Data matrix
i++;
}

Error : 1) ''void*'' : unknown size
2) ''typecast'' : cannot convert from void to double

i m sorry if it is illegal to point a 3rd party library here, but it
has also been written in standard C. But my problem is a pure "C
language" problem i think. I cast a void pointer but then i cant use
it...

Regards

解决方案

On Sep 23, 7:14 pm, kkirtac <kadir.kir...@gmail.comwrote:

Hi, i have a void pointer and i cast it to an appropriate known type
before using. The types which i cast this void* to are, from the
Intel''s open source computer vision library. Here is my piece of non-
working code:

CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images[0]->depth,1));

//rowVect is a pointer to CvMat type

cvReshape( images[0], rowVect, 0, 1 ); //vectorize the image; readrow
is a 1xN matrix(vector)

void* ptr;

switch (images[0]->depth)
{
case IPL_DEPTH_8U :
ptr = (unsigned char *) rowVect->data.ptr ; //ptr is defined as an
unsigned char pointer
break; //
data is ''union'' type

case IPL_DEPTH_8S :
ptr = (signed char *) rowVect->data.ptr ;
break;

case IPL_DEPTH_16S :
ptr = (unsigned short *) rowVect->data.ptr ;
break;

case IPL_DEPTH_32S :
ptr = (int *) rowVect->data.i ; // i is defined as an int pointer
break;

case IPL_DEPTH _32F :
ptr = (float *) rowVect->data.fl ; // fl is defined as float
pointer
break;

case IPL_DEPTH_64F :
ptr = (double *) rowVect->data.db ; // db is a double
break;

}

int i=0, k;
while(i<nsamples)
{
cvReshape( images[i], readrow, 0, 1 );
for(k=0; k<nfeatures; k++)
Data->data.db[i*(Data->width)+k] = (double) ptr[k]; // errors occur
here
//fill the Data matrix
i++;
}

Error : 1) ''void*'' : unknown size
2) ''typecast'' : cannot convert from void to double

i m sorry if it is illegal to point a 3rd party library here, but it
has also been written in standard C. But my problem is a pure "C
language" problem i think. I cast a void pointer but then i cant use
it...

Regards



Try rewriting (double) ptr[k] as ((double *)ptr)[k]

Regards,
B.


On Sep 23, 12:50 pm, boroph...@gmail.com wrote:

On Sep 23, 7:14 pm, kkirtac <kadir.kir...@gmail.comwrote:

Hi, i have a void pointer and i cast it to an appropriate known type
before using. The types which i cast this void* to are, from the
Intel''s open source computer vision library. Here is my piece of non-
working code:

CvMat *rowVect = cvCreateMat(1,nfeatures,CV_MAKETYPE(images[0]->depth,1));

//rowVect is a pointer to CvMat type

cvReshape( images[0], rowVect, 0, 1 ); //vectorize the image; readrow
is a 1xN matrix(vector)

void* ptr;

switch (images[0]->depth)
{
case IPL_DEPTH_8U :
ptr = (unsigned char *) rowVect->data.ptr ; //ptr is defined as an
unsigned char pointer
break; //
data is ''union'' type

case IPL_DEPTH_8S :
ptr = (signed char *) rowVect->data.ptr ;
break;

case IPL_DEPTH_16S :
ptr = (unsigned short *) rowVect->data.ptr ;
break;

case IPL_DEPTH_32S :
ptr = (int *) rowVect->data.i ; // i is defined as an int pointer
break;

case IPL_DEPTH _32F :
ptr = (float *) rowVect->data.fl ; // fl is defined as float
pointer
break;

case IPL_DEPTH_64F :
ptr = (double *) rowVect->data.db ; // db is a double
break;

}

int i=0, k;
while(i<nsamples)
{
cvReshape( images[i], readrow, 0, 1 );
for(k=0; k<nfeatures; k++)
Data->data.db[i*(Data->width)+k] = (double) ptr[k]; // errors occur
here
//fill the Data matrix
i++;
}

Error : 1) ''void*'' : unknown size
2) ''typecast'' : cannot convert from void to double

i m sorry if it is illegal to point a 3rd party library here, but it
has also been written in standard C. But my problem is a pure "C
language" problem i think. I cast a void pointer but then i cant use
it...

Regards


Try rewriting (double) ptr[k] as ((double *)ptr)[k]

Regards,
B.

Hmm thank you but the problem here is, if we set a double* in front of
ptr, then it reads amount of bytes that corresponds to type ''double''.
What i want to do here is to decide the amount of bytes(the type that
will come in front of ''ptr'') in the upper ''switch-case'' statement
once, and read that amount of bytes in each iteration, cast it to
double type. For instance, if the switch-case decides that ptr will be
an unsigned char* , then instead of the double* in the while loop, i
want unsigned char* be placed there, and then convert the result to
double like this : (double)((unsigned char *)ptr)[k] . But problem is
i want this to happen automatically..that i dont want to run the
switch-case inside the while loop to decide for that..I m sorry if it
got too complicated..
Regards


kkirtac wrote:

>
Hmm thank you but the problem here is, if we set a double* in front of
ptr, then it reads amount of bytes that corresponds to type ''double''.
What i want to do here is to decide the amount of bytes(the type that
will come in front of ''ptr'') in the upper ''switch-case'' statement
once, and read that amount of bytes in each iteration, cast it to
double type. For instance, if the switch-case decides that ptr will be
an unsigned char* , then instead of the double* in the while loop, i
want unsigned char* be placed there, and then convert the result to
double like this : (double)((unsigned char *)ptr)[k] .

That doesn''t make sense, if ptr is unsigned char*, ptr[k+1] will be one
byte on from ptr[k], so it makes no sense and is potentially crash
inducing to cast these arbitrary byte sequence to double.

--
Ian Collins.


这篇关于使用铸造空指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆