0

People have marked this post as a duplicate of this but it's actually not because I'm not asking what is a composite type but what is its purpose and i gave a use case where it could cause a harm for software developers and then asked if this should be undefined behavior instead.

I was reading that part of the C11 standard about composite types and i was just like : Why the heck on earth would someone do something like that ?

I mean i can't see anything interesting about that. I think this is very confusing for programmers. For the sake of example, let's say we had a function void f() that people used everywhere in the software, they just declare extern void f() and use it. The definition of the function is somewhere programmers don't care about; then one day, one smart guy needed to add an argument to that function and changed the definition to something like void f(int *ptr) {/* ..*/ return *ptr;}. The software will still compile happily but god, all that legacy code that used the old interface is screwed up with segfaults.

The code below is a demo :

/* main.c */
#include <stdio.h>

int f (); 
int p = 5;

int main () 
{
    int r = f ();   /* This call will cause segfault */
    int s = f (&p);

    printf ("r=%d / s=%d \n", r, s);

    return 0;
}


/* another_unit.c */

int f (int *ptr) 
{
    return *ptr;
}

I don't see the purpose of such a thing. In my opinion that should be undefined behavior or at least why do compilers like GCC don't say anything about it ?

Karim Manaouil
  • 1,177
  • 10
  • 24
  • Check out this question https://stackoverflow.com/questions/16417928/what-is-a-composite-type-in-c – Nikolai Fetissov Aug 19 '18 at 01:45
  • Possible duplicate of [What is a composite type in C?](https://stackoverflow.com/questions/16417928/what-is-a-composite-type-in-c) – David Maze Aug 19 '18 at 02:19
  • @NikolaiFetissov It doesn't answer my question. I'm not even asking what is a composite type. It's a different question with different goals. – Karim Manaouil Aug 19 '18 at 02:35
  • 2
    @DavidMaze It's not the same question at all. I'm willing to know something else about composite types & not what they are. I have a completely different goal. Please read my post before commenting. Beside that the link you gave doesn't have enough information in the answer. It doesn't even answer the original post and of course neither mine. – Karim Manaouil Aug 19 '18 at 02:37
  • 1
    One of the answers to the duplicate question explains why composite types are needed: in order to discuss relationship between *complete* and *incomplete* types. Your question seems to focus more on old-style function declarations, which are an obsolescent feature of C (which means it is going to be deprecated and eventually removed from the language). Compilers do warn about such declarations if you ask nicely. – n. m. could be an AI Aug 19 '18 at 03:12
  • 1
    Just because the feature *allows* programmers shoot their own foot off, is not good enough reason to make it Undefined Behaviour. C is not a hand-holding language; the programmers are responsible for making sure the code behaves in a sane manner. – Nominal Animal Aug 19 '18 at 06:27
  • @Karim, first, I didn't say it was a duplicate, second, the answers there provide the reasoning for the given language feature, which does look like what you were asking about. – Nikolai Fetissov Aug 19 '18 at 14:32

1 Answers1

4

The point in your example is that the function f() has no prototype, and the other one has one. The form of declaration without comes from early C and is marked as obsolete since many years.

The construct of composite type between the two helped when prototypes were introduced into C to accommodate between old code and new code.

Calling a function with arguments that do not correspond to the parameters that it expects has undefined behavior, so with that mechanism you have to know what you are doing.

In short, don't use declarations without prototype. They are outdated since long and might be removed from the C standard at any time.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Now, it makes more sense since composite types are something related to C's history and the need for them was to accommodate between old code and new code. I mean it wasn't something that people just brought up like this because they thought it was cool. – Karim Manaouil Aug 19 '18 at 15:13