# boards

C++ interdependence questiondifference between: char** p and char*p[] ??
C arrayC++ pointer problem

C里面一个被分配了内存的指针如何知道分配了多少?C语言的变量都一定要放在stack上吗？

 1 (共1页)
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.
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.

g***l

3
what is b/w?

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

S****t

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

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

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)

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.

g*******m

7
size = sizeof num /sizeof *num;
k*k

8
did you ever try it yourself?

【在 g*******m 的大作中提到】
: size = sizeof num /sizeof *num;
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.

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.

C里面一个被分配了内存的指针如何知道分配了多少?difference between: char** p and char*p[] ??

j****g

11
no way in C or C++, or there won't be buffer overflow problem.
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.

【在 j****g 的大作中提到】
: no way in C or C++, or there won't be buffer overflow problem.
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.

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.

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?
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.

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.

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.

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

t****t

20
how?

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

C++里get array size的问题 (转载)问个virtual table 的问题

S****t

21

【在 t****t 的大作中提到】
: how?
h****e

22

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

e*a

23
peng
b********n

24

P********e

25
I don't know if this will work or not, but, at least nobody is giving
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

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

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

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

29
yes. this is the function he read to me.

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

e*a

30

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

what's wrong with this C++ code?C++ interdependence question
C++小插曲C array

b******g

31

【在 b********n 的大作中提到】
: 你这多让父母伤心哪。
k*k

32
why don't you try it on your pc?

【在 b******g 的大作中提到】
: 这么做不对吗？好像没问题啊。
b******g

33
tried, 输出10啊。

【在 k*k 的大作中提到】
: why don't you try it on your pc?
k*k

34

#include
int GetSizeOfMemory(int *a) {
return sizeof(a)/sizeof(int);
}
int main() {
int a[10];
printf("%d\n", GetSizeOfMemory(a));
}

【在 b******g 的大作中提到】
: tried, 输出10啊。
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.

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

O*******d

37

【在 e*a 的大作中提到】
: 哈，我一开始也这么想的，试了试，发现没exception
b******g

38

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

h****e

39
wo kao...

【在 b******g 的大作中提到】
: 你跟我说的不是一回事。你这返回的是指针。so simple, sometimes naive. haha
t****t

40

【在 h****e 的大作中提到】
: wo kao...

h****e

41
kick，还有幸灾乐祸的

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

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 的大作中提到】
d*******d

43

【在 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

44
ha, that's why i simply replied gamil "then good luck"

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

e*a

45
thrust是因为别人不懂他的答案，但是你的答案本来就不对嘛

【在 S****t 的大作中提到】
: ha, that's why i simply replied gamil "then good luck"
S****t

46
right. but please take a look at which part i replied :)

【在 e*a 的大作中提到】
: thrust是因为别人不懂他的答案，但是你的答案本来就不对嘛
o**o

47

【在 g***l 的大作中提到】
: yes. this is the function he read to me.
 1 (共1页)

C puzzle 一日一题数组定义的时候，分配空间了么？
what's wrong with this C++ code?C里面一个被分配了内存的指针如何知道分配了多少?
C++小插曲在子函数内开内存，返回主函数指针然后释放空间是不是很糟糕的(转载)

C++ interdependence questiondifference between: char** p and char*p[] ??
C arrayC++ pointer problem