Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > casting trouble

Reply
Thread Tools

casting trouble

 
 
macluvitch
Guest
Posts: n/a
 
      04-27-2004
Hello folks,

During developping I've met a problem this is how it looks like

I have an expression like this

(type *)var + 1

Wath heppens is I want to make a pointer to this

sth like ptr = &((type *)var + 1)
I know this is meaningless but I don't have the idea in mind

please could you give a suggestion ?

thanks


 
Reply With Quote
 
 
 
 
Karthik
Guest
Posts: n/a
 
      04-27-2004
macluvitch wrote:

> Hello folks,
>
> During developping I've met a problem this is how it looks like
>
> I have an expression like this
>
> (type *)var + 1

Assuming var is a pointer, I don't understand you are casting like
this (invites trouble, for sure).

>
> Wath heppens is I want to make a pointer to this
>
> sth like ptr = &((type *)var + 1)


You are incrementing the pointer variable by a scalar, (which sounds
fine). Why would you get the address of a pointer variable ?
That is UB. Did u want to do a dereferencing out here.

> I know this is meaningless but I don't have the idea in mind
>
> please could you give a suggestion ?


Please let us know the objetive clearly.

--
Karthik
Humans please 'removeme_' for my real email.
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      04-28-2004
"macluvitch" <> wrote:

> I have an expression like this
>
> (type *)var + 1
>
> Wath heppens is I want to make a pointer to this
>
> sth like ptr = &((type *)var + 1)
> I know this is meaningless but I don't have the idea in mind


Well, it can't be done that way. You cannot take the address of an
expression result; expression results have no definite address in
memory. If you really need a pointer that points at that value, you'll
need to create an object of the appropriate type, assign the value to
that object, then get its address. Like this:

compatible_type var;
type *dummy, **ptr;

var=somevalue;

dummy=((type *)var + 1);
ptr=&dummy;

Since the value of a pointer does not depend on the value of what it
points at (think about it: does your address depend on your name? If you
change your name by deed poll, does your address change?), this is
correct, too:

compatible_type var;
type *dummy, **ptr=&dummy;

var=somevalue;

dummy=((type *)var + 1);

dummy needs to be of type "type *", because that is the type of the
cast, and therefore of the whole expression. ptr needs to be of type
"type **", because it points at dummy.

Richard
 
Reply With Quote
 
macluvitch
Guest
Posts: n/a
 
      04-28-2004
Thank you folks this sounds to be meaningfull

I've tried this before posting but this should be a bit slow
so I was thinking of a small hack , I mean some way to do it like any
simple pointer affectation like I've mentioned before
Anyhow, how this will probably work great why troubling oneself

Ok thank you folks this is very kind of you
Oh I've another question (sorry)
The casting is done by the compiler or the assembler
(no optimizing option is turning on)
90% it should be done by the compiler what do you think ?

bye

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-28-2004
"macluvitch" <> wrote:

> I've tried this before posting but this should be a bit slow


How do you know? Have you measured it? Was the difference significant?
Does it occur often enough to have a measurable influence on your
program, or are you just worrying prematurely?

> Ok thank you folks this is very kind of you
> Oh I've another question (sorry)
> The casting is done by the compiler or the assembler


That question does not make sense in the context of ISO C. There's no
reason to assume the existence of an assembler.

Richard
 
Reply With Quote
 
macluvitch
Guest
Posts: n/a
 
      05-02-2004
I've got a look at gcc qasembly output . I think the casting is done by gcc

 
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
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
having trouble with casting operators.. JustSomeGuy C++ 7 08-22-2005 12:45 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Trouble casting a struct with bit fields Bob Hairgrove C++ 3 11-18-2004 11:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57