CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2004
    Posts
    19

    Duplicating a struct

    I have a this following struct in my program.
    -------------------------------------------
    struct mytext{
    char text[1024]; //will store each individual lines of the textfile
    char unfield[1024];
    char field[1024]; //will store each column from each line
    char reversed[1024];
    };
    -------------------------------------------

    I created in a method called work()

    mytext st[1024];

    and I have another method called void sort()
    and I want to pass st from work to sort(st) but when I tried to call the data stored in the struct from sort() method, i cant reference the data.

    Could someone help me with how i can pass a struct from one method to another without erasing any data in the stuct??


    Thanks

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Duplicating a struct

    How about showing your code for work() and sort()? It helps us to better understand your problem.

  3. #3
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147

    Re: Duplicating a struct

    It sounds like you are passing by value instead of by reference...

    void sort(mytext x[])
    {

    }

    This will try to duplicate all of your 1024 structures on the stack (bad idea)

    Instead, you should pass by reference...

    void sort(mytext &x[])
    {

    }

    Hope this helps,

    - Nigel

  4. #4
    Join Date
    Aug 2004
    Location
    INDIA
    Posts
    260

    Re: Duplicating a struct

    Static Declration Example.

    Code:
    #define NUMBER_TEMS 10
    
    // strucure array declaration
    mytext st[NUMBER_TEMS ];
    
    //sort function call 
    sort (st);
    
    // sort function definition.
    
    void sort (mytext *st)
        {
    
        // for example to access all items in structure array.     
        for (long ii =0; ii < NUMBER_TEMS ; ii++)
             {
             strcpy (st[ii].text, "Hai");
             }
    
        }
    This example will give crash when number of array elements are more.

    In your case.
    For an item = 1024*4 = 4096 KB
    For 1024 item = 1024 * 4 = 4096 KB Around 4MB.

    For 1024 items, Stack needs to allocate around 4MB. Sometimes application will get crash when you try to allocate huge amount of memory in stack. When you want more array elements dont use static array decalaration. Use Dynamic memory allocation using Malloc and Free.


    Dynamic Memory Allocation Example

    Code:
       
    //   Allocate memory for 1024 items
        mytext *st =(mytext*) malloc (sizeof (mytext) *1024);
    
    // call sort 
        if (NULL != st) 
          {
          sort(st);
    
         // free used memory
          free (st);
          st = NULL;
          }
    Last edited by santoct2002; February 1st, 2005 at 03:15 AM.
    If you feel this post is useful,
    Rate this Post by clicking right top corner (Rate this Post)
    Santhosh

    ***Add strength to your country - Interesting Poll
    ***

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Duplicating a struct

    Code:
    struct mytext{
    char text[1024]; //will store each individual lines of the textfile
    char unfield[1024];
    char field[1024]; //will store each column from each line
    char reversed[1024];
    };
    There is a lot of memory allocated for this structure. Are those fields really needing 1024 bytes? You should consider using dynamic allocated memory.
    Code:
    struct mytext{
    char *text; //will store each individual lines of the textfile
    char *unfield;
    char *field; //will store each column from each line
    char *reversed;
    };
    Or use STL:
    Code:
    struct mytext{
    std::string text; //will store each individual lines of the textfile
    std::string unfield;
    std::string field; //will store each column from each line
    std::string reversed;
    };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Duplicating a struct

    Quote Originally Posted by santoct2002
    For 1024 items, Stack needs to allocate around 4MB. Sometimes application will get crash when you try to allocate huge amount of memory in stack. When you want more array elements dont use static array decalaration. Use Dynamic memory allocation using Malloc and Free.
    Or, even better: using new[] and delete[].
    Old Unix programmers never die, they just mv to /dev/null

  7. #7
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Duplicating a struct

    Quote Originally Posted by NigelQ
    It sounds like you are passing by value instead of by reference...

    void sort(mytext x[])
    {

    }

    This will try to duplicate all of your 1024 structures on the stack (bad idea)

    Instead, you should pass by reference...

    void sort(mytext &x[])
    {

    }

    Hope this helps,

    - Nigel
    Actually, this is incorrect. When you pass an array to a function, only a pointer to the first element is passed (in effect, arrays are always "pass-by-reference").

    Code:
    Or, even better: using new[] and delete[].
    Or yet, even better, use a vector. (if the poster is using C++)
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured