0

I'm going through something that should be simple in C but for some reason cant seem to get it to work.

Here are the structs:

    #define MAX_BRANCH 500
    #define MAX_BANK_CLIENTS 100000
    #define MAX_CLIENTS 10000


   typedef struct Client{
        char *pName;
        char *fName;
        int id;
        int branch;
        int AccountNum;
        int credit;
        double surplus;
        double IOU;
        double savings;
    }Client;

   typedef struct Branch{
        int BrnachNum;
        char *Name;
        int Accounts;
        double sumOfAll;
        double profit;
        int ActiveLoans;
        int Opened;
        int Closed;
        Client ClientList[MAX_CLIENTS];
    }Branch;

    typedef struct Bank{
        char *Name;
        int Branches;
        int Accounts;
        int ActiveLoans;
        double sumOfAll;
        double Profit;
        Branch BranchList[MAX_BRANCH];
    }Bank;




int main()
{
   Bank Discount;
   Discount.BranchList[0].Accounts = 1;

   return 0;
}

//--------------------------------------------

this simple placement of an integer value to an integer argument shows me a stack overflow or any other accesses to the inner fields and the char pointer will be assigned by strdup(the only memory allocation i can use).

And just keep in mind that I cant use memory allocation of any kind.

Second is, some one instructed me to set a static array of the struct. Something like

static Branch BranchList[500]

but how can I do the same for each of the branches?

tshepang
  • 12,111
  • 21
  • 91
  • 136
lddmonster
  • 63
  • 11
  • You really should tag this as homework... – Nathan Fellman Apr 09 '12 at 07:11
  • 1
    could u show a bit more code, specifically: You have a few pointers in your struct and you say that you are not allowed to use memory allocation so I was wondering how you setup what they point to? – AndersK Apr 09 '12 at 07:19
  • It is probably just an initialization problem. Initialize all your arrays with the default initializer `= { 0 }`, always. – Jens Gustedt Apr 09 '12 at 08:32

2 Answers2

1

The problem seems to be that you're using structs before they've been declared. You might want to define them in the reverse order.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
  • But why should that result in a SO? His program would in fact would trigger a compilation error. – Pavan Manjunath Apr 09 '12 at 07:14
  • Because of the way arrays are implemented in C. In C the arrays are automatically allocated as contiguous areas. You're asking the compiler to allocate `MAX_BRANCH` `Branch`es in the `Bank` struct, but at that point in the file, `Branch` isn't defined yet, so the compiler doesn't know how much space to allocate for it. The same goes for `Client` in `Branch`. By the way, I may be wrong. Try changing the order of the declarations and see if it helps. If it doesn't, let me know so that I can remove this answer. – Nathan Fellman Apr 09 '12 at 07:19
  • did that, i just copy'ed it to site in reverse. and just to let you know the structs are placed in diffrent header files. – lddmonster Apr 09 '12 at 07:20
0

First, change the order of the structs in order to do away with compilation errors. And then, coming to your problem of stack overflow, you are really using HUGE arrays and if you have these inside a function, then you just declare one variable of each struct you should get a stack overflow in all probability.

Converting them to static, might save you as you would be pushing them onto the data section rather than the stack. But this depends on the data section allocated to your process which in turn depends on the system ( OS ) you are running on.

And once you do,

 static Branch BranchList[500];

you are allocating space for all branches, so where is the question of each branch?

As you are ruling out malloc and if static isn't solving your problem, try increasing stack size using setrlimit

Community
  • 1
  • 1
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • 1
    I can't imagine an entry-level assignment which rules out dynamic allocation requiring the use of `setrlimit` :-) – Nathan Fellman Apr 09 '12 at 07:38
  • ha ha :) True, but the OP is insistent on that. – Pavan Manjunath Apr 09 '12 at 07:39
  • doing static Branch BranchList[500] gives me an array of Branch Structs now i need that each block in the array to have an array of Client Structs and thats my question how do i do that?! – lddmonster Apr 09 '12 at 12:34
  • Your code shows no runtime error on [codepad](http://codepad.org/Nfb89ew5). So as speculated, your development environment might not be supporting a big stack. Whats your development enviroment? – Pavan Manjunath Apr 09 '12 at 12:38
  • i use visual studio and eclipse. the deal is that im required to use eclipse i was just trying to check it on a diffrent platform. than how can i use the static arrays?! – lddmonster Apr 09 '12 at 12:54
  • thanks every body the problem was that the structs were to big so i just had to set the Bank as a static global argument. – lddmonster Apr 09 '12 at 16:17