Click to See Complete Forum and Search --> : Duplicating a struct


jihaes
February 1st, 2005, 12:09 AM
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

Kheun
February 1st, 2005, 12:39 AM
How about showing your code for work() and sort()? It helps us to better understand your problem.

NigelQ
February 1st, 2005, 12:47 AM
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

santoct2002
February 1st, 2005, 12:51 AM
Static Declration Example.



#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



// 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;
}

cilu
February 1st, 2005, 05:30 AM
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.

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:

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;
};

HighCommander4
February 1st, 2005, 11:09 AM
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[].

cma
February 1st, 2005, 03:48 PM
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").

Or, even better: using new[] and delete[].
Or yet, even better, use a vector. (if the poster is using C++)