corrupt function parameters question

This is a discussion on corrupt function parameters question within the C Programming forums, part of the General Programming Boards category; I have two large arrays of structures I could not declare as arrays (array too big error) but could allocate ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    corrupt function parameters question

    I have two large arrays of structures I could not declare as arrays (array too big error) but could allocate dynamically

    n[138] and p[317]
    and am trying to pass them as parameters to a function.

    void create_reports (n_t n[], p_t p[]);

    int main(void)
    {

    n_t *n;
    p_t *p;

    n = calloc(138,sizeof(n_t));
    p = calloc(317,sizeof(p_t));
    ...
    create_reports(n,p);

    return(0);
    }

    The program works fine with smaller arrays but seems to pass the function garbled values with the full sized arrays. Am I running out of memory? Is it being passed by value and not address?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I have two large arrays of structures I could not declare as
    > arrays (array too big error) but could allocate dynamically

    Get rid of your old 16-Bit compiler.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Sayeh
    Guest
    >arrays (array too big error) but could allocate dynamically


    How do you know? I don't see any error checking to see if the calloc() call was successful. In which case you're writing all over your RAM!

    ...
    hth

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Depends what n_p and n_t types are - if they're large structures, could be a problem

    n[138] and p[317]
    and am trying to pass them as parameters to a function.

    void create_reports (n_t n[], p_t p[]);

    Code:
    // this is on the stack, which is a real problem for old compilers on old OS, with very small stacks
    int main(void)  { 
      n_t n[138];
      p_t p[317];
      // blah
    }
    
    // these are not on the stack, but in the normal data segment, but their scope is still within main
    int main(void)  { 
      static n_t n[138];
      static p_t p[317];
      // blah
    }
    Your prototype for your solution is
        void create_reports (n_t *n, p_t *p);

    And the call is
        create_reports(n,p);

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    18
    Forgot to include the error checking, but it is in place:

    if (!p)
    exit(1);
    if (!n)
    exit(1);

    Will try them as static, but I thought static variables could not be changed?

    I appreciate the responses.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but I thought static variables could not be changed?
    Static variables that are local in scope retain their value when they go out of scope. Constant variables are variables that can't be changed (technically they can, but you'd pretty much have to be deliberately going around it, so const protects against accidental modification). So you can change the value of a local static variable.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

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