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

[icon] Blogroll

[icon] Creative Hedgehog
La parte A se refiere solamente a las dos novelas estudiadas. La parte A debe ser preparada después de leer la primera mitad de la novela y contestar las siguientes preguntas: ¿te está gustando la novela/película o no, y por qué? No me gusta la novela. Las personajes que puedes gustar son superficiales, o hacen [...][Link to post]06/08/10 - SPN3730 diario: Pascual Duarte parte A

[icon] Hari's corner
Why being bi-lingual has its advantages[Link to post]10/08/10 - Being bi-lingual has its advantages

[icon] Place of Stuff
Isn't this exciting? We're out of the tedium of Genesis (world created, man falls, many people live and die. Oh, and attempted forced buggery and a spot of incest). We're into Exodus now; the Bible has got going, that tricky first chapter is out of the way and the real action can start! When the [...]

[Link to post]
03/08/10 - The Bible ? On The Waterfront

[icon] Advice From a Single Girl

I was giddy and hopeful when I first met Cary and spent a brief amount of time with him.

The week after that I was happily high on the idea of what could be, the possibility of getting to know someone interesting and intriguing, the wide open potential of what could be.

And I wanted to tell my friends all about him and what had, and hadn't happened, but I also wanted to keep it to myself, sealed safely in the happy bubble that was floating inside me. So I talked to some close friends about him, told them he lived in Vancouver and they, meaning well, told me quite firmly that they would not allow me to go through another long distance relationship. That I shouldn't even consider it.

My bubble had been burst.

I was completely deflated. Hurt. Let down.

I talked to C-Dawg, a sad tinge to the story now that I'd been told it could. . . should never work out.

"Vancouver?" she said, her voice somewhere between amused and incredulous. "That's not long distance! Get serious. Go for it."

And I let my bubble maybe start to re-inflate. Cautiously. Maybe just a little.

Then I talked to my friend about Cary. She said good things.

Maybe there was reason to be hopefully optimistic. Maybe it was ok to be a little girly and dreamy over what-ifs.

I went for a walk with S. We had life to catch up on.

Life including Cary and the story that still makes me smile.

She encouraged me to get his email, which I did, and then she went home and tried to find out what she could about him.

See, I'm not on Facebook. (No, really.) But S is, and in the small world way that Facebook seems to work, she found that Cary and she had a mutual friend and so she looked him up for me. (The modern background check.)

You can sometimes tell a lot about a person by what they put on their Facebook, she cautioned me. Sometimes.

How old is he?

Me: I don't know.

Is he a smoker?

Me: Um, I don't know? (God, I hope not)

Could he maybe be a little bit immature?

Me: I don't know. I suppose.

Well, he seems like a good guy. Cute. Interesting. I'd say he was my type, you know. (We laugh, we already know we share similar excellent taste in men.)

"I say go for it." She says, "just be aware that he's human. Not perfect."

I don't want to hear it.

Don't want to know the reality of him.

Find myself running away from all the what might have been's towards it'll never work what what I thinking's.

It's all or nothing. Perfect or awful. It'll work or it'll be a disaster.

And I realize that my bubble, the one that's been growing and floating inside me will burst on its own, without anyone's help if I get too far into imagining just how great Cary is, how great we'd be together, how perfectly perfect it all will be.

I'm Icarus. My friends don't want me flying too close to the sun.

But I like the feeling.

I like the soaring giddiness of how utterly fantastic this thing I've found will be.

Every single time I meet someone I like that feeling.

And I ride it higher and higher until I'm flapping my bare arms, feathers fallen into the sea and the crash is coming, the relationship splintering and I'm left staring at the brokenness wondering how on earth I could have been so wrong again.

The extremes are familiar. Addictive perhaps.

But I'm trying to learn to ride in the middle.

Safer. A shorter distance to fall.

A smaller bubble to burst.

Expectations that can be met and exceeded.

A safe, yet joyful and giddy flight. Wings intact.
[Link to post]
03/09/10 - Icarus

[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 [...][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] Strange, how the only people who ever seem to complain that Linux sucks or doesn't work well are people who don't like using the CLI...
03/09/10

[Icon][Icon] Dominic tried to explain how circular references can cause a memory leak to a colleague this morning, and got told off for not working. Apparently, the analogy of a madman shooting anybody who isn't being pointed at by somebody else was NOT the boss-safe way to go..
01/09/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]


September 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      

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

multi-blog