LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-25-2004, 11:28 PM   #1
amoor757
LQ Newbie
 
Registered: Oct 2003
Location: wisconsin
Distribution: slack
Posts: 7

Rep: Reputation: 0
sizeof( ) in c++ question


hi guys,

Is there anyway to use sizeof within a local function when
I've passed an array to this function.

When I use a function call and function heading like so....

someFunct(int* const array)
{
cout << sizeof array/ size(int);
}

in the effort to determine the number of
elements in the array, I get 4, which is
the size of memory in bytes of an int data type

I think I can only use the
cout << sizeof array / sizeof(int)

in main, is this so?

Thanks much!
 
Old 01-26-2004, 12:28 AM   #2
amoor757
LQ Newbie
 
Registered: Oct 2003
Location: wisconsin
Distribution: slack
Posts: 7

Original Poster
Rep: Reputation: 0
OK,

So maybe if I clear things up I wont confuse everyone.

I'm trying to pass two arrays to a function. Among other things
I want to compare the size of the array(# of elements)
which can be accomplished by
sizeof array / sizeof(int)

This works fine in main(), but once I reference the arrays
to a local function and I run the same code,
I get a value of 1.

Breaking it down,

sizeof array results in 4
sizeof(int) results in 4
4/4 results in 1

So, I guess in the local function, the first sizeof call
is working on the first element of the array and
not the whole array as in main( )

Does anyone know a trick for this, I am sure there must be.

Thanks
 
Old 01-26-2004, 02:20 AM   #3
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
When you pass the array to the reference variable, it becomes a pointer(sort of). So, when you say 'size of', you get the size of the reference variable(an int here) and not the size of the array.
 
Old 01-26-2004, 06:18 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
As a generall guide, sizeof will only tell you the size of things whose size is known at compile time. As soon as you pass an array as an argument, as UltimaGuy said, it basically becomes a pointer, and (without complicated analysis of the code) the C compiler has no way of knowing the size of the block of memory a pointer is pointing to.

Alex

Last edited by llama_meme; 01-26-2004 at 06:19 AM.
 
Old 01-26-2004, 06:29 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
smart pointers are a class available from www.boost.org

There are other implementations of smart pointers as well. The idea is that these pointers know about things like array bounds and can return the length of arrays of objects.
 
Old 01-26-2004, 06:46 AM   #6
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Thanks jim mcnamara, I've joined in the boost's mailing list. I am going to play with it as this smart pointer concept is quite new to me.
 
Old 01-26-2004, 02:42 PM   #7
amoor757
LQ Newbie
 
Registered: Oct 2003
Location: wisconsin
Distribution: slack
Posts: 7

Original Poster
Rep: Reputation: 0
thanks everyone,!
I just guess I will not be running
sizeof() on referenced arrays anytime soon....
 
  


Reply



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
A strange usage of # and sizeof. George2 Programming 2 10-19-2005 12:11 AM
sizeof and malloc in C exvor Programming 4 08-07-2005 12:06 PM
when does sizeof ( ) operator return zero !! joeyBig Programming 3 10-01-2004 06:43 AM
sizeof my typedef... jhorvath Programming 7 10-11-2002 05:41 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:03 PM.

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
Open Source Consulting | Domain Registration