#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}
gives me :
1
8 <- ????? Why ????
40
5 4 12085
onkar wrote:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}
gives me :
1
8 <- ????? Why ????
2*sizeof(char*) ?
--
Ian Collins.
onkar wrote:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
Have a look at std::make_pair.
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}
gives me :
1
8 <- ????? Why ????
Why not? What did you expect?
40
5
Hi,
sizeof of STL containers does not return the size of the contained
object - sizeof vector<charcontaing 100 chars is not 100. You need to
check your STL implementation of pair to see why it is returning 8.
Thanks and regards
Sonison James
onkar wrote:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}
gives me :
1
8 <- ????? Why ????
40
5
In message <11*********************@b28g2000cwb.googlegroups. com>, so***********@gmail.com writes
[Please don't top-post. Rearranged]
>onkar wrote:
>#include<iostream> using namespace std; int main(void){ const pair<const char*,const char*arr[]={ pair<const char*,const char*>("1","1"), pair<const char*,const char*>("12","12"), pair<const char*,const char*>("123","123"), pair<const char*,const char*>("1234","1234"), pair<const char*,const char*>("12345","12345"), }; cout<<sizeof(char)<<endl; cout<<sizeof(arr[0])<<endl; cout<<sizeof arr<<endl; cout<<sizeof(arr)/sizeof(arr[0])<<endl; return 0; }
gives me :
1 8 <- ????? Why ???? 40 5
sizeof of STL containers does not return the size of the contained object - sizeof vector<charcontaing 100 chars is not 100.
std::pair isn't a "container" the way vector is. It's just a templated
2-element structure.
>You need to check your STL implementation of pair to see why it is returning 8.
What would you expect the size of the following to be?
struct {
const char * first;
const char * second;
};
--
Richard Herring This discussion thread is closed Replies have been disabled for this discussion. Similar topics
14 posts
views
Thread by Neil Zanella |
last post: by
|
3 posts
views
Thread by JustSomeGuy |
last post: by
|
6 posts
views
Thread by pmatos |
last post: by
|
1 post
views
Thread by MDS |
last post: by
|
3 posts
views
Thread by Jon |
last post: by
|
4 posts
views
Thread by Florent Garcin |
last post: by
|
11 posts
views
Thread by kietzi |
last post: by
| |
10 posts
views
Thread by Alex Vinokur |
last post: by
| | | | | | | | | | |