array size issue

P

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

#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);}
 
P

peter koch

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);}
 
M

mlimber

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-lite/containers.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
 
P

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
#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-lite/containers.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?
 
M

mlimber

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
 
J

Juha Nieminen

puzzlecracker said:
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.
 
P

puzzlecracker

 puzzlecracker said:
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.
 
P

puzzlecracker

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


Would you explain what it does?
 
J

Joe Greer

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
 
F

Fraser Ross

"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.
 
P

puzzlecracker

"puzzlecracker"


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.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top