[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.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] http://t.co/9VG31Knw
01/02/12

[Icon][Icon] Facebook Syndication Error
04/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[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]


February 2012
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        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

multi-blog