Sujet : Re: New VSI post on Youtube
De : arne (at) *nospam* vajhoej.dk (Arne Vajhøj)
Groupes : comp.os.vmsDate : 23. Aug 2024, 18:03:44
Autres entêtes
Organisation : SunSITE.dk - Supporting Open source
Message-ID : <66c8c0f0$0$705$14726298@news.sunsite.dk>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Mozilla Thunderbird
On 8/23/2024 9:02 AM, Arne Vajhøj wrote:
On 8/23/2024 4:14 AM, Michael S wrote:
and that the best option in C is the same
as in many other languages - return the structure itself.
Returning the struct itself result in a copy of the struct. The
time to do the copy is probably insignificant though.
I am not quite convinced yet.
It seems like one frequently provided reason for using pointer
is ABI compatibility.
I don't know about that. It is not that easy to create the
problem.
But it is possible:
$ type i.h
struct data
{
int a;
int b;
#ifdef NEWVERSION
int c;
#endif
};
$ type s1.c
#include "i.h"
struct data get()
{
struct data res;
res.a = 123;
res.b = 456;
#ifdef NEWVERSION
res.c = 0x7FFFFFFF;
#endif
return res;
}
$ type m1.c
#include <stdio.h>
#include "i.h"
struct data get();
int main()
{
struct data res = get();
printf("%d %d\n", res.a, res.b);
return 0;
}
$ type s2.c
#include <stdlib.h>
#include "i.h"
struct data *get()
{
struct data *res = malloc(sizeof(struct data));
res->a = 123;
res->b = 456;
#ifdef NEWVERSION
res->c = 0x7FFFFFFF;
#endif
return res;
}
$ type m2.c
#include <stdio.h>
#include <stdlib.h>
#include "i.h"
struct data *get();
int main()
{
struct data *res = get();
printf("%d %d\n", res->a, res->b);
free(res);
return 0;
}
$ cc s1
$ cc m1
$ link m1 + s1
$ run m1
123 456
$ cc/define="NEWVERSION" s1
$ link m1 + s1
$ run m1
99108 0
$ cc s2
$ cc m2
$ link m2 + s2
$ run m2
123 456
$ cc/define="NEWVERSION" s2
$ link m2 + s2
$ run m2
123 456
Arne