Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Type-casting problems

Reply
Thread Tools

Type-casting problems

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Tobin) writes:

> In article <(E-Mail Removed)>,
> Ben Bacarisse <(E-Mail Removed)> wrote:
>
>>Post, if you can, minimal code that exhibits the problem. I don't get
>>any error from valgrind if I add the minimum required to make your
>>code fragment work. The error (about memcpy) also suggests it relates
>>some other part of the code that you did not post

>
> The memcpy() is probably the structure assignment. And you probably
> didn't see an error because in your minimum program the assigned-to
> structure wasn't used, so the assignment was optimised away.


Kind of you to assume I'd be silly, but no.

The memcpy is probably the assignment but the error relates to "some
other part of the code that [the OP] did not post" in that you can't
get the reported error (at least I can't) if the structure is
"correct". Unless we see the struct, we can only guess at the error.

> The error is simple: he allocates space for an int, then copies it as
> a whole struct.


No problem if the struct is the right size. I want to see the whole
program before assuming even an "obvious" mistake. I think it helps
more to say that what was posted was insufficient, but we all have our
own ways of helping.

--
Ben.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-20-2008
Ramon <(E-Mail Removed)> writes:

> Richard Tobin wrote:
>> In article <frtj7i$s9$(E-Mail Removed)>,
>> Ramon <(E-Mail Removed)> wrote:
>>
>>>> What are you trying to achieve? Why do you think you can assign
>>>> a block of sizeof(int) bytes to something which is supposed to point
>>>> to 100 bytes?

>>
>>> As i've said before,
>>> The problem is that processResult() is designed to be a generic function; i.e. you don't know the type of result that it will process --
>>> for example, sometimes it may return an integer, while sometimes
>>> it may return a "string". It depends on the implementation of the
>>> user.
>>>
>>> In this case it was an int, but it may be any other data-structure
>>> (hopefully).

>>
>> If it was an int, why did you assign it to a struct?
>>
>> -- Richard


Best to snip sig blocks (OK, just one line in this case, but it is
still the preferred posting style to remove them).

> Ok here is non-C stuff. As far as i know, POSIX message queues do not
> allows you to transmit pointers from one process to another (since
> each process has a protected address space). Hence one cannot assign
> an int to a void and post it to a message queue.
>
> To what type (except void) can i cast it (the int or something)?


You need to separate your different problems.

(1) To solve the problem of passing an address between POSIX processes
you need to post in comp.unix.programmer explaining what you are trying
to do at the top level. The best solution may be very different to
fixing the "how can I turn a pointer into something else" problem.

(2) You specific problem here is that taking a pointer and treating as
a pointer to something bigger will always be an error. Often, the
solution is that generic data needs to be represented as a void * and
an size_t to say how big the object is. You function might have to
return a

struct generic_data { void *ptr; size_t size; };

However, your problem (1) may well be solved by some entirely
different approach (i.e. shared memory or some RPC-like structure
marshaling) so this problem could end up being secondary.

--
Ben.
 
Reply With Quote
 
 
 
 
Rod Pemberton
Guest
Posts: n/a
 
      03-20-2008
"Ramon" <(E-Mail Removed)> wrote in message
news:frtj7i$s9$(E-Mail Removed)...
> Richard Tobin wrote:
> > In article <frtg1p$hdl$(E-Mail Removed)>,
> > Ramon <(E-Mail Removed)> wrote:
> >> How can i solve this problem without changing the type of result --

i.e.
> >> leaving result as a pointer to void?

> >
> > What are you trying to achieve? Why do you think you can assign
> > a block of sizeof(int) bytes to something which is supposed to point
> > to 100 bytes?
> >

>
> As i've said before,
> The problem is that processResult() is designed to be a generic
> function; i.e. you don't know the type of result that it will process --
> for example, sometimes it may return an integer, while sometimes it
> may return a "string". It depends on the implementation of the user.
>
> In this case it was an int, but it may be any other data-structure
> (hopefully).


Like the other responders, I think you're "barking up the wrong tree" for a
couple of reasons. "Why, why, why?" It seems that there is a fundamental
problem with the structure of your original program that would require you
to need a function that can be "overloaded" with numerous types and allocate
them too. Overloading is for C++, not C. C has a typesystem. Abusing C's
typesystem when you don't know how to cast, when to cast, or how to
determine if the cast is correct, is almost guaranteed to lead you into
trouble since you'll be unable to locate and fix any errors with the casts.
I.e., if you have to ask, you shouldn't be doing it. But, it seems you just
want some code to solve your problem. So, here's some code. I don't know
if it'll solve your problem. It has some limitations or restrictions: 1)
sizeof() must return the size for the desired type, 2) must also have a
pointer to the type, 3) type must be assignable (i.e., copies data when
assigned to same type)... I've been up almost 19 hours so you'll have to
locate any errors, illegal/undefined behavior, etc. You should pass it
through Valgrind.

#include <stdlib.h>

struct mytask
{
char info[100];
};

void* processResult(size_t insize)
{
void *res;

if(insize<(sizeof(int)))
insize=sizeof(int);
res=malloc(insize);
*(int *)res=505;

return(res);
}

void foo(void)
{
void *result=NULL;
struct mytask mtask,*ptask;

result=processResult(sizeof(struct mytask));

ptask=((struct mytask *)result);
mtask=*ptask;
}

int main(void)
{
foo();

return(0);
}


Rod Pemberton

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-21-2008
Ramon wrote:
>

.... snip ...
>
> Ok here is non-C stuff. As far as i know, POSIX message queues
> do not allows you to transmit pointers from one process to another
> (since each process has a protected address space). Hence one
> cannot assign an int to a void and post it to a message queue.


I have no idea what your real problem is, but consider that there
is probably a very good reason for such a POSIX ban. Memory space
in one process can be entirely different from memory space in
another process. Thus the same 'pointer' value could point to
entirely different entities.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving the web, charset problems and symbols problems Sak Na rede Ruby 0 01-30-2009 05:05 AM
Problems, problems for newbie Shelly ASP .Net 1 09-03-2007 02:10 AM
Problems compiling simple C++ code (also problems with std::string) Susan Baker C++ 2 06-26-2005 01:43 AM
Re: sound problems and modem problems Harold Potter Computer Support 5 12-04-2003 04:12 PM



Advertisments