0/1 Knapsack problem, what am I doing wrong

R

Richard Bos

But you can use something like signed char* as your intermediate, which is
guaranteed to be able to hold any (suitably-cast) pointer, but requires
casts both ways.

You can do that, but it is no safer than using a void * in the first
place; in particular, the casts add no safety whatsoever.

Richard
 
J

jameskuyper

blargg said:
(e-mail address removed) wrote: ....

Debatable; having to cast means having to intentionally convert the
pointer type, while using void* means it gets converted to whatever
pointer type is needed, even if you didn't realize the type it was
converting to.

So let me get this straight:

enum inttype {SCHAR, SHORT, INT, LONG, LLONG};

int void_func(void* pv, enum inttype code)
{
switch(code)
{
case SCHAR:
signed char * psc = pv;
*psc = 42;
break;
// etc. for the other codes.
}
}

intptr_t caller()
{
intpt_t n;
void_func(&l, LONG);
return n;
}

Is less safe than:

enum inttype {SCHAR, SHORT, INT, LONG};

int void_func(char* pv, enum inttype code)
{
switch(code)
{
case SCHAR:
signed char * psc = (signed char*)pv;
*psc = 42;
break;
// etc. for the other codes.
}
}

intptr_t caller()
{
intptr_t n;
char_func((char*)&l, LONG);
return n;
}

How exactly is the greater safety of the second form achieved? In
particular, how would it make it any easier for the user to find out
if he's made a mistaken assumption about what intprt_t is?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top