Thread: Passing data

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Passing data

    Hello,

    Earlier today, I asked about how to allocate a big memory location. Thanks all! Now I have another problem, please help.
    I have a short program like this:

    int main(void) {
    CallFunction (datapassing1);
    CallFunction (datapassing2);
    .
    .
    return 0;
    }

    Where datapassing1 and datapassing2 are short programs that need a memory space of about 4kb to do some cratch work. I don't know how to pass the data, allocate the memory location for this datapassing. Anyone has any idea, please give me some advice. I really appreciate.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Passing data

    Originally posted by vnrabbit
    Where datapassing1 and datapassing2 are short programs that need a memory space of about 4kb to do some cratch work
    Do you mean functions?
    The fastest way would be to send the pointer to the data (only pass 4 bytes instead of 4k, or whatever).
    Code:
    void datapassing1(int* Data)
    {
       ... //Do whatever
    }
    
    void datapassing2(int* Data)
    {
       ... //Do whatever
    }
    
    int main(void)
    {
       int* Data; //Assume your data are integers
       Data = new int[SizeOfData]; //Allocate memory
       if(Data == NULL) return 0; //Check if allocation was successful
    
       ...
    
       CallFunction (datapassing1(Data));
       CallFunction (datapassing2(Data));
    
       delete[] Data; //Deallocate the memory
       return 0; 
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Data = new int[SizeOfData]; //Allocate memory

    Slight correction. The above code is C++, not C. You'd want:

    Data = malloc( sizeof(int) * number_to_allocate ) );

    The same goes for the 'delete' call.

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

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thanks guys.

    However, I can't change the datapassing1 and datapassing2 functions on the way I call them. Maybe I didnt explain my question clearly. When I call in the CallFunction() in Main, I then need to allocate a memory location about 4kb for datapassing1 and datapassing2 to do their stuff on this 4kb memory location.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void myFunction( void * data )
    {
        /* do something */
    }
    
    int main( void )
    {
        /*
            Allocate 4 KB of data.
        */
        void *dataBlock;
        dataBlock = malloc( 4096 );
    
        myFunction( dataBlock );
    
        return 0;
    }
    There's one way to allocate a 4KB block of data for use elsewhere. Naturally it's better to actually allocate specific types, but if you just want a solid 4K block of memory allocated for you, that'd do it.

    You could then dice that up or do whatever you intend with it.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Data member passing
    By wirefree101 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2006, 01:41 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing structure data to a function?
    By diddy02 in forum C Programming
    Replies: 8
    Last Post: 08-17-2002, 04:15 PM