473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

array size issue

Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks

#include<iostream>
#include<string>
using namespace std;

char array[200];

void func1(char *str){
cout<<"func1:"<<sizeof(str)<<endl;

}
int main(){

strcpy(array, "one.two");
func1(array);}
Aug 13 '08 #1
12 1355
On 13 Aug., 16:07, puzzlecracker <ironsel2...@gmail.comwrote:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array.
Of course it does - that is what you asked for.
Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
Yes. The easiest way is
cout<<"func1:"<<200<<endl;

Which can be optimised a little bit ;-)

The way to do it is of course to use std::vector instead, but there
are other ways - such as passing the size as a parameter.

/Peter
>
#include<iostream>
#include<string>
using namespace std;

char array[200];

void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;

}

int main(){

*strcpy(array, "one.two");
*func1(array);}
Aug 13 '08 #2
On Aug 13, 10:07*am, puzzlecracker <ironsel2...@gmail.comwrote:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks

#include<iostream>
#include<string>
using namespace std;

char array[200];

void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;

}

int main(){

*strcpy(array, "one.two");
*func1(array);}
Don't use arrays unless you have to; they're evil (http://
http://www.parashift.com/c++-faq-lit...html#faq-34.1). Use
std::string (you've already included the header) or std::vector<char>
instead. Both of these carry their size around with them, unlike
pointers (that's why std::memcpy needs a size parameter, for
instance). Alternately, if your teacher or boss requires that you use
arrays, use std::strlen or pass in sizeof(array) into func1.

Cheers! --M
Aug 13 '08 #3
On Aug 13, 10:23*am, mlimber <mlim...@gmail.comwrote:
On Aug 13, 10:07*am, puzzlecracker <ironsel2...@gmail.comwrote:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
#include<iostream>
#include<string>
using namespace std;
char array[200];
void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;
}
int main(){
*strcpy(array, "one.two");
*func1(array);}

Don't use arrays unless you have to; they're evil (http://www.parashift.com/c++-faq-lit...html#faq-34.1). Use
std::string (you've already included the header) or std::vector<char>
instead. Both of these carry their size around with them, unlike
pointers (that's why std::memcpy needs a size parameter, for
instance). Alternately, if your teacher or boss requires that you use
arrays, use std::strlen or pass in sizeof(array) into func1.

Cheers! --M
I have to use char [] for optimization. What are my options?
Aug 13 '08 #4
In article
<27**********************************@l42g2000hsc. googlegroups.com>,
puzzlecracker <ir*********@gmail.comwrote:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
[...]
char array[200];
void func1(char *str){
cout<<"func1:"<<sizeof(str)<<endl;
}
[...]
func1(array);}
Here's a direct answer, though it's probably not what you want. If this
is for homework, your prof won't believe you figured it out, or might
not even understand it (but it can be rewritten with a typedef in a
clearer way, which he might understand/believe):

void func1(char (&str)[200]){
cout<<"func1:"<<sizeof(str)<<endl;

}
Aug 13 '08 #5
On Aug 13, 10:56*am, puzzlecracker <ironsel2...@gmail.comwrote:
I have to use char [] for optimization. What are my options?
See my previous response for your options.

Also, don't forget Hoare's dictum: premature optimization is the root
of all evil in programming. And remember that it is far easier to make
a correct program fast than it is to make a fast program correct. To
quote guru Sutter (http://www.gotw.ca/publications/mill09.htm):

'The rules [for optimization] boil down to: "1. Don't optimize early.
2. Don't optimize until you know that it's needed. 3. Even then, don't
optimize until you know what needed, and where."

'By and large, programmers--that includes you and me--are notoriously
bad at guessing the actual space/time performance bottlenecks in their
own code. If you don't have performance profiles or other empirical
evidence to guide you, you can easily spend days optimizing something
that doesn't need optimizing and that won't measurably affect runtime
space or time performance. What's even worse, however, is that when
you don't understand what needs optimizing you may actually end up
pessimizing (degrading your program) by of saving a small cost while
unintentionally incurring a large cost. Once you've run performance
profiles and other tests, and you actually know that a particular
optimization will help you in your particular situation, then it's the
right time to optimize.'

In other words, use std::string until you can prove *by profiling
measurements* that a char array is necessary.

Cheers! --M
Aug 13 '08 #6
puzzlecracker wrote:
Here is a dummy program that outputs 4 instead of 200.
That's because, in your computer, the size of a char pointer is 4
bytes. There's nothing strange there.
I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array.
You don't have to suspect anything. The function gets a pointer, and
it sees a pointer. There's no "array" of any kind, just a pointer.
Is there way to make it print 200?
Give 200 as a second parameter to the function.
Aug 13 '08 #7
On Aug 13, 10:57*am, blargg <blargg....@gishpuppy.comwrote:
In article
<2717b187-29ac-4d08-b9b3-0d8b24cc6...@l42g2000hsc.googlegroups.com>,

*puzzlecracker <ironsel2...@gmail.comwrote:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
[...]
char array[200];
void func1(char *str){
* cout<<"func1:"<<sizeof(str)<<endl;
}
[...]
*func1(array);}

Here's a direct answer, though it's probably not what you want. If this
is for homework, your prof won't believe you figured it out, or might
not even understand it (but it can be rewritten with a typedef in a
clearer way, which he might understand/believe):

void func1(char (&str)[200]){
* cout<<"func1:"<<sizeof(str)<<endl;

}

Good idea, thanks. It's actually for work, fixing issue with trading
system.
Aug 13 '08 #8

void func1(char (&str)[200]){
* cout<<"func1:"<<sizeof(str)<<endl;
}
Would you explain what it does?
Aug 13 '08 #9
puzzlecracker <ir*********@gmail.comwrote in news:2717b187-29ac-4d08-
b9***************@l42g2000hsc.googlegroups.com:
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks

#include<iostream>
#include<string>
using namespace std;

char array[200];

void func1(char *str){
cout<<"func1:"<<sizeof(str)<<endl;

}
int main(){

strcpy(array, "one.two");
func1(array);}
Sadly, your options are limited.

1) If the function is only used for one kind of array, you can always
declare it as:

void func1(char (&str)[200])
{
cout<<"func1:"<<sizeof(str)<<endl;

}

and the size will be maintained.

2) If that isn't an option, you can pass the size in as a parameter.

void func1(char * str, size_t sz)
{
cout<<"func1:"<<sz<<endl;

}

3) You can write a template

template<int N>
void func1(char (&str)[N])
{
cout<<"func1:"<<sizeof(str)<<endl;
cout<<"func1:"<<N<<endl;

}

Hope that helps in some way.

joe
Aug 13 '08 #10
"puzzlecracker"
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks
Can't the function access the global directly? If not the function
would have to be a function template with a pearameter that is a
reference to an array of a size that is a template parameter.

Fraser.
Aug 13 '08 #11
On Aug 13, 1:44*pm, "Fraser Ross" <a...@zzzzzzzzzzz.comwrote:
"puzzlecracker"
Here is a dummy program that outputs 4 instead of 200. I suspect it's
because it calculates the size of pointer on 32 bit machines instead
of array. Is there way to make it print 200? I need to pass the this
global variable via a function call. Thanks

Can't the function access the global directly? *If not the function
would have to be a function template with a pearameter that is a
reference to an array of a size that is a template parameter.

Fraser.
I have a bunch of globals (char arrays), I have one function that
processes them, I pass each of them from a different functions,
respectively.
Aug 13 '08 #12
In article
<92**********************************@x35g2000hsb. googlegroups.com>,
puzzlecracker <ir*********@gmail.comwrote:
void func1(char (&str)[200]){
cout<<"func1:"<<sizeof(str)<<endl;
}

Would you explain what it does?
Defines a function which takes a reference to a char [200], then prints
the size of said array. Just think of how a function pointer is declared
and you'll see the similarity to the array reference above.

If you must use C-style arrays, avoid passing them around bare; put them
in a structure instead. If you must pass them around bare, use a typedef,
and avoid attempting to pass them by value.

vector<charstr; // best
struct str_t { char str [200]; }; // at least sane value semantics
typedef char str_t [200]; // avoid passing by value
Aug 13 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
10
by: apropo | last post by:
int main(void) { int v; int i,j,tmp; for(i=0;i<5;i++) { printf("v:",i+1); scanf("%d",&v); }
4
by: terry | last post by:
Hi, Could anyone tell me how to determine the size of array of characters dynamically? For example, : : char *a={"hello","hi","kitty"}; char *b={"orange","apple"};
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
4
by: mathieu | last post by:
Hello, I would like implement a nice way to work around the array of references issue in C++. What do usually people do ? Do you maintain a separate factory/pool of elements as part of the API ?...
12
by: howa | last post by:
// example #include <iostream> using namespace std; size_t bsearch(int *a) { cout<<a<<endl; cout<<sizeof(a) / sizeof(a)<<endl;
24
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.