Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

f77 to c - using struct 2

Status
Not open for further replies.

ielo

Programmer
Jan 22, 2004
15
0
0
CA
I need to integrate a few C subroutines within a f77 program. I've tested on many occasions passing 2D arrays back and forth between C and f77 before, so that's ok. However, in this instance, there's a lot more data to be handled by the C code (data that the f77 code doesn't really need to 'see') and the data really lends itself well to using C structs (would make my life easier if I could store and manipulate the data in C struct arrays, instead of using f77 common blocks and/or arrays). However, the only way I've managed to use the benefits of C here (structs, malloc, pointers, etc.), is to declare a static struct within a C subroutine. That works as long as the same C subroutine is called by f77 main program. If I want the subroutine to do something different within the f77 main program, all I've managed to do is send an integer 'flag' as an argument: depending on the value of the flag, the subroutine will do different stuff. My question is the following: is there anyway to have the struct become 'global' once it's declared in the first C subroutine encountered? That would give me the opportunity to use several different C subroutines within the f77 main program and still use the same C structs ... (I'm testing this out under gcc by the way).

If required, I can type out a C file and f77 main program for you to test ...
 
Do you mean 'global' as accessible to the Fortran prog or 'global' as accessible to all C subroutines? If it is the latter, then

In header file

extern struct ...

In all source files

#include "header file"

In one source - call it something like global.c

struct ...
 
I left something out: what I really want is a struct POINTER which is global to all C subroutines, allowing me to use malloc in one (the first) C subroutine I use, then have all the other C subroutines access the same dynamically-declared array of structs. So a few changes to what you posted ... still tweaking with it, but once I find it, I'll post the specific solution to my problem ... in the meantime, if you (or anyone else) spots a problem with this, let me know ...
 
OK, a few minutes after the last post ... quite simple, but if any hardcore C programmers see a problem with this let me know:

In a "header file", following the usual struct definitions, e.g.
typedef struct
{
int braincells;
} human;


declare 'global variable':
human* pointertohumans;

and voilà, a 'global' pointer to a 'human' struct type. Within the first C subroutine called by the f77 main program, one can
pointertohuman = (human*)malloc(nbofhumans*sizeof(human));

... and then have access to its content within any other C subroutine:
i = pointertohumans[j].braincells; or whatever else ...
 
> pointertohuman = (human*)malloc(nbofhumans*sizeof(human));
Just a couple of points...
1. Casting the result of malloc hides a potentially serious problem if you fail to include stdlib.h at the same time.
Casting malloc if you have included stdlib.h does nothing, since ANSI-C automatically casts [tt]void*[/tt] to [tt]type*[/tt] for you.

If the compiler complains about void* casts, it usually means you're compiling your C code with a C++ compiler.

2. Using the sizeof 'of the dereferenced pointer' is one less maintenance problem to think about.
Code:
pointertohumans = malloc(nbofhumans*sizeof(*pointertohumans));
You can change the type of pointertohumans and the compiler automatically adjusts the malloc call for you :)


--
 
I have a curious, follow up question to my initial one:

In the F77 program I'm modifying, some C header files are added after the program declaration:

PROGRAM COUNTHUMANBRAINCELLS
#include "human.h"
...
CALL CFUNCTION()
...
RETURN
END


In my previous posts, I suggested a simple solution, obvious to some, on how to use a global C pointer to a dynamically declared array of C structs by declaring it within a header file, e.g. 'human.h'. The actual F77 program using the C functions, which in turn use the global pointer to the struct array, has a short life span before its ends (a few minutes), so I'm ASSUMING that all used memory is freed when the program ends. Is this correct? Are there any other dangers of using such global variables for short-life-span programs? Thanks in advance ...
 
I think you are asking if the memory you alocatted with malloc is still "allocated" (aka memory leak) after the F77 program ends. In C, when a program ends all "allocated" memory is freed. I would assume that this is the case when your F77 program ends. However, it is good practise to call the
"free(pointertohumans)" C function when you are done using the memory.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top