MAMEWorld >> Programming
View all threads Index   Flat Mode Flat  

Mario Figueiredo
MAME Fan
Reged: 10/17/08
Posts: 66
Send PM
Re: C coders, what method do you use to clear a character array?
11/23/08 04:19 AM


> You can't deallocate/allocate arrays & sizeof only does what you want for arrays &
> not pointers. If CharArray was a pointer then sizeof(CharArray) would return the size
> of the pointer ( sizeof(*CharArray) would return the size of what the pointer points
> at ). To use pointers you might want to use calloc ( malloc doesn't guarantee that
> the memory will be cleared at all ) & then if you later wanted to clear it you need
> to keep the size that you allocated and pass that into memset instead.

Meanwhile, one more thing come to mind...

If you are using a char array as a storage area that needs to be repopulated with new data try instead to follow the maxim that says that the fastest way to do something is not doing it at all. On this case consider building your array as such:


Code:

struct my_data
{
int count;
char data[100];
};



Repopulating the array is then a matter of updating my_data.count and doing your business on my_data.data, overwriting needed elements and leaving be any unused ones.

This answers any issues regarding performance. But comes at the cost of memory efficiency.

If this is a problem, or the struct doesn't somehow fit into your plans(?), then memset is the best option indeed and you should fallback to smf's post.

Edited by Mario Figueiredo (11/23/08 04:20 AM)







Entire thread
Subject Posted by Posted on
* g*DELETED* *DELETED* italieAdministrator 11/10/08 02:49 AM
. * C coders, what method do you use to clear a character array? italieAdministrator  11/10/08 02:54 AM
. * Re: C coders, what method do you use to clear a character array? krick  11/10/08 05:44 AM
. * Re: C coders, what method do you use to clear a character array? Anonymous  11/19/08 11:59 AM
. * Re: C coders, what method do you use to clear a character array? Mario Figueiredo  11/23/08 04:19 AM
. * Re: C coders, what method do you use to clear a character array? Anonymous  12/30/08 12:10 PM

Extra information Permissions
Moderator:  Pi 
0 registered and 3 anonymous users are browsing this forum.
You cannot start new topics
You cannot reply to topics
HTML is enabled
UBBCode is enabled
Thread views: 3578