Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dereference

Reply
Thread Tools

dereference

 
 
Barry Schwarz
Guest
Posts: n/a
 
      08-26-2012
On Sat, 25 Aug 2012 16:40:13 -0700, Keith Thompson <(E-Mail Removed)>
wrote:

>Barry Schwarz <(E-Mail Removed)> writes:
>> On Sat, 25 Aug 2012 09:46:50 -0400, "Bill Cunningham"
>> <(E-Mail Removed)> wrote:
>>>Barry Schwarz wrote:
>>>> There are no pointer variables in your code. Go back and start again
>>>> from scratch while writing "Arrays and pointers are different objects"
>>>> 1000 times.
>>>
>>> This is something I know because its been drilled into me. But the thing
>>>is I haven't yet caught onto the real meaning yet. For example I know of a

>>
>> If you don't understand the meaning, how can you possibly claim to
>> know something? If you really knew arrays and pointers were
>> different, you would never attempt to assign a value to an array name
>> as you did in this code from your 24 Aug message:
>>
>>>>char p[]="hello world\n";
>>>>p==NULL;

>
>That's not an assignment, it's a comparison. The statement
>
> p==NULL;
>
>is perfectly legal, though it's utterly useless.


Absolutely true on both counts. Another reason why those of us with
bad eyes beg for judicial horizontal white space.

>
>(I don't know whether it was *meant* to be an assignment.)


Since Bill responded yes, I claim clairvoyance and responded to his
intent if not his text.

--
Remove del for email
 
Reply With Quote
 
 
 
 
Adrian Ratnapala
Guest
Posts: n/a
 
      08-26-2012


> > int cbc_crypt(char *key, char *data, unsigned datalen,
> > unsigned mode, char *ivec)
> >
> > Notice it is `char *`, not `const char *`. This is a hint that the

> [snip]
> Would the man page author have done us all a big favor and instead of
> calling all the char *'s in this example char key[] for example? Or is this
> just me and something I didn't get at first.


Good question, but the answer is: NO! The author got it right.

For example you could do:

char *key = malloc(length_of_the_goddamn_key);

cbc_crypt(key, ...);
 
Reply With Quote
 
 
 
 
Adrian Ratnapala
Guest
Posts: n/a
 
      08-26-2012
On Sunday, 26 August 2012 23:40:13 UTC+2, Adrian Ratnapala wrote:
> > > int cbc_crypt(char *key, char *data, unsigned datalen,
> > > unsigned mode, char *ivec)

>
> > Would the man page author have done us all a big favor and instead of
> > calling all the char *'s in this example char key[] for example? Or is

[snip]
> Good question, but the answer is: NO! The author got it right.


I should add, that although this all look like a big subtlety, it isn't. The thing in the man page was probably created by just cut-and-pasting whatever was in the header file. And that's a good thing -- it gives the language-lawyers a canonical source to work with.

And here in this thread, we are language-lawyers. I don't even know if there would be any pracitcal difference had the function prototype said `char key[]`; the only reason I care is that an unusually sharp newbie is asking pointed questions.
 
Reply With Quote
 
Adrian Ratnapala
Guest
Posts: n/a
 
      08-26-2012
On Saturday, 25 August 2012 22:39:22 UTC+2, Ike Naar wrote:

>
> > char key[] = "some stuff";
> > In this case the compiler allocates ten bytes on the stack,

>
> Eleven bytes, and not necessarily on the stack.


Correct, if you will beleive it, I remembered the '\0' but forgot the space.

Damn!

Also you are right, it is not necessarily on the stack, it's just somewhere that is safe against multithreading and re-entrant calls. Like all the other local ****ing variables.
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      08-27-2012
On 25 aug, 03:53, Andrew Cooper <(E-Mail Removed)> wrote:
> On 24/08/2012 18:31, Kenneth Brody wrote:
>
>
>
>
>
> > On 8/24/2012 6:00 AM, Hans Vlems wrote:
> >> On 23 aug, 03:26, "Bill Cunningham" <(E-Mail Removed)> wrote:
> >>> Keith Thompson wrote:
> >>>> No, it isn't the same thing at all, and the difference is *exactly*
> >>>> what we've been discussing.

>
> >>> Ok I read Kenneth Brody's post. I guess the difference is the pointer
> >>> and
> >>> what it's pointing at.

>
> >> Imagine two drawers in a cabinet. The right drawer contains a one
> >> dollar note. The left drawer contains a note that says "one dollar
> >> note in right drawer".
> >> The content of the left drawer points to the right drawer. Only the
> >> right drawer holds the real thing (the money).

>
> > Using GC, if I remove the note from the left drawer, what happens to the
> > money in the right drawer?

>
> Depends.
>
> Is there another draw (the centre one perhaps) with a note saying "one
> dollar in the right drawer"?
>
> If yes, then nothing
> If no, then the dollar turns into free space.
>
> ~Andrew- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


No, the dollar remains inside its own (right) drawer. It is not
affected by the operation.
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      08-27-2012
On 25 aug, 05:42, Adrian Ratnapala <(E-Mail Removed)> wrote:
> On Saturday, 25 August 2012 01:36:12 UTC+2, Bill Cunningham *wrote:
> > Kenneth Brody wrote:

>
> > > Using GC, if I remove the note from the left drawer, what happens to

>
> > > the money in the right drawer?

>
> > the money in the right drawer?

>
> I think the "Nothing" answer was better. *I think the right drawer doesn't have a note saying "one dollar in left drawer". *Rather it has a notesaying "Money, left drawer", where it is previously understood that the *right* drawer contains a treasure map to finding, say, auntie Rohini's life savings.
>
> * *typedef int Dollars;
> * *Dollars rohinis_stash = 1;
> * *Dollars *treasure_map = &rohinis_stash;
>
> * *rohinis_stash = 0;
> * *assert(treasure_map == &rohinis_stash); *// passes
> * *assert(*treasure_map == 0); // passes
>
> The pointer in the right draw is unchanged, and still correct, if Rohini empties out the here stash. *What was a pointer to $1 is now a pointer to$0. *A NULL pointer would be if Rohini keeps here money where it is, butreplaces the pointer with a not saying "Money, up your nether hole".
>
> * *typedef int Dollars;
> * *Dollars rohinis_stash = 1;
> * *Dollars *treasure_map = &rohinis_stash;
>
> * *treasure_map = NULL;
> * *assert(*treasure_map == 1); hopefully segfaults


The right hand drawer is not a pointer. It is simply a location
holding a value.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-27-2012
On Aug 27, 8:11*am, Hans Vlems <(E-Mail Removed)> wrote:
> On 25 aug, 05:42, Adrian Ratnapala <(E-Mail Removed)> wrote:
>
>
>
>
>
>
>
>
>
> > On Saturday, 25 August 2012 01:36:12 UTC+2, Bill Cunningham *wrote:
> > > Kenneth Brody wrote:

>
> > > > Using GC, if I remove the note from the left drawer, what happens to

>
> > > > the money in the right drawer?

>
> > > the money in the right drawer?

>
> > I think the "Nothing" answer was better. *I think the right drawer doesn't have a note saying "one dollar in left drawer". *Rather it has a note saying "Money, left drawer", where it is previously understood that the *right* drawer contains a treasure map to finding, say, auntie Rohini's life savings.


<snip>

> The right hand drawer is not a pointer. It is simply a location
> holding a value.


sounds like a distinction without a difference
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-27-2012
On Aug 25, 5:44*am, Barry Schwarz <(E-Mail Removed)> wrote:
> On Fri, 24 Aug 2012 19:36:12 -0400, "Bill Cunningham"
>
> <(E-Mail Removed)> wrote:
> >Kenneth Brody wrote:
> >> Using GC, if I remove the note from the left drawer, what happens to
> >> the money in the right drawer?

>
> >That sounds like a null pointer.
> >char p[]="hello world\n";
> >p==NULL;


what?

> There are no pointer variables in your code. *Go back and start again
> from scratch while writing "Arrays and pointers are different objects"
> 1000 times.
>
> --
> Remove del for email


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-27-2012
On Aug 25, 11:36*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Adrian Ratnapala wrote:
> > On Saturday, 25 August 2012 15:46:50 UTC+2, Bill Cunningham *wrote:
> > [regarding "array" is not a pointer"]
> >> * * This is something I know because its been drilled into me. But
> >> the thing is I haven't yet caught onto the real meaning yet. For
> >> example I know of a funtion whose prototype is char * but if you put
> >> a string into it the function doesn't work correctly it's called
> >> cbc_crypt(). You have to pass char key[]="string"; to char *key, the
> >> first parameter. Not quite sure why yet but that's the way it works.

>
> > That's a really nice example; (warning) I didn't know about
> > cbc_crypt()
> > until just now, but I will try to explain it anyway. *My man page
> > says:

>
> > * * int cbc_crypt(char *key, char *data, unsigned datalen,
> > * * * * * * * * * unsigned mode, char *ivec);

>
> > Notice it is `char *`, not `const char *`. *This is a hint that the
> > function will be read, and *write* into the buffer. *So one way or
> > another, `key` must point to writeable memory.

>
> [snip]
>
> * * Would the man page author have done us all a big favor and instead of
> calling all the char *'s in this example char key[] for example? Or is this
> just me and something I didn't get at first.


presumably the author assumed his audience were C programmers
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-27-2012
On Aug 26, 10:46*pm, Adrian Ratnapala <(E-Mail Removed)>
wrote:
> On Sunday, 26 August 2012 23:40:13 UTC+2, Adrian Ratnapala *wrote:
> > > > * * int cbc_crypt(char *key, char *data, unsigned datalen,
> > > > * * * * * * * * * unsigned mode, char *ivec)

>
> > > * * Would the man page author have done us all a big favor and instead of
> > > calling all the char *'s in this example char key[] for example? Or is

> [snip]
> > Good question, but the answer is: NO! *The author got it right.

>
> I should add, that although this all look like a big subtlety, it isn't. *The thing in the man page was probably created by just cut-and-pasting whatever was in the header file. *And that's a good thing -- it gives the language-lawyers a canonical source to work with.
>
> And here in this thread, we are language-lawyers. *I don't even know ifthere would be any pracitcal difference had the function prototype said `char key[]`;


no, none.

> the only reason I care is that an unusually sharp newbie is asking pointed questions.


Bill? newbie? sharp newbie?!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dereference Adaptor Matthias Kaeppler C++ 3 02-28-2005 05:42 PM
How does dereference operator overloading really work? Joe Seigh C++ 18 09-22-2003 05:42 PM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM
Re: how to properly dereference STL list item Howard C++ 0 07-01-2003 05:46 PM
Re: how to properly dereference STL list item Jakob Bieling C++ 0 07-01-2003 05:45 PM



Advertisments