473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem on using a casted void pointer

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

Sep 23 '07 #1
10 2838
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.

Sep 23 '07 #2
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

Sep 23 '07 #3
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.
Sep 23 '07 #4
On Sep 23, 1:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
hmm it just turns 47 to 47.0000000 ..nothing more than that , but my
prob. is not that..

Sep 23 '07 #5
kkirtac wrote:
On Sep 23, 1:33 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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.

hmm it just turns 47 to 47.0000000 ..nothing more than that , but my
prob. is not that..
Ah, sorry, I misread your parenthesis (common problem with yucky code!).
In that case, the cast is unnecessary.

The casts in you original are also unnecessary.

Your best bet is to put the switch in a function and call that from the
loop.

--
Ian Collins.
Sep 23 '07 #6
kkirtac wrote:
Error : 1) 'void*' : unknown size
2) 'typecast' : cannot convert from void to double
Does you compiler really use the word "typecast"?

--
pete
Sep 23 '07 #7
kkirtac wrote:
>
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:
.... snip ...
>
void* ptr;
.... snip ...
>
case IPL_DEPTH_8S: ptr = (signed char *) rowVect->data.ptr;
break;
.... snip ...
>
Error : 1) 'void*' : unknown size
2) 'typecast' : cannot convert from void to double
Don't cast there. You are placing the result of the cast in a
void*, which immediately casts it right back to a void*, which
can't be dereferenced, etc.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 23 '07 #8
CBFalconer <cb********@yahoo.comwrites:
kkirtac wrote:
>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:
... snip ...
>>
void* ptr;
... snip ...
>>
case IPL_DEPTH_8S: ptr = (signed char *) rowVect->data.ptr;
break;
... snip ...
>>
Error : 1) 'void*' : unknown size
2) 'typecast' : cannot convert from void to double

Don't cast there. You are placing the result of the cast in a
void*, which immediately casts it right back to a void*, which
can't be dereferenced, etc.
No, it's immediately *converted* back to void*. A cast is an explicit
operator; there's no such thing as an implicit cast.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 23 '07 #9
On Sun, 23 Sep 2007 , kkirtac <ka**********@gmail.comwrote:
>Hi, i have a void pointer and i cast it to an appropriate known type
before using.
Unfortunately, that is not what you have. What you have is a void
pointer to which you assign a cast-ed value, thereby losing any
information the cast might have provided.
>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)
There is no readrow. The major, perhaps only, benefit of comments is
help someone reading the code understand what is happening. Incorrect
or misleading comments perform the opposite function.

If anyone is tempted to try to compile your code, your use of // style
comments makes it impractical. Line wrap is a fact of life in Usenet.
Use /* */ style comments and eliminate syntax errors introduced by
message readers.
>

void* ptr;
switch (images[0]->depth)
{
case IPL_DEPTH_8U :
ptr = (unsigned char *) rowVect->data.ptr ; //ptr is defined as an
unsigned char pointer
Nothing you do on the right hand side will change the definition of
ptr. It is still a pointer to void. Perhaps you meant data.ptr. If
so, then the cast is particularly useless (as opposed to generally
useless) since you do not change the type of the expression. See next
comment.
break; // data is 'union' type
case IPL_DEPTH_8S :
ptr = (signed char *) rowVect->data.ptr ;
There is an implicit conversion defined between object pointers and
pointers to void. It is never necessary to cast one to the other on a
simple assignment.
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
Now you introduce confusion. Every other assignment to ptr was from a
pointer. Are you really trying to stuff the VALUE of a double into a
void*. (I hope not. There is no guarantee it will fit. This is also
no guarantee that such a conversion is defined). Perhaps you meant
the comment to day "db is a double pointer" which would be consistent
with your previous code and your for loop below.
break;
}
At this point, ptr contains the address obtained from one of the
pointers in the data union. While you were going through the switch,
you actually determined what type of data you want to deal with.
However, now all you have is a void* that points to something but you
no longer know what that something is.
>

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
ptr is a pointer to void. If it were a valid expression, ptr[k] would
be the k'th void ptr pointed to. However, void is a permanently
incomplete type (it can never be completed). As such, an expression
of type void is not an arithmetic value that can be converted to
double.

While you compiler will have no trouble distinguishing between Data
and data, I don't think most people who would be tempted to help will
appreciate the distinction.
//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...
The only place where you know the type of data that ptr points to is
inside each switch case. That would be the logical place to process
the data. Furthermore, it would make life so much simpler to use the
"original" pointers (i, fl, etc) rather than try to use ptr for
everything. For example

case IPL_DEPTH_32S :
{
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] = rowVect->data.i[k];
/* Since I don't know anything about your structures, unions, or
non-standard functions, I don't vouch for the validity of any of the
expressions. I am only translating your code to work within the case.
*/
i++;
}
}

With the exception of the expression data.i, this same code would be
used for all cases. That would make this a candidate for a macro.
Remove del for email
Sep 24 '07 #10
On Sep 24, 3:19 am, Barry Schwarz <schwa...@doezl.netwrote:
On Sun, 23 Sep 2007 , kkirtac <kadir.kir...@gmail.comwrote:
Hi, i have a void pointer and i cast it to an appropriate known type
before using.

Unfortunately, that is not what you have. What you have is a void
pointer to which you assign a cast-ed value, thereby losing any
information the cast might have provided.
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)

There is no readrow. The major, perhaps only, benefit of comments is
help someone reading the code understand what is happening. Incorrect
or misleading comments perform the opposite function.

If anyone is tempted to try to compile your code, your use of // style
comments makes it impractical. Line wrap is a fact of life in Usenet.
Use /* */ style comments and eliminate syntax errors introduced by
message readers.
void* ptr;
switch (images[0]->depth)
{
case IPL_DEPTH_8U :
ptr = (unsigned char *) rowVect->data.ptr ; //ptr is defined as an
unsigned char pointer

Nothing you do on the right hand side will change the definition of
ptr. It is still a pointer to void. Perhaps you meant data.ptr. If
so, then the cast is particularly useless (as opposed to generally
useless) since you do not change the type of the expression. See next
comment.
break; // data is 'union' type
case IPL_DEPTH_8S :
ptr = (signed char *) rowVect->data.ptr ;

There is an implicit conversion defined between object pointers and
pointers to void. It is never necessary to cast one to the other on a
simple assignment.
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

Now you introduce confusion. Every other assignment to ptr was from a
pointer. Are you really trying to stuff the VALUE of a double into a
void*. (I hope not. There is no guarantee it will fit. This is also
no guarantee that such a conversion is defined). Perhaps you meant
the comment to day "db is a double pointer" which would be consistent
with your previous code and your for loop below.
break;
}

At this point, ptr contains the address obtained from one of the
pointers in the data union. While you were going through the switch,
you actually determined what type of data you want to deal with.
However, now all you have is a void* that points to something but you
no longer know what that something is.
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

ptr is a pointer to void. If it were a valid expression, ptr[k] would
be the k'th void ptr pointed to. However, void is a permanently
incomplete type (it can never be completed). As such, an expression
of type void is not an arithmetic value that can be converted to
double.

While you compiler will have no trouble distinguishing between Data
and data, I don't think most people who would be tempted to help will
appreciate the distinction.
//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...

The only place where you know the type of data that ptr points to is
inside each switch case. That would be the logical place to process
the data. Furthermore, it would make life so much simpler to use the
"original" pointers (i, fl, etc) rather than try to use ptr for
everything. For example

case IPL_DEPTH_32S :
{
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] = rowVect->data.i[k];
/* Since I don't know anything about your structures, unions, or
non-standard functions, I don't vouch for the validity of any of the
expressions. I am only translating your code to work within the case.
*/
i++;
}
}

With the exception of the expression data.i, this same code would be
used for all cases. That would make this a candidate for a macro.

Remove del for email
Thank you, embedding the loop into "switch-case"s looks the best way..
Regards

Sep 24 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bj?rn Toft Madsen | last post by:
Hi all, The network library I use communicates with my app using a callback function. This callback function is called with the type of message, a void pointer to he actual message and a user...
2
by: Timo Qvist | last post by:
Hi, I know there is a proper way to do this but I find myself with a void pointer returned from a malloc( sizeof( class T ) ) which gives me a pointer to a memory area of the size of class T. The...
5
by: Niks | last post by:
what is a void pointer?
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
12
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg =...
23
by: Eric J.Hu | last post by:
Hi, I have following code, want do pointer convert. It always complain: vcnvt.c: In function `main': vcnvt.c:20: warning: dereferencing `void *' pointer vcnvt.c:20: request for member `key'...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
5
by: A. Farber | last post by:
Hello, I call readv() and writev() in several spots of a program which I run under Linux, OpenBSD and Cygwin. Since it always the same way (check the return value; then check errno and retry if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.