String question

Discuss PocketC for CE (including Desktop Edition)

Postby Glasswalker on Tue Sep 17, 2002 9:32 pm

Hey again...
question, I have a number of structs that are for a custom database... each struct represents a record, and each record contains a couple dozen strings, and a couple dozen ints... about 50 variables in all, (almost 2 3rds are strings)

the only thing that will work is strings, as it has to be names, and functions and such that cannot be represented any other way easily...

the problem is I need to create an array of about 250 of these units in memory to load in the array and do work with it, then I write it back to file...

But I am getting out of memory errors...

Now I am under the understanding that PocketC Strings define as 32K strings... that is HUGE! (I need 32K for my fileI/O routines) but I only need like 40 or 50 bytes for some of these strings...

Is there a way to define a string of a fixed size in PocketC as you can in C++?

That way the array would not be massive...

Right now if they are 32K strings and I have 30 of them, that makes 900K per record... then 250 records makes almost 200 megs! That is silly :)

I need to be able to define a small string...

I would appreciate any help...
Posts: 47
Joined: Wed Mar 21, 2001 12:18 am
Location: Canada

Postby guy on Wed Sep 18, 2002 8:20 am

PocketC strings are not created at 32K!

A PocketC variable is represented by a C++ class that contains a union of basic variable types (int, wchar_t and double) followed by a member of class ustring. This is a unicode version of a string class. The ustring class contains a reference to a reference counted string Srep struct. The Srep struct contains a pointer to the actual string text in memory, the length of the string and a reference count.

The memory used by the actual text of the string is allocated and deallocated using the new and delete operators, they impose some overhead for memory management (stored before the start of the allocated memory). They also allocate in convenient sizes of bytes rather than exactly what is needed for the thing that was allocated (this assists in cleaning up).

So the overhead for a string is about 24 bytes plus the memory management overhead.

Any non-string variable will take up 12 bytes. An instantiation of a PocketC struct will take up 12 bytes per field, plus the overhead for any strings that are assigned to fields in the struct.

An array will occupy the storage taken up by its PocketC variable elements, plus 12 bytes for the array variable.

If you want to reduce the storage that your variables take, the easiest thing to do is to store them as real variables in real structs in real memory instead of PocketC variables in PocketC structs in PocketC memory. You can do this by creating one PocketC struct for manipulating the current value with PocketC, and copy the entry that you are working on out of real memory to change it, then putting it back into real memory afterwards.

The CE API interface library has functions for moving PocketC variables and structs into and out of real CE allocated memory. You could do what you want like this:

<pre id="code"><font face="courier" size="2" id="code">

#include "/pocketc/winbase.h"

// PocketC version of struct

struct EXAMPLE {
int i ;
string s ;
string t ;
} ;

// Real layout of struct. First string is 24 wchars, second is 32 (say). See CEAPI readme.

#define DSC_EXAMPLE "ic[24]c[32]"


struct EXAMPLE rE ;
int iSize ;

iSize = sizeofx(DSC_EXAMPLE) ; // Get real memory size used by real struct

lprE = LocalAlloc(LMEM_FIXED, 250 * iSize) ; // Allocate memory for 250 of them

// Setup an entry and save it in element 5 of the real memory array

rE.i = 14 ;
rE.s = "Aardvark" ;
rE.t = "Supervisor" ;

memwritestruct(&rE, DSC_EXAMPLE, lprE + (5 * iSize)) ;

// Get hold of a copy of element 22 of the array

memreadstruct(lprE + (22 * iSize), &rE, DSC_EXAMPLE) ;

putsl("Int value of element 22 is " + rE.i) ;


// Return the allocated memory to Windows CE

LocalFree(lprE) ;

</font id="code"></pre id="code">

That should cut down the memory used. You could also choose to allocate the memory from your own heap using HeapAlloc() and HeapFree() so that it doesn't interfere with PocketC. You'd need to create the heap first with HeapCreate() and remove it afterwards with HeapDestroy().

PocketC CE API interface:
PCForm and CE API forum:
PocketC CE API interface:
PCForm and CE API forum:
Posts: 879
Joined: Thu Dec 07, 2000 8:58 am
Location: United Kingdom

Return to PocketC for CE

Who is online

Users browsing this forum: No registered users and 4 guests