Clearing a string (char array)

Programming, for all ages and all languages.
Post Reply
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Clearing a string (char array)

Post by piranha »

Uh, is there a better way to clear a string then just:

Code: Select all

for(i=0;i<128;i++) str[i] = '\0';
Because it gets old typing that......-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

memset?
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Clearing a string (char array)

Post by Brendan »

Hi,
piranha wrote:Uh, is there a better way to clear a string then just:

Code: Select all

for(i=0;i<128;i++) str[i] = '\0';
Because it gets old typing that......-JL
Do you need to clear the string? Often, for ASCIIZ strings it's enough to do:

Code: Select all

str[0] = '\0';
Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

I recommend using aligned buffers for 4 bytes in each iteration using any general purpose register (GPR) or using 8 bytes per iteration using MMX. Putting 1 byte per iteration is really not a good idea in my opinion.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Which means, memset. ;)
Every good solution is obvious once you've found it.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Thanks, it did work with just str[0] = '\0'

Cool

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Post Reply