LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-08-2007, 12:54 PM   #16
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled

Quote:
Originally Posted by ta0kira
Who knows what it will do to your program, but it can't be good.
I really got to thinking about what I said here. Here is what I really think would happen if you passed a char(*)[] and overwrote it in a function (removed 1 x * for simplicity.)

Suppose you allocate a char[10] on the stack. It would look something like this:
Code:
     0x10      0x1a
     |         |
... [~~~~~~~~~~~]
When passing this array to a function, you will just be passing the value of its location, which is 0x10. This value is just a number and has no location.

If you try to pass a pointer to the char[10], you have no address to take since 0x10 is just a number. In order for it to have a location, it needs to be converted to an lvalue by placing it on the stack:
Code:
     0x10      0x1a   0x1b
     |         |      |
... [~~~~~~~~~~~]    [0x10]
This is just a copy of the pointer, so the value 0x1b is passed to the function. When it's dereferenced in the assignment, you are left with an lvalue of the copy on the stack, so any change to the pointer will be lost when the function returns. It's therefore highly pointless to pass a pointer to a fixed array since it doesn't provide any functionality not available when just passing the array. The only thing char*(*)[] gives you over char*()[] is the requirement for an additional NULL check unless you literally have a char*(*)[] you want to change.
ta0kira

PS If you are wondering, taking char string[10] as an example, why the address of string isn't just passed since "it's a variable which holds a pointer to the array", here is my answer. string is not a variable, but a hard-wired value which represents an offset from the point on the stack where the function will return. If you created the variable char(*pointer)[10] = &string on the stack, however, pointer would be a valid value to pass and can legitimately be changed.

Last edited by ta0kira; 08-08-2007 at 01:05 PM.
 
Old 08-08-2007, 01:27 PM   #17
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Would I be out of line - or, perhaps, off topic -- to mention the Standard Template Library? True, it provides templates for use with C++, but using them can avoid lots of wheel reinvention.

From rubadub's question, I suspect that he's not too concerned about program size and speed, so a C++ solution instead of a C one might suit his needs. True, using a template is not a "nuts and bolts" learning experience, but it could be an "increased productivity" one . . .
 
Old 08-08-2007, 02:26 PM   #18
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by PTrenholme
Would I be out of line - or, perhaps, off topic -- to mention the Standard Template Library? True, it provides templates for use with C++, but using them can avoid lots of wheel reinvention.
If people never reinvented the wheel, the world would eventually forget why the wheel is round and would just make round things because that's what makes things roll I've reinvented several wheels even in C++.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About pointer signedness warnings isuck@linux Linux - Software 2 08-26-2006 08:18 PM
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 02:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 02:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 07:40 AM
How does one surpress incompatible pointer type warnings? tonyfreeman Programming 1 08-18-2004 07:06 PM


All times are GMT -5. The time now is 06:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration