473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_stac k(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_sta ck':
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 1335
Adam Warner <us****@consult ing.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_stac k(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***********@p hysik.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****@consult ing.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_stac k(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****@consult ing.net.nz> wrote:
On Fri, 04 Feb 2005 11:38:22 +0000, Jens.Toerring wrote:
Adam Warner <us****@consult ing.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_trac e( omg_vla + 2 );

and use instead of
void heap_trace_stac k(struct o **ret0) {
//Follow local variables
if (ret0[-2]!=NULL) {
struct o **var=(struct o **) *ret0[-2]; //Line 207
void heap_trace_stac k( 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***********@p hysik.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***********@p hysik.fu-berlin.de wrote:
void heap_trace_stac k( 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***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #6
void heap_trace_stac k(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
8357
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 lack of C 99 compliant compilers. Is this just a lame excuse for back-sliding?
27
511
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 ------------- Version 1.2 supports the MS Windows XP platform, beside Linux x86 and Mac OSX. It also supports the DOUBLE floating point type. Please see the README file for details on DOUBLE floating point type.
123
3920
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 that's both readable and consistent, impossible. C# is hindered by its predecessors and the Microsoft marketing department. If Anders had his way, this language would be a one where readable code isn't a near impossibility for non-trivial...
1
1645
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 named, etc. Specifically, within Control Panel>Administrative Tools>Microsoft .NET Framework 2.0 Configuration>Assembly Cache, I have added myApp.exe to the global assembly cache. Also, within >Microsoft .NET Framework 2.0 Wizards>Trust an...
2
1853
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 machines I get a message: "missing or erroneous pg_hba.conf file" I set tcpip_socket = true in var/lib/pgsql/data/postgresql.conf I added '-i' optin to etc/rc.d/init.d/postgresql as follows
41
18190
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
20
4290
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. How can I dynamically load a LIB file and access all its functions? Surely someone has solved similar task? My intention is to use a Forth system for programming the controller,
8
2444
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 want to know what does the standard say about this situation. here is the code just to demonstrate the error.
1
2958
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 the config file and when you double-click the EXE, it works perfectly. However, from the shared drive, 2 things happen....
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.