Advertisement

Can't return array from a function, right?

Started by April 12, 2002 08:13 PM
38 comments, last by rhino2876 22 years, 5 months ago
from Oluseyi:
First page, second reply. std::vector. Have a nice day.

Unfortunately, there was a slight bit of confusion in some of the posts following the second reply that needed clearing up.

Karg
quote: Original post by Anonymous Poster
Making blanket statements like "you should avoid them and use vectors instead" doesn''t get us anywhere, especially when those statements are based on less than complete information.

On the contrary, there are very few problems requiring an array that can''t be solved with a vector, and a vector behaves as most people expect arrays to behave. The existence of this thread is evidence of the problems with arrays.
Advertisement
no, the evidence gathered from this thread is that there is a problem with how ppl percieve terminology and how books are anal. there are not problems with arrays (which are just blocks of memory).

an array is a varible that contains an address that points to a block of memory.

a pointer is a varible that contains an address that points to a block of memory.

so far i fail to see the difference except that ppl consider arrays to be pre allocated. while pointers are considered general varibles that contain addresses which change at runtime (as if using malloc or new does not create an array of elements for you). in the end its simply that many books/teachers make gross errors on differentiating arrays and pointers. they are the same things with different names.

heck it reminds me of multidimensial arrays, which many ppl think require having arrays of arrays (ie planar) and that a flat chuck of memeory (ie linear) is not really multidimensioal (even if you are accessing elements as if it were multidimensional).


  int *linear;int **planar;linear = (int*)malloc(width*height*sizeof(int));planar = (int**)malloc(height*sizeof(int*));for(i=0; i<height; i++)    planar[i] = (int*)malloc(width*sizeof(int));// accesselement = linear[y*width+x];element = planar[y][x];  


you see, both multidimensial. basically its more a matter of ppl needing to understand the underlying concepts of what is going on vs understanding silly text book definations.

i vote that this and other seemingly non obvious questions should be added to a faq. this will help newbies understand programming better, since many times its the conflicting definations of things that make things seem quirky when in fact when looked at in a more generalized manner work together well.

btw, a std::vector is not an array if a pointer is not an array. since after all, std::vector is really an object. which then circles back to say that the original asker of the question answer his own question but was confused by some definations or concepts.

have a nice day.
quote: Original post by a person
an array is a varible that contains an address that points to a block of memory.

No, an array *is* a block of memory. Referencing the array by it''s name decays into a pointer to the first element.
quote:
so far i fail to see the difference

That appears to be because you don''t know the difference.
quote:
they are the same things with different names.

No they are not. Please stop spreading this misinformation.
quote:
heck it reminds me of multidimensial arrays, which many ppl think require having arrays of arrays

They do. A quote from TC++PL (section C.7):

"Multidimensional arrays are represented as arrays of arrays".

Some languages directly support multidimensional arrays. C++ doesn''t, but they can be satisfactorily emulated.
quote:
i vote that this and other seemingly non obvious questions should be added to a faq.

I hope you''re not suggesting you should write it.
quote:
btw, a std::vector is not an array if a pointer is not an array.

A pointer is not an array.
quote:
since after all, std::vector is really an object.

No it''s not. It''s a templated class. An instance of a vector behaves as most people reasonably expect an array to behave.
quote:
which then circles back to say that the original asker of the question answer his own question but was confused by some definations or concepts.

I believe the OP went away happy that he understood the differences between arrays and pointers, and why an array cannot be passed into, or returned from, a function. The only person who seems to be confused is you.

I''ll leave you with another quote from Stroustrup:

"The built-in arrays are a major source of errors - especially when they are used to build multidimensional arrays. For novices, they are also a major source of confusion. Wherever possible, use vector, list, valarray, string, etc."
The value of an array is a pointer to the first element in the array, and you can use that to effectively pass arrays around.

I''m not entirely sure what a multidimensional array is. I know some languages have such a thing, but I''m not sure how the memory configuration of int[10,10] multiDimensionalArray; (in C#-like languages) differs from int array[10][10];. The latter creates ten arrays of ten elements contiguously (i.e. an array of array); I would have thought the former did the same. If it doesn''t, what does it do?

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
quote: Original post by DrPizza
The value of an array is a pointer to the first element in the array, and you can use that to effectively pass arrays around.

An array can be implicitly converted to a pointer to the first element, but that doesn''t mean an array is a pointer. And yes, a pointer to the array can be passed around, but not the array itself. The lack of value semantics in this scenario is a major source of irritation to experienced programmers, and drives newbies quite mad. That''s not what C++ has to be about. As Oluseyi said several posts ago, use std::vector and get on with it.
quote:
I''m not entirely sure what a multidimensional array is. I know some languages have such a thing, but I''m not sure how the memory configuration of int[10,10] multiDimensionalArray; (in C#-like languages) differs from int array[10][10];.

The memory configuration isn''t important. It''s the syntax (and semantics, naturally) that is offered that defines a multidimensional array, and what the compiler does with it is the compiler''s business.
quote:
The latter creates ten arrays of ten elements contiguously (i.e. an array of array); I would have thought the former did the same. If it doesn''t, what does it do?

Who cares? The fact that we have to concern ourselves with the way C++ arrays work internally to understand them is a serious detraction from actually doing productive work. The same philosophy should not be carried through to any hypothetical multi-dimensional array in C++.
Advertisement


I suppose, when some talk about "arrays" and "pointers",
they actually mean "array variable" when saying arrays and pointers are the
same thing.

ie. when defining
int a[10];
or
int *a = new int[10];

in both cases "a" is just a pointer.

However, an array is a memory chunk.
And a memory chunk is not the same as a pointer
to the beginning of a memory chunk.
So, if you''re taking it precisely, SabreMan is right.

("a" is not the array itself, "a" points to it.)



ooops! I see... posted 2 mins after sabre explained it again...

- unshaven
I am not going to name names (although it is very obvious), but I really think some of you should learn to be more polite. Some of you have a serious problem correcting others peoples'' posts without acting really suprior and pastronising.

Why are you acting this way?

It does not help anyone and just makes people ignore your opinions. If you are too clever for everyone else why don''t you find somewhere else to post.

Maybe you should stop learning programming for a minute and take a lesson in manners.
Good bye...

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement