g***l 发帖数: 2753 | 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. |
S****t 发帖数: 1186 | 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.
|
g***l 发帖数: 2753 | 3 details,please?
what is b/w?
【在 S****t 的大作中提到】 : find the diff b/w buffer + 1 and buffer : : int
|
S****t 发帖数: 1186 | 4 b/w = between :)
(char*)(buffer + 1) - (char*)(buffer)
【在 g***l 的大作中提到】 : details,please? : what is b/w?
|
g***l 发帖数: 2753 | 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)
|
S****t 发帖数: 1186 | 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.
|
g*******m 发帖数: 42 | 7 size = sizeof num /sizeof *num; |
k*k 发帖数: 508 | 8 did you ever try it yourself?
【在 g*******m 的大作中提到】 : size = sizeof num /sizeof *num;
|
c*****g 发帖数: 119 | 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.
|
O*******d 发帖数: 20343 | 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.
|
|
|
j****g 发帖数: 597 | 11 no way in C or C++, or there won't be buffer overflow problem. |
g***l 发帖数: 2753 | 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.
|
e*a 发帖数: 32 | 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.
|
a****l 发帖数: 8211 | 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.
|
g***l 发帖数: 2753 | 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?
|
k*k 发帖数: 508 | 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.
|
h****e 发帖数: 2125 | 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.
|
t*r 发帖数: 26 | 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.
|
t*r 发帖数: 26 | 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...
|
t****t 发帖数: 6806 | 20 how?
【在 t*r 的大作中提到】 : "I don't know" is a much better answer than "No way". It can be done. : : whatever
|
|
|
S****t 发帖数: 1186 | 21 co ask
【在 t****t 的大作中提到】 : how?
|
h****e 发帖数: 2125 | 22 done in your dream.
【在 t*r 的大作中提到】 : "I don't know" is a much better answer than "No way". It can be done. : : whatever
|
e*a 发帖数: 32 | |
b********n 发帖数: 609 | |
P********e 发帖数: 2610 | 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.
|
P********e 发帖数: 2610 | 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.
|
e*****w 发帖数: 144 | 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.
|
e*****w 发帖数: 144 | 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 : }
|
g***l 发帖数: 2753 | 29 yes. this is the function he read to me.
【在 e*****w 的大作中提到】 : 如果是下面的原型,那肯定没戏了: : int GetTheSizeofMemory(int *buffer);
|
e*a 发帖数: 32 | 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) : {
|
|
|
b******g 发帖数: 54 | 31 这么做不对吗?好像没问题啊。
【在 b********n 的大作中提到】 : 你这多让父母伤心哪。
|
k*k 发帖数: 508 | 32 why don't you try it on your pc?
【在 b******g 的大作中提到】 : 这么做不对吗?好像没问题啊。
|
b******g 发帖数: 54 | 33 tried, 输出10啊。
【在 k*k 的大作中提到】 : why don't you try it on your pc?
|
k*k 发帖数: 508 | 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啊。
|
b******g 发帖数: 54 | 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.
|
h****e 发帖数: 2125 | 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); : }
|
O*******d 发帖数: 20343 | 37 有没有exception和操作系统有关系。
【在 e*a 的大作中提到】 : 哈,我一开始也这么想的,试了试,发现没exception
|
b******g 发帖数: 54 | 38 你跟我说的不是一回事。你这返回的是指针。so simple, sometimes naive. haha
【在 h****e 的大作中提到】 : try : int * s = (int *)malloc(10 * sizeof(int)); : sigh, you guys are so...
|
h****e 发帖数: 2125 | 39 wo kao...
【在 b******g 的大作中提到】 : 你跟我说的不是一回事。你这返回的是指针。so simple, sometimes naive. haha
|
t****t 发帖数: 6806 | 40 嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧.
啊,你们都说我脾气不好,嘿嘿,嘿嘿.
【在 h****e 的大作中提到】 : wo kao...
|
|
|
h****e 发帖数: 2125 | 41 kick,还有幸灾乐祸的
【在 t****t 的大作中提到】 : 嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧. : 啊,你们都说我脾气不好,嘿嘿,嘿嘿.
|
t*r 发帖数: 26 | 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.
|
d*******d 发帖数: 2050 | 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 : }
|
S****t 发帖数: 1186 | 44 ha, that's why i simply replied gamil "then good luck"
【在 t****t 的大作中提到】 : 嘿嘿,你知道有时候对牛弹琴是一件很累人的事了吧. : 啊,你们都说我脾气不好,嘿嘿,嘿嘿.
|
e*a 发帖数: 32 | 45 thrust是因为别人不懂他的答案,但是你的答案本来就不对嘛
【在 S****t 的大作中提到】 : ha, that's why i simply replied gamil "then good luck"
|
S****t 发帖数: 1186 | 46 right. but please take a look at which part i replied :)
【在 e*a 的大作中提到】 : thrust是因为别人不懂他的答案,但是你的答案本来就不对嘛
|
o**o 发帖数: 3964 | 47 如果说明int* buff是malloc或者什么函数返回的,取决于implementation,从这个
地址可能推算保存内存分配信息的地址,从而得到当初分配内存的大小。
【在 g***l 的大作中提到】 : yes. this is the function he read to me.
|