Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array of C-strings
Index -> Programming, C -> C Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Anon




PostPosted: Mon Feb 25, 2008 11:32 pm   Post subject: Re: Array of C-strings

OneOffDriveByPoster @ Sun Feb 24, 2008 8:47 pm wrote:
Anon @ Sun Feb 24, 2008 11:37 am wrote:
code:
grid some_function();
The primary disadvantage is that you are really copying the array. Note that what you are doing is likely having an automatic array inside the function, and even if it is a static array, you are getting a temporary created (and associated copying) when you return the aggregate from the function. If you are doing assignment after, you copy yet again. If you expect a compiler to optimize this away then let us hope you have one with good Inter-Procedural Analysis. (It is likely that a compiler could take care of the last copy when you are assigning the result directly).



*slaps forehead*

Yes, I realize I'd have to return a pointer to the grid, as md has already said. Also, I'd guess that whilst passing in an array is the same as passing in a pointer, passing in a struct as a parameter by value is probably rather inefficient.

I'm curious though, is using a struct to contain the array, and then using a pointer to said struct a good way to allocate space for N-dimensional arrays? Because to allocate for each block iteratively would seem like a lot of needless overhead for N>2. If a more compact method exists, I'd love to hear about it!

Again, thanks for the helpful comments!
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Mon Feb 25, 2008 11:59 pm   Post subject: Re: Array of C-strings

Anon @ Mon Feb 25, 2008 11:32 pm wrote:
Because to allocate for each block iteratively would seem like a lot of needless overhead for N>2. If a more compact method exists, I'd love to hear about it!
You would need to do it that way if you have a jagged array. Since you do not, and your sub-dimension sizes are fixed, using a struct works great. If you do not have a jagged array but your sizes are not fixed, you could allocate a big block and refer to it through a pointer to VLA (I think).
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 17 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: