Help with sizeof and pointers

K

Kevin Goodsell

Buster said:
I think we're in agreement about what does and doesn't work. What I expected
to happen, happens. What I'm not sure of is whether what I expect is enforced
by the standard.

I should have removed that line from my post when I realized what you
were doing.
The question was "I want to find the length of an int array that is being
passed to a function". No mention of pointers there.

But an array cannot be (directly) passed to a function. I think the
template and/or reference solution seems unlikely to work well in
practice. When dealing with "arrays", one often actually deals with
pointers (because it was passed from elsewhere, or dynamically allocated).

-Kevin
 
A

Artie Gold

Buster said:
I already posted some. Shane Beasley's post has examples too.
Unless I'm missing something (not terribly unlikely), all the
examples provided only work when the actual array is in scope
at the point where its size is being asked (which means that
the size would be known anyway).

BTW - there's no flame intended here at all.

Cheers,
--ag
 
D

Duane Hebert

What happens if you do:
int main ()
{
short array [] = { 1, 2, 3, 4, 5, 6, 7, 8 };
std::cout << MACRO (array) << std::endl;
}

Implementation specific. But I should have had:
#define MACRO(a) (sizeof (a) / sizeof * (a))
which works in that situation.

I'm curious. Why not just use std::vector<int>?
 
B

Buster

Kevin Goodsell said:
But an array cannot be (directly) passed to a function.

You've got me there. Indirection is required, although in this case
it's penalty-free and pretty much a syntactic thing.
I think the
template and/or reference solution seems unlikely to work well in
practice. When dealing with "arrays", one often actually deals with
pointers (because it was passed from elsewhere, or dynamically allocated).

Agreed, with reservations. The template I gave (or one of Shane's) works
when it works and doesn't do any harm.

IIRC, reference-to-array function parameters are not well supported on
certain compilers (*cough* borland) and can decay into a pointer to the
first element. That's another reason to be careful with them.

Regards,
Buster
 
B

Buster

Artie Gold said:
Unless I'm missing something (not terribly unlikely), all the
examples provided only work when the actual array is in scope
at the point where its size is being asked (which means that
the size would be known anyway).

No, you're quite right. As others have said, it's not terribly useful.
But I have found something like the following handy in the past, to
save a little typing.

template <typename U, typename V, std::size_t N>
U (& u) [N] add (U (& u) [N], V (& v) [N])
{
for (std::size_t i = 0; i != N; ++ i) u += v;
return u;
}
BTW - there's no flame intended here at all.

None taken; likewise.

Buster.
 
A

Artie Gold

Buster said:
Artie Gold said:
Unless I'm missing something (not terribly unlikely), all the
examples provided only work when the actual array is in scope
at the point where its size is being asked (which means that
the size would be known anyway).


No, you're quite right. As others have said, it's not terribly useful.
But I have found something like the following handy in the past, to
save a little typing.

template <typename U, typename V, std::size_t N>
U (& u) [N] add (U (& u) [N], V (& v) [N])
{
for (std::size_t i = 0; i != N; ++ i) u += v;
return u;
}

BTW - there's no flame intended here at all.


None taken; likewise.

Oh. Well. OK then!

Cheers,
--ag
 
K

Kevin Goodsell

Steven said:
I'm curious what news reader program u use that is better with bottom
posting.

Correct posting does not depend on the news client. Please read the
following:

http://www.cs.tut.fi/~jkorpela/usenet/brox.html
http://www.caliburn.nl/topposting.html
http://www.dickalba.demon.co.uk/usenet/guide/faq_topp.html

Top-posting is wrong. It violates internet standards. It's illogical. It
encourages over-quoting. You've been asked not to do it. If you have a
problem with that, I suggest you find a group that doesn't care how you
post. Here, we insist on proper posting (for many reasons).

-Kevin
 
K

Kevin Goodsell

Steven said:
Chill out I just asked! I guess it's kind of a religious thing with you.

Please quote some context when replying. Assuming your reply was meant
for me, here's my response.

I read usenet a lot. I actively participate in groups, and I frequently
search the archives for information. Correct posting makes everything go
much more smoothly. It avoids confusion and misunderstandings, and keeps
the discussion organized and easy to follow. It also prevents wasted
time and space caused by overquoted replies. Isn't that worth a little
tiny bit of extra effort, especially considering 1) the size of the
audience (potentially thousands) and 2) the lifetime of the articles
(maybe forever)?

It's not as if these are arbitrary rules. They have a purpose, and were
determined to be the best practices based on experience.

-Kevin
 
J

Jon Bell

I'm curious what news reader program u use that is better with bottom
posting.

Actually, even with Outlook Express it's easy to post "properly". All you
have to do is start at the top where OE puts you to begin with, and scroll
down through the quoted text. As you go, delete the stuff that isn't
directly relevant to your response. When you come to a point that you
want to respond to, stop scrolling and put your comments after that point.
Then, if you're not at the end of the message yet, keep scrolling and
deleting, and inserting more comments if appropriate. Eventually you'll
reach the bottom, and your comments will be interspersed nicely among the
remaining quoted material, point/counterpoint style.

With this procedure, it's actually *better* to have the cursor at the top
to begin with. If the cursor starts out at the bottom, you have to scroll
all the way up to the top before you can start working your way down.
 
J

Jack Klein

You have two choices either end your array with a unque int or pass the
size.

main()
{
int array[]={1,2,3,4,5,6,7,8};
function (array, sizeof(array)/sizeof(int))
}

function(int *array, int size)
^^^

function(int *array, size_t size)
{
}



hello,
ok, I want to find the length of an int array that is being passed to a
function:

main()
{
int array[]={1,2,3,4,5,6,7,8};
function(array);
}
function(int *array)
{
int arraylen = sizeof(*array)/sizeof(int);
}


arraylen should be 8 I get 1.

What am I doing Wrong?

Thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top