Welcome, Guest. Please login or register.

Author Topic: assignment problem with general pointers (VOID type )  (Read 1268 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
assignment problem with general pointers (VOID type )
« on: December 28, 2004, 07:01:04 PM »
Hey. I'm using jpeg.library and the pointer to the buffer I'm decompressing to is declared as APTR. Now the problem is that when I try to assign the value jpeg.library puts in it to another pointer VBCC complains sayint that it's an invalid type. If I try to cast it to the same type of pointer I'm copying it's value to, it complains saying that type VOID cannot be casted or something...
Maybe the easy way would be to copy it to some LONG first (haven't tryied it yet anyway...), but I wanted to know, is there a more elegant way around this?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #1 on: December 28, 2004, 07:08:16 PM »
By the way, the pointer I'm trying to copy the buffer pointer value to, was declared, if I remember correctly, something like:
Code: [Select]
struct Pixel
{BYTE Red;
 BYTE Green;
 BYTE Blue;
} *ImgPixelPosition;

 but I don't think this matters, it's just a pointer to a diferent type.

Hope the Pixel declaration doesn't coincide with some stuff on the Gfx library by the way, if I use it latter...
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #2 on: December 30, 2004, 02:30:34 AM »
Bump.. :-(
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Trev

  • Zero
  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show only replies by Trev
Re: assignment problem with general pointers (VOID type )
« Reply #3 on: December 30, 2004, 03:46:31 AM »
So you're trying to assign a `APTR' (typedef void *APTR) to a `struct Pixel *'? Well, in C, this should work:

APTR ptr;
struct Pixel *pixel;

func(&ptr);
pixel = (struct Pixel *)ptr;

In C++, your compiler might let you do something like this:

APTR ptr;
BYTE red;

func(&ptr);
red = static_cast(ptr)->Red;
static_cast(ptr)->Red = 0xff;

Trev
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #4 on: December 30, 2004, 07:13:26 PM »
@Trev

"So you're trying to assign a `APTR' (typedef void *APTR) to a `struct Pixel *'?"
Yes!

Thx for the tip. Not sure I uderstand the second part of the code (the C one).

func(&ptr); calls a function with an argument with a pointer to the ptr pointer correct? But for what?

pixel = (struct Pixel *)ptr;
This seems to be a normal cast.

Or did you mean:

func(&ptr);
void func(some type I don't know ptr, I guess void wouldn't work)
{pixel = (struct Pixel *)*ptr;
}

Ok, maybe this isn't what you mean.... :-D

Why da heck can't this be done easily in C :-o
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #5 on: December 30, 2004, 07:21:03 PM »
Hey!!! Got one idea.
What about declaring Ptr as a Union with both types of pointers (struct Pixel * and APTR) ? :-D

By the way, since I'm an optimization freak, will the compiler make less efficient code like this, or will it simply remind the the Ptr variable can be both types. If I recall correctly the variable types in C are just like a list the compiler keeps to avoid the programmer making errors, shouldn't affect the code except for when a Union contains two or more types of diferent sizes, in this case the only difference would still be the size of the memory block reserved for the variable (being a size that could take the biggest type in the Union), but performance wise the code would be as fast right? :-D

I'm gonna try this when I get home, but some of you could say something about it... Maybe we're just all preparing to get drunk in the end of the year :-D
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Trev

  • Zero
  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show only replies by Trev
Re: assignment problem with general pointers (VOID type )
« Reply #6 on: December 30, 2004, 08:15:34 PM »
You don't need a union for this. In fact, using a union for a type conversion would be an incorrect use of a union. It's only safe to read from a member of a union if that member was the last member written to:
Code: [Select]

struct my_struct {
  union {
    int int_val;
    void* ptr;
  };
};

void some_func()
{
  struct my_struct ms;
  void* ptr;

  ms.int_val = 1;
  ptr = ms.ptr; /* UNSAFE--newer compilers should issue a warning */
}

Anyhow, you should use unions when you want to conserve memory and you know exactly how your compiler and linker organizes structures and data in general in memory.

The code samples from my previous message were just samples of casting in C/C++. func() was just a hypothetical function that took a pointer to a pointer as a parameter. This might be clearer:
Code: [Select]

struct sample_struct {
  int value1;
  int value2;
};

APTR func(void); /* hypothetical function returns a pointer to struct sample_struct as a APTR */

int main(void)
{
  APTR ptr;
  struct sample_struct* ss;

  ptr = func();
  ss = (struct sample_struct*)ptr;
  ss->value1 = 1;

  /* OR */

  ss = (struct sample_struct*)func();
  ss->value1 = 1;
}

Now might be a good time to go over the rules for casting in the C standard. :-) If you're not using C++, then you can ignore my static_cast sample.

Also note that these are just casting examples. Be sure to keep on eye on those pointers, especially if they point to memory that needs to be deallocated.

Trev
 

Offline Einstein

  • Sr. Member
  • ****
  • Join Date: Dec 2004
  • Posts: 402
    • Show only replies by Einstein
Re: assignment problem with general pointers (VOID type )
« Reply #7 on: January 04, 2005, 02:07:50 PM »
Just a silly question as I'm not much at home in advanced C-programming: why does pointers in C need to be declared as certain types ? I figure this is a compiler-directive to have access to the different offsets in the varibales/structures/functions, am I correct ?  :-?
I have spoken !
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: assignment problem with general pointers (VOID type )
« Reply #8 on: January 04, 2005, 03:21:53 PM »
Quote

Einstein wrote:
Just a silly question as I'm not much at home in advanced C-programming: why does pointers in C need to be declared as certain types ? I figure this is a compiler-directive to have access to the different offsets in the varibales/structures/functions, am I correct ?  :-?


That's about it, really. At a machine level, pointers are just addresses. In the 680x0 for example, a pointer can be any 32-bit value. You usually have to care about the size of the element pointed to when modifying them.

In (slightly) higher level languages such as C, you define a pointer as pointing to a particular type. This can be anything , from a byte to a complex structure.

For example, assuming we have 32-bit integers;

int x;
int myInt[4] = { 0, 1, 2, 3 };
int *pInt = &myInt[1]; /* points to the second integer in the array */

C then takes care to correctly do the maths when you operations like

x = *(++pInt); /* x = 2 */
x = *(--pInt); /* x = 1 */
x = pInt[2]; /* x = 3 */

On a system with 32-bit integers and 32-bit pointers, the actual value of pInt would be changing by 4 when you use ++/-- and the index scaled by 4 when you use [].

Having typed pointers also helps to prevent many problems that could otherwise arise when dealing with structures etc.  The compiler can see the definition of a structure and can therefore ensure that the -> operation can only access the defined members of that structure.

The void pointer was introduced to deal with the requirement of uninitialised/undefined memory. The void pointer is purely designed to simply hold the address of something and does not allow dereferencing or arithmetic of any kind (since no information about the thing pointed to is known).
int p; // A
 

Offline Einstein

  • Sr. Member
  • ****
  • Join Date: Dec 2004
  • Posts: 402
    • Show only replies by Einstein
Re: assignment problem with general pointers (VOID type )
« Reply #9 on: January 04, 2005, 03:37:58 PM »
Thanks for the reply Karlos
I have spoken !
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #10 on: January 07, 2005, 10:34:41 PM »
Hey, it just stroke me, that must also be why it's not permited to do pointer arithmetic with type void right ? Since the compiler doesn't know the size of the type the void * pointer points to it can't do pointer arithmetic.
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: assignment problem with general pointers (VOID type )
« Reply #11 on: January 07, 2005, 10:54:45 PM »
Quote

Jose wrote:
Hey, it just stroke me, that must also be why it's not permited to do pointer arithmetic with type void right ? Since the compiler doesn't know the size of the type the void * pointer points to it can't do pointer arithmetic.


Exactly. As I said, void poiters cannot have any arithmetic applied to them and cannot be dereferenced.

Their sole purpose is to allow a pointer that can "point to anything" as a handle to raw memory.
int p; // A
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: assignment problem with general pointers (VOID type )
« Reply #12 on: January 07, 2005, 10:57:09 PM »
Ups, hadn't read that part... :-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: assignment problem with general pointers (VOID type )
« Reply #13 on: January 08, 2005, 02:14:40 PM »
Quote

Jose wrote:
Ups, hadn't read that part... :-)


No, but you grasped it anyway, which is even better than someone telling you :-)
int p; // A