[1+1=2]

OneAndOneIs2

« What the GPL v3 isn'tForking GNU/Linux »

Sun, Sep 24, 2006

[Icon][Icon]My. . . brain. . .

• Post categories: Omni, Rant, Technology, My Life

I just got to the bit about pointers and arrays.

NNNnngg...

So. . . if you want strings, you either have a character array or a character pointer.

So you can define the string "foobar" by either:

char word[]="foobar"

or

char * word="foobar";

And then if you want to change "foobar" to "FooBar", you would use

word[0]='F';word[3]='B';

or

word="FooBar";

respectively. The array changes in-place, but the pointer drops "foobar" from memory and points to the newly-created string "FooBar" stored someplace else in memory.

You run into problems because functions like gets() can't distinguish between arrays and pointers, so always try to change in-place. So if you use gets(word) and the user types "Foooobaaaarrrr", the array would ignore everything that was too long to fit, but the pointer would accept the whole lot, potentially over-writing a piece of memory in use by some other function and causing problems.

So you either have to use arrays, which have fixed lengths and have to be changed character-by-character; pointers which can be any size and can be changed in one go, but you can't use functions like gets(); or a pointer to an array, which can be changed all in one go but can only be as long as the array.

Is that about right?

3 comments

Vincent Povirk
Comment from: Vincent Povirk [Visitor]
First of all, you should never use gets. Let me quote from the man page for gets.

"Never use gets()."

Arrays do not have any more protection from buffer overruns than pointers do. If you try to write outside the bounds of an array, the write will work, and it'll probably write over something important in the stack (like the address in the calling function where the current function will return).

In the pointer example, I believe what you are actually doing is changing the target address of the pointer. Both "foobar" and "FooBar" will end up in your executable somewhere, and they will be mapped to some read-only section of memory when your program is loaded. They exist as long as your program is running; nothing you do under normal circumstances creates or destroys them. When you assign to the pointer, you are changing the target address of your pointer to the address of one of those strings.

In most cases, when you use pointers you need to be very aware of how much memory you have allocated, how it will be deallocated, and how you can make sure it will last as long as you need it. A lot of weird bugs and memory leaks come from messing up this sort of thing, and other languages (like java or python) are considered "safer" because they are immune to this problem.
26/09/06 @ 07:40
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Interesting, I hadn't realized there were man pages for functions. But I've read it now, and it does indeed have that quote in it. Bizzare :o)

At the moment, I'm making my way through the beginner's book of C - it's explaining the language fairly well (tho I'm not impressed by the intro. to arrays and pointers, I must say) but it's not in any way getting into technical matters yet.

I've got "The C programming language" sitting on my table waiting for me to start on next, and I'm making my way through the "Learning GNU C" online book now & then as well.

Hopefully once I've made it through all of those, I'll know enough to avoid memory leaks & buffer overruns.

Cheers for the comment!
26/09/06 @ 10:26
Spudd86
Comment from: Spudd86 [Visitor]
I'm fairly certain that:

char foo[] = "foobar";
foo = "Foobar";


does not chage the array in place, if it works at all it will change where the array is pointing to (arrays and pointers are almost totaly interchangable)
06/11/06 @ 20:15

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
 

[icon] Blogroll

[icon] Creative Hedgehog
This week has been learning about what a Systems Analyst is, how to think about systems, and to be very aware of gorillas. Gorillas? There is a problem with simply asking how the company wants you to solve their problem, or even what they think the problem is. It's called situational blindness, and it [...][Link to post]04/03/10 - FIT2001 week 1: beware gorillas!

[icon] Hari's corner
How do you awaken a sleepy lawyer in the mornings?[Link to post]07/03/10 - The Legalease - Episode 10

[icon] Place of Stuff
Genesis 19 is a fun little tale, full of reversals and surprises.  It begins with two angels going to visit Lot in Sodom.  Lot welcomes them in and offers a good foot washing and feeding session.  As is polite and in custom with receiving visitors.  Especially angels.  Shortly after they arrive, the local men turn [...]

[Link to post]
27/02/10 - The Bible ? Sod ?Em

[icon] Advice From a Single Girl
So I'm kind of in an odd space these days.Still sort of smarting from stuff from a month ago and alternating between wanting to let it go and move on and giving myself time to respect how I'm feeling about it and, well, still being hurt.But I'm also doing pretty well in a lot of ways, I'm feeling good physically and enjoying hanging out with friends and so things are good in some ways too.But [Link to post]31/12/69 - Ah Me

[icon] Nation
&#160; This was possibly the most ridiculous show I have seen in a long time and I can get Sky 1 I know ridiculous. It could be summed up in three sentences Do you know what's in your cereal? Want to? Read the label. Instead it went on for a hour about how evil the cereal [...][Link to post]27/10/09 - Dispatches ? do you know what?s in your breakfast? (warning...

Blogroll generated by MagpieRSS

[Links][icon] My links

[Icon][Icon] Dominic can drive for another year! My little Fiesta survived its ordeal by MOT :).
05/03/10

[Icon][Icon] I last listened to:
The Offspring - She's Got Issues

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


March 2010
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

multi-blog