Hi all,
When adding variable length arrays to my program I created the elements as
type struct o *. This is because /most/ of the time the VLAs contains
pointers to struct o objects (these objects are as stringently aligned as
any C type in the implementation). Occasionally a single element is
actually a pointer to another VLA of pointers to struct o objects (and
occasionally I'm storing an integer index (of type ptrdiff_t) as an
immediate object).
Casting a pointer to an integer is no problem. But GCC will not compile my
program when I tell it that an element of an array is actually a pointer
to the same type of array:
void heap_trace_stack(struct o **ret0) {
//Follow local variables
if (ret0[-2]!=NULL) {
struct o **var=(struct o **) *ret0[-2]; //Line 207
index num_var=(index) var[0];
...
}
...
}
The code above has been passed a VLA offset to the first element of a
particular category (&ret0[0]). If the element at the legal index position
-2 is not NULL then it's actually a pointer to another VLA of the same
type. The line 207 cast is rejected by GCC as an error (gcc-3.4 -std=c99
-O2 -Wall):
heap.c: In function `heap_trace_stack':
heap.c:207: error: cannot convert to a pointer type
If I have to change the VLAs to elements of type void ** then I'll have to
declare many variables as pointers (to pointers ...) to void; and castings
to struct o * will cascade though the program and it will become far less
type safe as a whole.
Perhaps this is the C way. Advice appreciated.
Regards,
Adam 6 1302
Adam Warner <us****@consulting.net.nz> wrote: When adding variable length arrays to my program I created the elements as type struct o *. This is because /most/ of the time the VLAs contains pointers to struct o objects (these objects are as stringently aligned as any C type in the implementation). Occasionally a single element is actually a pointer to another VLA of pointers to struct o objects (and occasionally I'm storing an integer index (of type ptrdiff_t) as an immediate object).
I hope you never will have to port your program... Why don't you simply
use an array of unions? And how do you figure out what's the type of a
certain element when you pass it to another function, as you seem to
do? You have a good chance that you can't distinguish i.e. the ptrdiff_t
value 0 from a NULL pointer... And determining if something is a pointer
to a structure or a pointer to an array of structures is impossible,
even if you use some platform-specific extra information (like that you
know that a ptrdiff_t value must always be a small value and memory
addresses are always larger).
Casting a pointer to an integer is no problem. But GCC will not compile my program when I tell it that an element of an array is actually a pointer to the same type of array:
void heap_trace_stack(struct o **ret0) { //Follow local variables if (ret0[-2]!=NULL) { struct o **var=(struct o **) *ret0[-2]; //Line 207
While a negative array index looks definitely strange I have to take
your word that it's correct. But the problem you ask about is that
ret0 is of type 'struct o **'. Then 'ret0[-2]' is of type 'struct o *'
and finally '*ret[-2]' is of type 'struct o'. And the compiler doesn't
like to cast a structure to pointer to pointer to structure, which
seems to be rather reasonable - what would be the value of a structure
that would have to be assigned to the pointer?
The code above has been passed a VLA offset to the first element of a particular category (&ret0[0]). If the element at the legal index position -2 is not NULL then it's actually a pointer to another VLA of the same type.
Sorry, but all of this sounds like a real maintainance nightmare in the
making. I hope _you_ still understand all the non-standard and weird
stuff you seem to be doing there in just a few days...
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
On Fri, 04 Feb 2005 11:38:22 +0000, Jens.Toerring wrote: Adam Warner <us****@consulting.net.nz> wrote: When adding variable length arrays to my program I created the elements as type struct o *. This is because /most/ of the time the VLAs contains pointers to struct o objects (these objects are as stringently aligned as any C type in the implementation). Occasionally a single element is actually a pointer to another VLA of pointers to struct o objects (and occasionally I'm storing an integer index (of type ptrdiff_t) as an immediate object). I hope you never will have to port your program...
Try to avoid jumping to misconclusions. My request concerns a type issue
I'm experiencing. Once resolved this will again be portable C99 code.
Why don't you simply use an array of unions?
Try overlaying a VLA of length m+n+3 with these types:
| struct o *** | m | struct o *[m] | n | struct o *[n] |
-2 -1 0 m m+1 m+n
^pointer to this position is passed.
And how do you figure out what's the type of a certain element when you pass it to another function, as you seem to do?
It's aways passed at a particular offset.
You have a good chance that you can't distinguish i.e. the ptrdiff_t value 0 from a NULL pointer...
There's no chance involved. The type depends on the array position.
And determining if something is a pointer to a structure or a pointer to an array of structures is impossible, even if you use some platform-specific extra information (like that you know that a ptrdiff_t value must always be a small value and memory addresses are always larger).
I know what they are by their index position. I just have to coerce the
compiler into believing me. Casting a pointer to an integer is no problem. But GCC will not compile my program when I tell it that an element of an array is actually a pointer to the same type of array:
void heap_trace_stack(struct o **ret0) { //Follow local variables if (ret0[-2]!=NULL) { struct o **var=(struct o **) *ret0[-2]; //Line 207
While a negative array index looks definitely strange I have to take your word that it's correct. But the problem you ask about is that ret0 is of type 'struct o **'. Then 'ret0[-2]' is of type 'struct o *' and finally '*ret[-2]' is of type 'struct o'.
Then I may be following one too many levels of indirection!
And the compiler doesn't like to cast a structure to pointer to pointer to structure, which seems to be rather reasonable - what would be the value of a structure that would have to be assigned to the pointer?
It's only generally reasonable if the type system can process the original
data layout. What should be the type of my original VLA? void *** where
every element is cast to its correct type? I'd prefer an array of struct o
*vla[m+n+3] where I only have to cast index positions -2, -1 and m to
their correct type (after offsetting the pointer into the array).
Regards,
Adam
Adam Warner <us****@consulting.net.nz> wrote: On Fri, 04 Feb 2005 11:38:22 +0000, Jens.Toerring wrote:
Adam Warner <us****@consulting.net.nz> wrote: When adding variable length arrays to my program I created the elements as type struct o *. This is because /most/ of the time the VLAs contains pointers to struct o objects (these objects are as stringently aligned as any C type in the implementation). Occasionally a single element is actually a pointer to another VLA of pointers to struct o objects (and occasionally I'm storing an integer index (of type ptrdiff_t) as an immediate object).
I hope you never will have to port your program...
Try to avoid jumping to misconclusions. My request concerns a type issue I'm experiencing. Once resolved this will again be portable C99 code.
Why don't you simply use an array of unions?
Try overlaying a VLA of length m+n+3 with these types:
| struct o *** | m | struct o *[m] | n | struct o *[n] | -2 -1 0 m m+1 m+n ^pointer to this position is passed. And how do you figure out what's the type of a certain element when you pass it to another function, as you seem to do?
It's aways passed at a particular offset.
You have a good chance that you can't distinguish i.e. the ptrdiff_t value 0 from a NULL pointer...
There's no chance involved. The type depends on the array position.
And determining if something is a pointer to a structure or a pointer to an array of structures is impossible, even if you use some platform-specific extra information (like that you know that a ptrdiff_t value must always be a small value and memory addresses are always larger).
<rearranged>
I know what they are by their index position. I just have to coerce the compiler into believing me.
<rearranged>
It's only generally reasonable if the type system can process the original data layout. What should be the type of my original VLA? void *** where every element is cast to its correct type? I'd prefer an array of struct o *vla[m+n+3] where I only have to cast index positions -2, -1 and m to their correct type (after offsetting the pointer into the array).
Admitedly I don't understand what you try to do there, but why use a VLA
of structure pointers for something that rather clearly isn't one? As far
as I can see you just want some memory area with m+n+3 times the size of
th largest entity you want to store there. Using a VLA of unions would
simplify things a lot, because then it's clear what type is accessed
by accessing the appropriate union member and you don' have to "lie"
to the compiler.
If you had e.g.
union omg {
struct o ***o3;
size_t count;
struct o **o2;
};
and
union omg omg_vla[ m + n + 3 ];
which should take exactly as much space as your *vla[m+n+3]. Then you
could call the function as
heap_stack_trace( omg_vla + 2 );
and use instead of void heap_trace_stack(struct o **ret0) { //Follow local variables if (ret0[-2]!=NULL) { struct o **var=(struct o **) *ret0[-2]; //Line 207
void heap_trace_stack( union omg *ret0 )
if ( ret0[ -2 ] != NULL ) {
struct o **var = *ret0[ -2 ].o3;
so you don't need a single cast and hopefully things are easier to
understand for someone else (and possibly for you in the long run;-)
BTW, you can "repair" your original code
struct o **var=(struct o **) *ret0[-2]; //Line 207
by using instead
<
struct o **var = * ( struct o *** ) ret0[ -2 ];
since 'struct O ***' is what you seem to have storted at ret0[-2].
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
On Fri, 04 Feb 2005 14:12:08 +0000, Jens.Toerring wrote: Admitedly I don't understand what you try to do there, but why use a VLA of structure pointers for something that rather clearly isn't one? As far as I can see you just want some memory area with m+n+3 times the size of the largest entity you want to store there. Using a VLA of unions would simplify things a lot, because then it's clear what type is accessed by accessing the appropriate union member and you don' have to "lie" to the compiler.
If you had e.g.
union omg { struct o ***o3; size_t count; struct o **o2; };
and
union omg omg_vla[ m + n + 3 ];
which should take exactly as much space as your *vla[m+n+3].
That's fantastic! I finally understand your point thanks Jens. This will
be my first use of a top level union. I've used unions within structures
but I overlooked the flipside.
Many thanks,
Adam void heap_trace_stack(struct o **ret0) { //Follow local variables if (ret0[-2]!=NULL) { struct o **var=(struct o **) *ret0[-2]; //Line 207 index num_var=(index) var[0]; ... } ... }
Line 207 you are converting a struct itself into a pointer, which is the
problem.
ret0[-2] returns a "struct o *"
*ret0[-2] returns a "struct o", which can't possibly be cast into a pointer.
Perhaps you meant to do:
(struct o **) ret0[2]
Perhaps?
Anyway, that is what I think the problem is, at least, whatever the fix is.
Jon
----
Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: E. Robert Tisdale |
last post by:
I have access to a wide variety of different platforms here at JPL
and they all have pretty good C 99 compilers.
Some people claim that they have not moved to the new standard
because of the...
|
by: Mohd Hanafiah Abdullah |
last post by:
Axiomatic Solutions Sdn Bhd announces the availability of AMPC version 1.2.
You can purchase AMPC online now at:
http://www.axiomsol.com/hedesu/shopping/index.php
Major Changes
-------------...
|
by: C# Learner |
last post by:
I've had enough of C#. I've had enough of using parentheses for every
'if' statement. I've had enough of having to mix assignment of return
value of methods with flow control, making writing code...
|
by: Loane Sharp |
last post by:
Hi there
I have a Windows Forms application that contacts the server and retrieves
particular files, if they exist. To the best of my knowledge, the .exe
assembly is properly compiled, strongly...
|
by: Liza |
last post by:
Hi,
I've just setup postgreSQL on my Fedora Core1 box. I installed
postgresql from rpm files.
When I try to connect to the postgresql server in pgmanage, or through
an ODBC from other windows...
|
by: Miroslaw Makowiecki |
last post by:
Where can I download Comeau compiler as a trial version?
Thanks in advice.
|
by: Nickolai Leschov |
last post by:
Hello all,
I am programming an embedded controller that has a 'C' library for using
its system functions (I/O, timers, all the specific devices). The
supplied library has .LIB and .H files.
...
|
by: =?GB2312?B?yum09MXt?= |
last post by:
today I forgot to include some header,then I found the error message by the compiler is quite strange.
so I want to know exactly the inner details of the compiler impletation,if possible.
and I...
|
by: =?Utf-8?B?bXVzb3NkZXY=?= |
last post by:
Hi guys
I've just written a new C# application which needs to go on a Shared drive
on our network.
Now, the application works perfectly well from my local C: drive. I've
copied the EXE and...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |