Thread: big array

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    Question big array

    hello...
    anyone knew how to declare and operate a big multidimensional array by using virtual array (macros ?) ... ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's a "virtual array"? Do you mean a dynamicly allocated array? Or what? In either case, you'll always need to know the number of dimensions so you can dereference it correctly.

    You can fake a multidimensional array by simply creating a single dimension array and applying the appropriate math to access the correct 'cell' in the arrray.
    Code:
    char *array;
    int *dimensions, dim, count, size;
    
    dim = promptfordimensions( );
    dimensions = malloc( dim * sizeof(int) );
    for( count = 0; count < dim; count++ )
        dimensions[count] = promptforsizeofdim( );
    for( size = 1, count = 0; count < dim; count++ )
        size *= dimensions[count];
    array = malloc( size * sizeof( char ) );
    Vola!

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

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    Red face

    hello...
    Virtual array I meant is array that use virtual memory.......
    Where are codes of func. prototypes :
    promptfordimensions( );
    promptforsizeofdim( );

    Thank ya....

  4. #4
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    A virtual Array is not really an array. It's a list. They are similar but operations on the two happen very differently.

    an array is a static piece of contiguous memory that's allocated at program runtime. This is finite in size and cannot grow in size if needed.
    You access "members" of an array using the [ i ] ( or sub i ) iterator. This is not so with "dynamic" memory allocation.

    Sounds to me like you want an array of data that gets it's memory dynamically during the program execution. In C, we call this a linked list. For which you need to allocate memory on the fly and then manipulate some pointers to either insert or delete a piece of data.

    Before I go on any further, have you covered linked lists in your coursework? I don't want to go into a bigger explaination unless I am on target here.

    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by andu
    hello...
    Virtual array I meant is array that use virtual memory.......
    Yeah. My code should give you what you need then. Sound like exactly what you need.

    Originally posted by andu

    Where are codes of func. prototypes :
    promptfordimensions( );
    promptforsizeofdim( );
    You mean to tell me you don't know how to prompt the user for a number? And you're trying to use virtual memory? Woah there. One step at a time. First leanr how to prompt the user for a number and store it, then you can tackle something bigger like virtual memory.

    In other words: I figured any idiot should know how to prompt for a number, so there is no point in me writing that for you.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM