473,808 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#include<string >
using namespace std;

char array[200];

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

}
int main(){

strcpy(array, "one.two");
func1(array);}
Aug 13 '08 #1
12 1396
On 13 Aug., 16:07, puzzlecracker <ironsel2...@gm ail.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<iostre am>
#include<string >
using namespace std;

char array[200];

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

}

int main(){

*strcpy(array, "one.two");
*func1(array);}
Aug 13 '08 #2
On Aug 13, 10:07*am, puzzlecracker <ironsel2...@gm ail.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<iostre am>
#include<string >
using namespace std;

char array[200];

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

}

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<cha r>
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...@gm ail.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<iostre am>
#include<string >
using namespace std;
char array[200];
void func1(char *str){
* cout<<"func1:"< <sizeof(str)<<e ndl;
}
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<cha r>
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************ *************** *******@l42g200 0hsc.googlegrou ps.com>,
puzzlecracker <ir*********@gm ail.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)<<e ndl;
}
[...]
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)<<e ndl;

}
Aug 13 '08 #5
On Aug 13, 10:56*am, puzzlecracker <ironsel2...@gm ail.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....@gis hpuppy.comwrote :
In article
<2717b187-29ac-4d08-b9b3-0d8b24cc6...@l4 2g2000hsc.googl egroups.com>,

*puzzlecracker <ironsel2...@gm ail.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)<<e ndl;
}
[...]
*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)<<e ndl;

}

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)<<e ndl;
}
Would you explain what it does?
Aug 13 '08 #9
puzzlecracker <ir*********@gm ail.comwrote in news:2717b187-29ac-4d08-
b9************* **@l42g2000hsc. googlegroups.co m:
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<iostre am>
#include<string >
using namespace std;

char array[200];

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

}
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)<<e ndl;

}

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)<<e ndl;
cout<<"func1:"< <N<<endl;

}

Hope that helps in some way.

joe
Aug 13 '08 #10

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

Similar topics

6
4154
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
10
1298
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
2814
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
5490
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 elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
4
2829
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
4
1663
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 ? So in my example (*) I would need to either : 1. allocate on the heap (and then fragment memory): void fill(A *array) {
12
2017
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
1967
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
14214
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
17
7263
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 to show the array data to the end user. Can I do that? How?
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.