473,383 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

I've lied to the compiler and now it won't trust me

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
Nov 14 '05 #1
6 1308
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
Nov 14 '05 #2
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
Nov 14 '05 #3
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
Nov 14 '05 #4
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
Nov 14 '05 #5
Je***********@physik.fu-berlin.de wrote:
void heap_trace_stack( union omg *ret0 )
if ( ret0[ -2 ] != NULL ) {


This must of course be

if ( ret0[ -2 ].o3 != NULL ) {

I hope you sptted that;-)
Regards, Jens

--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6
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
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

233
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...
27
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 -------------...
123
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...
1
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...
2
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...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
20
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. ...
8
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.