Redian新闻
>
ask a simple question about int pointer.
avatar
ask a simple question about int pointer.# Programming - 葵花宝典
g*l
1
I was asked such a question: how to determine the size of memory of the int
pointer pointed? for example
int GetTheSizeofMemory(int *buffer)
{
int size;
//enter your code here, to calculate the memory size of buffer point to.
return size;
}
we can not use sizeof(buffer) to get the value, how should we do?
thanks.
avatar
S*t
2
find the diff b/w buffer + 1 and buffer

int

【在 g***l 的大作中提到】
: I was asked such a question: how to determine the size of memory of the int
: pointer pointed? for example
: int GetTheSizeofMemory(int *buffer)
: {
: int size;
: //enter your code here, to calculate the memory size of buffer point to.
: return size;
: }
: we can not use sizeof(buffer) to get the value, how should we do?
: thanks.

avatar
g*l
3
details,please?
what is b/w?

【在 S****t 的大作中提到】
: find the diff b/w buffer + 1 and buffer
:
: int

avatar
S*t
4
b/w = between :)
(char*)(buffer + 1) - (char*)(buffer)

【在 g***l 的大作中提到】
: details,please?
: what is b/w?

avatar
g*l
5
no, this will always return the sizeof(int *)
I mean if we have
int num[]={1,2,3,4,5,6,7,8,9,0};
int size=GetTheSizeofMemory(num);
how do you get the size as 10?
I was asked this question, I finally told him that I dont know how to do
this.

【在 S****t 的大作中提到】
: b/w = between :)
: (char*)(buffer + 1) - (char*)(buffer)

avatar
S*t
6
then good luck, he he

【在 g***l 的大作中提到】
: no, this will always return the sizeof(int *)
: I mean if we have
: int num[]={1,2,3,4,5,6,7,8,9,0};
: int size=GetTheSizeofMemory(num);
: how do you get the size as 10?
: I was asked this question, I finally told him that I dont know how to do
: this.

avatar
g*m
7
size = sizeof num /sizeof *num;
avatar
k*k
8
did you ever try it yourself?

【在 g*******m 的大作中提到】
: size = sizeof num /sizeof *num;
avatar
c*g
9
no way.
if they can do so, why do so many system calls need to pass buffer size as a
parameter?

int

【在 g***l 的大作中提到】
: I was asked such a question: how to determine the size of memory of the int
: pointer pointed? for example
: int GetTheSizeofMemory(int *buffer)
: {
: int size;
: //enter your code here, to calculate the memory size of buffer point to.
: return size;
: }
: we can not use sizeof(buffer) to get the value, how should we do?
: thanks.

avatar
O*d
10
没有办法知道。

int

【在 g***l 的大作中提到】
: I was asked such a question: how to determine the size of memory of the int
: pointer pointed? for example
: int GetTheSizeofMemory(int *buffer)
: {
: int size;
: //enter your code here, to calculate the memory size of buffer point to.
: return size;
: }
: we can not use sizeof(buffer) to get the value, how should we do?
: thanks.

avatar
j*g
11
no way in C or C++, or there won't be buffer overflow problem.
avatar
g*l
12
the interviewer seemed unhappy for I told him I dont know the answer. Ju
st said,oh... That's OK, let's moving to another topic.
ai.............bad luck.

【在 j****g 的大作中提到】
: no way in C or C++, or there won't be buffer overflow problem.
avatar
e*a
13
did you get the job?

【在 g***l 的大作中提到】
: the interviewer seemed unhappy for I told him I dont know the answer. Ju
: st said,oh... That's OK, let's moving to another topic.
: ai.............bad luck.

avatar
a*l
14
the answer is "No way". This is a trick question.

【在 g***l 的大作中提到】
: the interviewer seemed unhappy for I told him I dont know the answer. Ju
: st said,oh... That's OK, let's moving to another topic.
: ai.............bad luck.

avatar
g*l
15
how?
the interviewers was not statisfied with this question, and then only as
ked me another question then said bye to me.
the talk only lasted for 15 minutes.
May be I misunderstood his question, but he did read the code, let me wr
ite some code to return the value of the size.
I also told him that we can not use sizeof(buffer) to get the value, he
agreed, then I have no idea.

【在 e*a 的大作中提到】
: did you get the job?
avatar
k*k
16
either you misunderstood him, or he is stupid.
There's still very small chance he is a super NB person that solved
a big problem that has troubled a lot of NB persons for years.

【在 g***l 的大作中提到】
: how?
: the interviewers was not statisfied with this question, and then only as
: ked me another question then said bye to me.
: the talk only lasted for 15 minutes.
: May be I misunderstood his question, but he did read the code, let me wr
: ite some code to return the value of the size.
: I also told him that we can not use sizeof(buffer) to get the value, he
: agreed, then I have no idea.

avatar
h*e
17
you should not just say "don't know", you should have bullshitted whatever
you know...

【在 g***l 的大作中提到】
: how?
: the interviewers was not statisfied with this question, and then only as
: ked me another question then said bye to me.
: the talk only lasted for 15 minutes.
: May be I misunderstood his question, but he did read the code, let me wr
: ite some code to return the value of the size.
: I also told him that we can not use sizeof(buffer) to get the value, he
: agreed, then I have no idea.

avatar
t*r
18
What kind of company asked you this question? What type of position?

int

【在 g***l 的大作中提到】
: I was asked such a question: how to determine the size of memory of the int
: pointer pointed? for example
: int GetTheSizeofMemory(int *buffer)
: {
: int size;
: //enter your code here, to calculate the memory size of buffer point to.
: return size;
: }
: we can not use sizeof(buffer) to get the value, how should we do?
: thanks.

avatar
t*r
19
"I don't know" is a much better answer than "No way". It can be done.

whatever

【在 h****e 的大作中提到】
: you should not just say "don't know", you should have bullshitted whatever
: you know...

avatar
t*t
20
how?

【在 t*r 的大作中提到】
: "I don't know" is a much better answer than "No way". It can be done.
:
: whatever

avatar
S*t
21
co ask

【在 t****t 的大作中提到】
: how?
avatar
h*e
22
done in your dream.

【在 t*r 的大作中提到】
: "I don't know" is a much better answer than "No way". It can be done.
:
: whatever

avatar
e*a
23
peng
avatar
b*n
24
你这多让父母伤心哪。
avatar
P*e
25
I don't know if this will work or not, but, at least nobody is giving
answers
int sizeOfArray(int[] a) {
int i = 0;
try {
while(true)
int x = a[i++];
}
catch(OutOfBoundException e)
{
return i-1;
}
}

【在 g***l 的大作中提到】
: no, this will always return the sizeof(int *)
: I mean if we have
: int num[]={1,2,3,4,5,6,7,8,9,0};
: int size=GetTheSizeofMemory(num);
: how do you get the size as 10?
: I was asked this question, I finally told him that I dont know how to do
: this.

avatar
P*e
26
or
int foo(int[] a, int& nextInstantiatedVariable)
{
int x = 0;
while( a+x != & nextInstantiatedVariable)
x++;
return x;
}

【在 g***l 的大作中提到】
: no, this will always return the sizeof(int *)
: I mean if we have
: int num[]={1,2,3,4,5,6,7,8,9,0};
: int size=GetTheSizeofMemory(num);
: how do you get the size as 10?
: I was asked this question, I finally told him that I dont know how to do
: this.

avatar
e*w
27
C++貌似可以用模板:
#include
template
std::size_t GetTheSizeofMemory(const T &t) {
return sizeof(t);
}
int main() {
int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
std::cout << GetTheSizeofMemory(num) << std::endl; // 40
}

【在 g***l 的大作中提到】
: no, this will always return the sizeof(int *)
: I mean if we have
: int num[]={1,2,3,4,5,6,7,8,9,0};
: int size=GetTheSizeofMemory(num);
: how do you get the size as 10?
: I was asked this question, I finally told him that I dont know how to do
: this.

avatar
e*w
28
如果是下面的原型,那肯定没戏了:
int GetTheSizeofMemory(int *buffer);

【在 e*****w 的大作中提到】
: C++貌似可以用模板:
: #include
: template
: std::size_t GetTheSizeofMemory(const T &t) {
: return sizeof(t);
: }
: int main() {
: int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
: std::cout << GetTheSizeofMemory(num) << std::endl; // 40
: }

avatar
g*l
29
yes. this is the function he read to me.

【在 e*****w 的大作中提到】
: 如果是下面的原型,那肯定没戏了:
: int GetTheSizeofMemory(int *buffer);

avatar
e*a
30
哈,我一开始也这么想的,试了试,发现没exception

【在 P********e 的大作中提到】
: I don't know if this will work or not, but, at least nobody is giving
: answers
: int sizeOfArray(int[] a) {
: int i = 0;
: try {
: while(true)
: int x = a[i++];
: }
: catch(OutOfBoundException e)
: {

avatar
b*g
31
这么做不对吗?好像没问题啊。

【在 b********n 的大作中提到】
: 你这多让父母伤心哪。
avatar
k*k
32
why don't you try it on your pc?

【在 b******g 的大作中提到】
: 这么做不对吗?好像没问题啊。
avatar
b*g
33
tried, 输出10啊。

【在 k*k 的大作中提到】
: why don't you try it on your pc?
avatar
k*k
34
仔细看原题
#include
int GetSizeOfMemory(int *a) {
return sizeof(a)/sizeof(int);
}
int main() {
int a[10];
printf("%d\n", GetSizeOfMemory(a));
}
输出 1.

【在 b******g 的大作中提到】
: tried, 输出10啊。
avatar
b*g
35
ft, 仔细看贴。我说的是这个怎么不对:
#include
#define sizeofmemory(a) (sizeof(a)/sizeof(int))
void main()
{
int s[]={0,1,2,3,4,5,6,7,8,9};
int *b;
int size = sizeofmemory(s);
printf("%d\n ",size);
}

【在 k*k 的大作中提到】
: 仔细看原题
: #include
: int GetSizeOfMemory(int *a) {
: return sizeof(a)/sizeof(int);
: }
: int main() {
: int a[10];
: printf("%d\n", GetSizeOfMemory(a));
: }
: 输出 1.

avatar
h*e
36
try
int * s = (int *)malloc(10 * sizeof(int));
sigh, you guys are so...

【在 b******g 的大作中提到】
: ft, 仔细看贴。我说的是这个怎么不对:
: #include
: #define sizeofmemory(a) (sizeof(a)/sizeof(int))
: void main()
: {
: int s[]={0,1,2,3,4,5,6,7,8,9};
: int *b;
: int size = sizeofmemory(s);
: printf("%d\n ",size);
: }

avatar
O*d
37
有没有exception和操作系统有关系。

【在 e*a 的大作中提到】
: 哈,我一开始也这么想的,试了试,发现没exception
avatar
b*g
38
你跟我说的不是一回事。你这返回的是指针。so simple, sometimes naive. haha

【在 h****e 的大作中提到】
: try
: int * s = (int *)malloc(10 * sizeof(int));
: sigh, you guys are so...

avatar
h*e
39
wo kao...

【在 b******g 的大作中提到】
: 你跟我说的不是一回事。你这返回的是指针。so simple, sometimes naive. haha
avatar
t*t
40
嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧.
啊,你们都说我脾气不好,嘿嘿,嘿嘿.

【在 h****e 的大作中提到】
: wo kao...
avatar
h*e
41
kick,还有幸灾乐祸的

【在 t****t 的大作中提到】
: 嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧.
: 啊,你们都说我脾气不好,嘿嘿,嘿嘿.

avatar
t*r
42
I have done it before, not in my dream.
There are simple "hack" methods to find out the memory block size associated
with a pointer. It won't work across all compilers/platforms.

【在 h****e 的大作中提到】
: done in your dream.
avatar
d*d
43
这个很nice,试验了,work.

【在 e*****w 的大作中提到】
: C++貌似可以用模板:
: #include
: template
: std::size_t GetTheSizeofMemory(const T &t) {
: return sizeof(t);
: }
: int main() {
: int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
: std::cout << GetTheSizeofMemory(num) << std::endl; // 40
: }

avatar
S*t
44
ha, that's why i simply replied gamil "then good luck"

【在 t****t 的大作中提到】
: 嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧.
: 啊,你们都说我脾气不好,嘿嘿,嘿嘿.

avatar
e*a
45
thrust是因为别人不懂他的答案,但是你的答案本来就不对嘛

【在 S****t 的大作中提到】
: ha, that's why i simply replied gamil "then good luck"
avatar
S*t
46
right. but please take a look at which part i replied :)

【在 e*a 的大作中提到】
: thrust是因为别人不懂他的答案,但是你的答案本来就不对嘛
avatar
o*o
47
如果说明int* buff是malloc或者什么函数返回的,取决于implementation,从这个
地址可能推算保存内存分配信息的地址,从而得到当初分配内存的大小。

【在 g***l 的大作中提到】
: yes. this is the function he read to me.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。