Allocating memory of VL structures

This is a discussion on Allocating memory of VL structures within the C Programming forums, part of the General Programming Boards category; I'm trying to get a program working that has a very large structure group. The structure is of the form: ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    9

    Question Allocating memory of VL structures

    I'm trying to get a program working that has a very large structure group. The structure is of the form:

    Code:
    typedef struct {
    double x;
    double y;
    double z;
    } Vect;
    
    typedef struct {
    Vect vector1;
    Vect vector2;
    Vect vector3;
    //(6 doubles);
    } Param1;
    
    typedef struct {
    //(3 doubles);
    //(8 integers);
    } Param2;
    
    typedef struct {
    Param2 param2[256];
    double P3d[17];
    int        P3i[17];
    int        P3i1;
    int        P3i2;
    } Param3;
    
    typedef struct {
    double val[10000];
    } Vals;
    
    typedef struct {
    int   P4i;
    double P4d;
    } Param4;
    
    typedef struct {
    //(3 integers);
    //(6 doubles);
    Param4 param4;
    Vals      val1;
    Vals      val2;
    Vals      val3;
     } Param5;
    
    typedef struct {
    //(4 integers);
    //(3 doubles);
      Param4 param4;
      Param5 param5[256];
    } Param6;
    
    typedef struct {
      Param6 param6[64];
      Param1 param1;
      Param3 param3[64];
    } Param7;
    
    typedef struct {
      Param7 param7[3];
    } Structure;
    
    Structure struct;
    Whenever I build the program/subroutines (a combination of FORTRAN and C) everything claims it's fine. When I run it I immediately get a killed signal. When I strace:

    $ strace ./prog
    execve("/home/.../prog.run", ["/home/.../prog.run"], [/* 43 vars */]) = 0
    +++ killed by SIGKILL +++

    I'm pretty sure the problem is in allocating memory for my structure, but I can't figure out how to assign memory for this kind of structure system. Does anyone have any insight? Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Structure struct;
    Instead you could declare struct as a pointer and allocate memory dynamically for it:
    Code:
    Structure *struct;
    
    struct = malloc(sizeof *struct);
    if (struct == NULL)
    {
    	printf("Could not allocate memory for struct.\n");
    	return EXIT_FAILURE;
    }

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    I modified it the way you sugested (along with all of the instances it is called) and the same message appears.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    Quote Originally Posted by darsatdusk View Post
    Whenever I build the program/subroutines (a combination of FORTRAN and C) everything claims it's fine. When I run it I immediately get a killed signal. When I strace:

    $ strace ./prog
    execve("/home/.../prog.run", ["/home/.../prog.run"], [/* 43 vars */]) = 0
    +++ killed by SIGKILL +++

    I'm pretty sure the problem is in allocating memory for my structure, but I can't figure out how to assign memory for this kind of structure system. Does anyone have any insight? Thanks.
    You mentioned that your code is a combination of FORTRAN and C. How are these code interacting with each other? Because program termination because of memory allocation issues is generally SIGSEGV and not SIGKILL. Has the program ran before this structure was added in, or did it even run before you recompiled it?

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Fortran calling C functions (sample):
    in C)
    Code:
    void func_(void){}
    in F)
    Code:
    call func
    C calling Fortran functions (sample):
    in F)
    Code:
    subroutine func
    in C)
    Code:
    extern void func2_(void);
            ***
            func2_(); (inside subroutine)
    This is the way the original coding is written and how they describe it on other forums. That part works.

    The modifications I have made have worked in one iteration, long before I went into major detail work. I started by working with only FORTRAN to create arrays of array(10000,12,256,64,3) which did the same thing (killed by SIGKILL). I thought to then use C, thinking this would solve the size poblem, as well as using the structure to make things more legible. Now I'm really stuck.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    17
    It looks to me like the problem is not with whose calling what. Because at the machine level it's all the same. Without having some sample of how you're using the structure it's hard to tell. With computers these days, and the amount of memory available enough memory should (keyword SHOULD) be available to allocate it. However, it's possible that FORTRAN and C are seeing the data structure in different ways that effectively gets them all confused. See if it's something like between 2 integers it's easy, assuming that C and FORTRAN both implement C as a 32-bit block. However, once you start mixing between languages, a structure in 1 language could be totally different in another. This is where object brokers come into play. You might have to do this yourself to be able to translate the structure between FORTRAN and C.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    I've double-checked that the sizes correlate between the two programs. Everything is fine there.

    The structure only shows up in C coding. Mostly, I'm just assigning values from FORTRAN in the manner:
    Code:
    call dbsh_assign_mirs(s,m,t)
    Where the subroutine is a C function that would use the structure:
    Code:
    void dbsh_assign_mirs_(int sh, int mir, double* t0s)
    {
      dbshevnt->SH[sh].mirval[mir].mirror  = mir;
      dbshevnt->SH[sh].mirval[mir].t0shift = *t0s;
    
      return;
    }
    It's only handled between the two in one specific subroutine that has all of the appropriate conversions of terms:
    Code:
    void convert_terms(int m)
    {
      int i, t;
    
      dbsh_geas_.rini_dbsh[0] = dbshevnt->SH[1].shwval.rini.x;
      dbsh_geas_.rini_dbsh[1] = dbshevnt->SH[1].shwval.rini.y;
      dbsh_geas_.rini_dbsh[2] = dbshevnt->SH[1].shwval.rini.z;
    
    ***
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would probably pare down the size of the struct step by step, and if that's the problem, it should start working at some point. Alternately use a debugger to determine at what point it stops running, or add print statements to accomplish the same.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    I've tried the debugging, but it can't even reach step 1 since, I believe, it's trying to allocate too much memory from the start. I think I'm going to have to revert back to a previous version of my program where I know my modifications were working before I added in the structure/array. Thanks for the help, everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  2. dynamically allocating memory
    By rs07 in forum C Programming
    Replies: 6
    Last Post: 09-14-2008, 03:26 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. Shared memory in Linux: B-TREE of structures
    By zahid in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21