|
-
February 1st, 2005, 01:09 AM
#1
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
-
February 1st, 2005, 01:39 AM
#2
Re: Duplicating a struct
How about showing your code for work() and sort()? It helps us to better understand your problem.
-
February 1st, 2005, 01:47 AM
#3
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
-
February 1st, 2005, 01:51 AM
#4
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.
-
February 1st, 2005, 06:30 AM
#5
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;
};
-
February 1st, 2005, 12:09 PM
#6
Re: Duplicating a struct
 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
-
February 1st, 2005, 04:48 PM
#7
Re: Duplicating a struct
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|