由买买提看人间百态

topics

全部话题 - 话题: sizeof
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
z***f
发帖数: 121
1
来自主题: Programming版 - sizeof()的问题
typedef struct
{
double
double
int
} TEST
sizeof(TEST)怎么是24,而不是20呢?
sizeof(double)+sizeof(double)+sizeof(int) != sizeof(TEST)?
l****5
发帖数: 95
2
来自主题: JobHunting版 - C++ Q35: sizeof() (B20_20)
According to Bjarne Stroustrup's book "The C++ Programming language
(special edition)" page 78 (the very top):
"The sizeof an enumeration is the sizeof some integral type that can
hold its range and not larger than sizeof(int), unless an enumerator
cannot be represented as an int or as an unsigned int".
Here integral type include Boolean, character and integer types.
Please also refer the discussion here
http://stackoverflow.com/questions/1113855/is-the-sizeofenum-sizeofint-
always

defined, and
c**********e
发帖数: 2007
3
来自主题: JobHunting版 - C++ Q35: sizeof() (B20_20)
What is the size of an enumeration type?
a) sizeof(size_t)
b) 2
c) 4
d) sizeof(int)
e) implementation-defined
n**e
发帖数: 116
4
来自主题: JobHunting版 - C++ Q78: about sizeof
sizeof(intPtr)/sizeof(int)
R****i
发帖数: 104
5
来自主题: JobHunting版 - C++ Q78: about sizeof
有记录。
不过sizeof()是编译时运行的函数,所以不能用来得到new 的size。
ref: http://shangshufu.blogspot.com/2011/08/tips-about-sizeof-function-in-c-and-c.html
f*******t
发帖数: 7549
6
来自主题: JobHunting版 - C++ Q78: about sizeof
int array[] = { 1, 2, 3, 4 };
you can get array length by sizeof(array)/sizeof(int)
if the array is dynamically allocated, you can't get its length
b******w
发帖数: 52
7
来自主题: Programming版 - sizeof(string)
I am a little confused about the sizeof(string) = 4.
Since string is a specialized basic_string, and basic_string is derived
from _String_base.
_String_base has three protected pointers:
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;
thus sizeof(string) should be 12 instead of 4?
is there anybody to elaborate on this?
template class _Traits = char_traits<_CharT>,
class _Alloc = __STL_DEFAULT_ALLOCATOR(_CharT) >
class basic_string;
typedef basic_
x***y
发帖数: 633
8
来自主题: JobHunting版 - C++ Q35: sizeof() (B20_20)
Not sure...It may be stored in data segment, but it must have a size at
least sizeof(int) to be able to be converted to int...
c**********e
发帖数: 2007
9
来自主题: JobHunting版 - C++ Q35: sizeof() (B20_20)
The absolute size of integral types is generally implementation defined, and
as this question further shows, the size of an enumeration is not even
required to be the size of a specific integral type.
d) sizeof(int)
This is incorrect. This is the common wrong answer. Enumeration types may
allow values that will not fit in the range of an int.
e) implementation-defined
This is correct. The actual size of an enumeration type depends in part on
the magnitude and sign of the enumerator values. Ho
c**********e
发帖数: 2007
10
来自主题: JobHunting版 - C++ Q78: about sizeof
If an integer array is defined as the following
int* intPtr=new int[100];
How to use the sizeof() function to get the size of the integer array?
b*****i
发帖数: 262
11
来自主题: JobHunting版 - C++ Q78: about sizeof
sizeof is not a function.
v*****r
发帖数: 1119
12
来自主题: Programming版 - 请问大家怎样让程序难以读懂
这么些就可以了:
\
int
_,l;\
char*I,
*O[]={"",
"gjstu","t"
"fdpoe","uij"
"se","gpvsui",\
"gjgui","t"
"jyui","tfwf"
"oui","fjhiui",
"ojoui","ufoui",\
"fmfwfoui","uxfmgu"
"i","b!qbsusjehf!jo!"
"b!qfbs!usff/\xb\xb",""
"uxp!uvsumf!epwf"... 阅读全帖
s****n
发帖数: 700
13
来自主题: Programming版 - a question about bitwise operation
那看看这个code
#include
using namespace std;
int main() {
int i;
unsigned int ui;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
i = 1 << (sizeof(i) * 8 - 1);
cout << "sizeof(i) is " << sizeof(i) << endl;
cout << "i is " << i < ui = 1 << (sizeof(ui) * 8 - 1);
cout << "sizeof(ui) is " << sizeof(ui) << endl;
cout << "ui is " << ui < l = 1 << (sizeof(l) * 8 - 1);
cout << "sizeof(l) is " << sizeof(l) << endl;
cout << "l is " << l < ul = 1 << (sizeof(ul) * 8 -... 阅读全帖
p****o
发帖数: 46
14
来自主题: JobHunting版 - leetcode 的 triangle 一题 oj 怎么不过
41 / 43 test cases passed.
Status: Wrong Answer

Input: [[-9],[-8,0],[-3,2,5],[6,3,0,-4],[-2,-9,-5,-8,6],[0,-5,0,-2,-1,5]
,[0,6,-1,-5,-8,6,-5],[-8,-5,-9,-8,-4,-3,-5,7]]
Output: -1217805963
Expected: -41
code:
class Solution {
public:
int minimumTotal(vector > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

int n = triangle.size();
vector vmin(n, 0);
while (n--){
... 阅读全帖
t**r
发帖数: 3428
15
来自主题: JobHunting版 - topcoder- strange country problem.
贴个答案
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const long long LINF = (5e18);
const int INF = (1<<30);
#define EPS 1e-6
const int MOD = 1000000007;
using namespace std;
class StrangeC... 阅读全帖
f****4
发帖数: 1359
16
来自主题: JobHunting版 - 问Thomson Reuters两道算法题
抛个没有count的;一次写对还是有难度的
grass的count的办法能简化代码
#include
int k = 0;
int pts[k+1]; //pts[0] for color 1; pts[k-1] for color k; pts[k] is the
current index in balls
void printArray(int *arr, int length)
{
for(int i = 0; i < length; i++)
printf("%d,",arr[i]);
printf("\n");
}
void KquickSortPartition(int *arr, int length)
{
if (arr == NULL || length == 0 || k <= 0) return;
printArray(arr, length);
for (int i = 0; i < k+1; i++)
pts[i] = 0;
int ball;
int color;
while (pts[k... 阅读全帖
i**p
发帖数: 902
17
来自主题: Programming版 - unsigned long long
哪位大牛能解释一下ullong 1, ullong 2, ullong 4, ullong 5 的输出吗?
此程序在 Android (GB) emulator 上运行。
//刚刚发现,程序中的反斜杠都被mitbbs过滤掉了。
printf("sizeof(unsigned int): %dn", sizeof(unsigned int));
printf("sizeof(unsigned long): %dn", sizeof(unsigned long));
printf("sizeof(unsigned long long): %dn", sizeof(unsigned long long));
unsigned int uint=0x01020304;
printf("uint: 0x%xn", uint);
unsigned long ulong=0x01020304;
printf("ulong 1: 0x%xn", ulong);
printf("ulong 2: 0x%lxn", ulong);
u... 阅读全帖
b***i
发帖数: 3043
18
来自主题: JobHunting版 - How to find the size of an array? Thanks.
1
type* arr=new type[size];
cout < 结果取决于type的大小 (sizeof(arr[0])),和系统的指针的大小 type*的大小。
比如64位系统,int* arr=new int[10];
sizeof(arr)/sizeof(arr[0])=2,因为指针是8bytes,int是4bytes
再比如32位系统, double* arr=new double[10];
sizeof(arr)/sizeof(arr[0])=0.5也就是显示0(取整)
2
不光stack上,static变量也可以算sizeof。
3
用引用可以保留size,但是不多此一举吗?
void foo(int (&array)[10]) {
std::cout << sizeof(array) << "\n";
}
4
int* arr = new int[10];之后,arr是一个指针。
而int arr[10]是数组,static int arr[10]也是数组,这两个在编译的时候就知道大
小,... 阅读全帖
s*********5
发帖数: 514
19
来自主题: JobHunting版 - find the first missing positive integer.
int FindFirstMissingPositiveInteger(int a[], int size)
{
int index = 0;
while (index < size)
{
while (a[index] != index && a[index] < size
&& a[index] >0)
{
int tmp = a[a[index]];
a[a[index]] = a[index];
a[index] = tmp;
}
++index;
}
index = 1;
while (index < size)
{
if (a[index] != index)
return index;
++index;
}
return index;
};
int main(... 阅读全帖
s*********5
发帖数: 514
20
来自主题: JobHunting版 - find the first missing positive integer.
int FindFirstMissingPositiveInteger(int a[], int size)
{
int index = 0;
while (index < size)
{
while (a[index] != index && a[index] < size
&& a[index] >0)
{
int tmp = a[a[index]];
a[a[index]] = a[index];
a[index] = tmp;
}
++index;
}
index = 1;
while (index < size)
{
if (a[index] != index)
return index;
++index;
}
return index;
};
int main(... 阅读全帖
X****r
发帖数: 3557
21
数组和指针是两回事。
比如int a[10];定义了一个有10个int元素的数组,sizeof(a) == sizeof(int) * 10
而int *p;定义了一个指向一个int的指针,sizeof(p) == sizeof(int*)
之所以会有数组就是指针的混淆是因为int [10]类型可以自动转换成int *类型,这种
转换被称为array-to-pointer conversion,所有左值(lvalue)表达式作为右值
(rvalue)使用的时候都要经过这种转换,而转换结果就是指向该数组首元素的指针,
所以a的值就相当于&a[0],但是即使赋值p=a也不能认为它们是一回事,比如
&a和&p的类型和值就完全不同,因为这里&a并不是将a当作右值来用所以array-to-
pointer
conversion不适用。
数组的数组就是字面上的意义:一个数组的每个元素都是数组。
比如int a2[5][10];定义了一个有5个int [10]元素的数组,sizeof(a2) ==
sizeof(int [10]) * 5,即sizeof(int) * 10 * 5
而指针数组int... 阅读全帖
B*******1
发帖数: 2454
22
来自主题: JobHunting版 - 问一个老数组题
似乎可以吧,刚测试了一下,没有发现不对啊。
测了这几个例子
72 cout << "-----------------Test 1--------------------" << endl;
73 int test1[] = {1, -2, 4, -2, 6, 7};
74 getSubArray(test1, sizeof(test1)/sizeof(int), 7);
75
76 cout << "-----------------Test 2--------------------" << endl;
77 int test2[] = {1,1,1,1,-1};
78 getSubArray(test2, sizeof(test2)/sizeof(int), 3);
79
80
81 cout << "-----------------Test 3--------------------" << endl;
82 int test3[] = {1,1,1,1,0,-1,0,-1,1};
83 getSubAr... 阅读全帖
z****u
发帖数: 104
23
字符串的 permutation 肯定是比较基础的题了,可是自己写了一下发现要 bug free
真心很难啊。调试了半天才 ok,而且程序看起来很臃肿,这要是在白板上铁定写不出
来啊
求大家指点一下该向哪个方向改进?
#include
#include
#include
char* insert(char* dst, int n, char c, int j)
{
/* Insert char c into string dst at location j */
n++;
dst = (char*) realloc(dst, sizeof(char) * n);
while(j < n)
{
char tmp = dst[j];
dst[j] = c;
c = tmp;
j++;
}
return dst;
}
char** permutation_recursive(char* s, int n, in... 阅读全帖
X*K
发帖数: 87
24
来自主题: JobHunting版 - find k missing numbers in range [0, N].
根据前面的提示写了一个
void missingK(int a[], int n, int k) {
int * bitmap = new int[n / sizeof(int) + 1];

for (int i = 0; i < n - k; ++i) {
int number = a[i];
int index = number / sizeof(int);
int offset = number - number / sizeof(int) * sizeof(int);
bitmap[index] |= 1 << offset;
}
for (int i = 1; i <= n; ++i) {
int index = i / sizeof(int);
int offset = i - i / sizeof(int) * sizeof(int);
if ((bitmap[index] & 1 << offset) == 0) {
... 阅读全帖
c***2
发帖数: 838
25
void tree_postorder_traversal_no_recursion (tree_node_type *root)
{
Element *top=NULL, *node, *tmp;
tree_node_type *current_node=root;

if (!root)
return;

/* push current_node into stack */
node=(Element *)malloc(sizeof(Element));
memset(node,0,sizeof(Element));
node->tree_node_ptr=current_node;
top=stack_push(top,node);

while(top) {
/* peek stack */
current_node=top->tree_node_ptr;

/* push current_node->left ... 阅读全帖
h********r
发帖数: 51
26
来自主题: JobHunting版 - Palantir面经
下面这个方法行不行? O(n), 模拟一次买卖的DP解法。大牛们看看对不对?
#include
#include
#include
void dump(int *x, int l) {

int i;
for (i = 0; i < l; ++ i) {

printf("%d ", x[i]);
}
printf("\n");
}
int main() {
int array[] = {3, 6, 8, 4, 5, 7, 9, 13, 15, 10, 6, 2, 7, 11, 8, 6};
int len = sizeof(array)/sizeof(int);
assert (len >= 4);
int *a = (int*)malloc(sizeof(int)*len);
int *b = (int*)malloc(sizeof(int)*len);
int *c = (int*)malloc(sizeof(... 阅读全帖
t****t
发帖数: 6806
27
According to standard 18.2:
size_t is implementation-defined *unsigned* integer type, large enough to
contain the *size in bytes* of any object.
ptrdiff_t is implementation-defined *signed* integer type that can hold the
difference of two subscripts in an array object.
So, basically by definition, size_t is the result type of "sizeof" operator;
ptrdiff_t is the result type of "-" operator when the operand are pointers.
However, sizeof ptrdiff_t is NOT necessary sizeof pointer. sizeof pointer is
... 阅读全帖
a*****g
发帖数: 19398
28
来自主题: Programming版 - 计算围棋棋盘合法图案的源代码
#!/usr/bin/env pike
// legal.pike - Count the number of legal go boards.
// Copyright 2005 by Gunnar Farneb?ck
// [email protected]
/* */
//
// You are free to do whatever you want with this code.
//
//
// This program computes the number of legal go board configurations
// for a rectangular board of a given size. It is efficient enough to
// handle boards up to 8x8 within minutes and up to 11x11 in less than
// 24 hours (on a fast computer). For rectangular boa... 阅读全帖
a*****g
发帖数: 19398
29
来自主题: Mathematics版 - 计算围棋棋盘合法图案的源代码
【 以下文字转载自 Programming 讨论区 】
发信人: ananpig (●○ 围棋数学一把抓的安安猪), 信区: Programming
标 题: 计算围棋棋盘合法图案的源代码
发信站: BBS 未名空间站 (Fri Jan 22 10:39:02 2016, 美东)
#!/usr/bin/env pike
// legal.pike - Count the number of legal go boards.
// Copyright 2005 by Gunnar Farneb?ck
// [email protected]
/* */
//
// You are free to do whatever you want with this code.
//
//
// This program computes the number of legal go board configurations
// for a rectangular board of a given size. It is ef... 阅读全帖
a9
发帖数: 21638
30
Index: channels/chan_gtalk.c
===================================================================
--- channels/chan_gtalk.c (revision 346898)
+++ channels/chan_gtalk.c (working copy)
@@ -481,7 +481,8 @@
break;
}
if (!strcasecmp(name, "error") &&
- (redirect = iks_find_cdata(traversenodes, "r
edirect")) &&
+ ( (redirect = iks_find_cdata(traverseno... 阅读全帖
w******1
发帖数: 520
31
这个例子
https://www.securecoding.cert.org/confluence/display/seccode/ARR01-C.+Do+not
+apply+the+sizeof+operator+to+a+pointer+when+taking+the+size+of+an+array
void clear(int array[], size_t len) {
for (size_t i = 0; i < len; i++) {
array[i] = 0;
}
}
void dowork(void) {
int dis[12];
clear(dis, sizeof(dis) / sizeof(dis[0]));
/* ... */
}
However, array has a pointer type because it is a parameter. As a result, sizeof(array) is equal to the sizeof(int *). For example, on an architecture
g*******s
发帖数: 490
32
来自主题: JobHunting版 - 网上c sample question的一堆错误
这边的c sample question
http://www.bestsamplequestions.com/technical-questions/c-sample-questions/c-sample-questions.html
列一下我发现的错误
第8
system dependent, be cautious of all sizeof questions, most of them are
system dependent
第10
system dependent, depends on the size of int
第11
compiler dependent
it's fine. 其实即使function的argument是数组形式,compiler也会自动改成指针,
just remember array is always
passed by its pointer in function calls
第16
won't print anything, 两个指针都没有allocate memory
第18
for declaration, it is not g... 阅读全帖
v***a
发帖数: 365
33
来自主题: JobHunting版 - 问个算法题
第一次写 c 程序,不保证正确,请自己 debug
程序假设单词是 a to z 组成
用的 bst counting, 然后 导出来 qsort
#include
#include
struct _node;
typedef struct _node {
int cc;
char c;
struct _node * n[26];
struct _node * fa;
} node;
void addToTree(node * root, node * r, const char * p1, const char * p2) {
int t;
int i;
if (p1 == p2) {
if (r->cc == 0) root->cc++;
r->cc++;
return;
}
t = (*p1) - (int)'a';
if (r->n[t] == NULL) {
r->n[t] = (node *... 阅读全帖
e*****e
发帖数: 1275
34
来自主题: JobHunting版 - 用BFS 和 inorder 重构二叉树?
我觉得这题得用queue.
随便乱写了点,运行没有问题:
typedef struct bfs_binary_tree_info
{
binary_tree * parent;
//true for left child, false for right child
bool left_flag;
int * inorder;
int m;
int BFS_index;
}bfs_binary_tree_info;
binary_tree * build_binary_tree_from_inorder_BFS2 ( int inorder[], int m,
int BFS[], int BFS_index )
{
//verify input, make sure they are all valid
int root = BFS[BFS_index];
int root_in_index = -1;
queue tree_queue;
int... 阅读全帖
d**********2
发帖数: 52
35
这个貌似比较简单吧。。
/*return 1 if s is anagram of t, otherwise return 0*/
int anagram(char s[], char t[]){
int checker[256];
int i;
memset(checker, 0, sizeof(checker));
for(i=0; i< sizeof(s)/sizeof(char), i++;){
checker[s[i]] ++;
}
for(i=0; i if(checker[t[i]] == 0) return 0;
}

return 1;
}
s****A
发帖数: 80
36
struct ST{
int *ptr;
size_t sz;
};
int main(){
ST* s;
s->sz = 7;
s->ptr = (int *)malloc((s->sz)*sizeof(int));
int allocsize = sizeof(ST) + (s->sz)*sizeof(int);
uint8 * buffer = (uint8 *)malloc(allocsize);
int offset = sizeof(ST);
memcpy(& buffer[offset], s->ptr, s->sz*sizeof(int));
*(int **)&buffer[(char*)&(s->ptr)-(char*)s] = (int *)&buffer[offset];
}
我大概知道好像是要分配一块连续的空间把struct和struct里指针指向的数组内容放在
一起,是这样吗?
但是最后一行code谁能讲一下大概什么意思?
特别是*(int **)&buf[...]和 (char*)&(s->p... 阅读全帖
l*********i
发帖数: 483
37
来自主题: Programming版 - 问个g++的问题
如下一段code:
int PrecFunc(void *params)
{
double m1 = *(double *)params;
double m2 = *(double *)(params+sizeof(double));
double M = *(double *)(params + sizeof(double)*2);
double mu = *(double *)(params + sizeof(double)*3);
double eta = *(double *)(params + sizeof(double)*4);
double m = *(double *)(params + sizeof(double)*5);
... ...
}
如果存成.c文件用gcc编译可以通过,但是存成.cpp文件用g++编译就会说
"error: pointer of type ‘void *’ used in arithmetic"
因为用到现有的一个函数库,PrecFunc的输入参数类型必须是void *,应该
怎么样修改才能在gcc和g++下编译都通过呢?
M*m
发帖数: 141
38
it seems that if I do
int main(){
int a[] = {12, 23, 121};
//sizeof(a) returns 12 = 3*sizeof(int)
}
as stated in the sizeof definition, we can do sizeof of an object or a type.
so the question is
1. how does sizeof work for an array object,
2. how do we pass the array to a function as an object. so that we can get
the size in the same way as we do here.

or
g*********s
发帖数: 1782
39
来自主题: Programming版 - 有没有现成的模拟fread的bufferRead()?
在做IO优化。原来有大量fread操作,现在想精简成一次读到一个buffer里,从里面取
出data。有没有这样现成的库函数啊?
C++的binary stream应该可以把?但上次有个讨论说C++的binary stream比较慢?
原来的code:
for (int i = 0; i < count; i++) {
int x;
fread(&x, sizeof(int), 1, input_fp);
long y;
fread(&y, sizeof(long), 1, input_fp);
}
现在想改成:
void** buffer = new char[count];
fread(*buffer, sizeof(char), count, input_fp);
for (int i = 0; i < count; i++) {
int x;
bufferRead(&x, sizeof(int), 1, buffer);
long y;
bufferRead(&y, sizeof(long), 1, buffer)
J**********y
发帖数: 1891
40
class A
{
public:
int a;
};
class B
{
public:
int a;
B(int aa=0)
{
a=aa;
}
~B(){};
};
class C: public A, public B
{
//public:
// C(int cc):B(cc)
// {
// };
};
int _tmain(int argc, _TCHAR* argv[])
{
char aa[100]="0123456789";
cout << sizeof(func()) << endl;
cout << sizeof(int) << endl;
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
A a;
B b;
C c;
cout << a.a << endl;
cout << b.a ... 阅读全帖
d****i
发帖数: 4809
41
来自主题: Programming版 - serialization 到底该怎么理解啊?
一个简单的方法来判断
#include
struct A {
int a;
short b;
char c;
};
#pragma pack (1)
struct B {
int a;
short b;
char c;
};
int main(int argc, char *argv[])
{
int size_A = sizeof(struct A), size_B = sizeof(struct B);
int size_sum = sizeof(int) + sizeof(short) + sizeof(char);

printf("siza_A=%d, size_B=%d, size_sum=%d\n", size_A, size_B, size_sum);
if(size_A > size_sum && size_B == size_sum) {
printf("The compiler pads the struct to be byte aligne... 阅读全帖
h*****2
发帖数: 114
42
来自主题: JobHunting版 - 我在微软做interviewer的经验
#include
using namespace std;
int main(void)
{
bool isOdd(false);
int A[] = {2,5,7,8,9};
int B[] = {1,3,4,7,9,10};
int m=sizeof(A)/sizeof(int), n=sizeof(B)/sizeof(int);
int Half_Place;
if((m+n)%2 == 1)
{
isOdd = true;
Half_Place = (m+n)/2+1;
}
else
Half_Place = (m+n)/2;
int i=0, m_temp(0), n_temp(0);
int Value;
int current_A, current_B;
while(i < Half_Place)
{
if(A[m_temp] < B[n_temp])
{
j**l
发帖数: 2911
43
写成sizeof(integers)/sizeof(int)还是错的。函数has_num的第一个参数是int *
integers,是把它当指针而不是数组名处理的,这样sizeof(integers)的结果总是4。
如果这样写
int integers[100];
那么sizeof(integers)的结果就是400了
如果是我,就把数组大小用参数传入
bool has_num(int integers[], int n, int target)
UD
发帖数: 182
44
来自主题: JobHunting版 - one amazon interview problem
below code seems to work, and should be O(n) time and O(1) space, if you disagree, please list your argument.
The idea behind is the use and resue of sum(i,j), that's why the O(1) space.
sum(i,j)=sum of all the 1s between i and j.
sub-seq(i,j) has equal 1s and 0s when sum(i,j)*2=j-i+1, we start by setting i=0,j=A.length-1, then searching from both ends.
1. if sum(i,j)*2==j-i+1, then found it
2. else if sum(i,j)*2>j-i+1, we have more 1s then 0s, then we check A[i] and A[j]:
a. if A[i]==A[j], t... 阅读全帖
L*******e
发帖数: 114
45
来自主题: JobHunting版 - 砸了面试,发面题
骑驴找驴,砸了面试,潜心修行,明春再战。
//1. what is the output of the following program?
class A{
public:
struct HEADER{
int a_;
double b_;
unsigned int c_;
unsigned char d_;
static int SIZE = 100;
}header;
private:
double k;
};
int main()
{
A a;
cout << "sizeof A: " << sizeof(a) << endl;
cout << "sizeof structure: " << sizeof(a.HEADER) << endl;
}
//2. What is the output of the following program?
void print(int a, int b)
{
cout<<"value a: " << a
r*******e
发帖数: 7583
46
来自主题: JobHunting版 - 请教几个电面题
You passed a wrong size of the array and accessed memory out of boundary
int arr[8] = {3, 2, 7, 4, 4, 6, 1, 5};
...
len = remove_duplicate(arr, sizeof(arr));
When sizeof is applied to an array, the result is the size in bytes of the
array in memory. Thus, it should be changed to:
len = remove_duplicate(arr, sizeof(arr)/sizeof(int));
b********n
发帖数: 29
47
来自主题: JobHunting版 - 问个算法题
#define N 4
void FindWordsBoggle(char board[][N])
{
int i=0,j=0;
char* outStr = (char*) malloc((N*N+1)*sizeof(char));
int* occupMap = (int*) calloc((N*N),sizeof(int));
int level = 0;
RecursiveBoggle(board, outStr, level, occupMap);
free(occupMap);
free(outStr);
}
void RecursiveBoggle(char board[][N], char* outStr, int level, int*
occupMap)
{
int* occupMapCopy = (int*) malloc((N*N)*sizeof(int));
memcpy(occupMapCopy, occupMap, N*N*sizeof(int));
int i=0,j=0;
for ( ; i < N; i++... 阅读全帖
s*****y
发帖数: 897
48
来自主题: JobHunting版 - 求一题的完美简洁解答
can you give me your input to test.
I extend your solution in finding kth smallest element without calling it
two times.
I test
1. {1} {2}
2. {1}, {2,3}
3, {1,2}, {3,4}
4 {1,2} {4,5,6}
#define MAX(A,B) ((A) > (B) ? (A):(B))
#define MIN(A,B) ((A) < (B) ? (A):(B))
static int A[] = {2};
static int B[] = {1};
/* Find the Kth smallest element of sorted array A and B */
double Find_Kth_Smallest(int A[], int len1, int B[], int len2, int k, bool
divide)
{
int i, j;
int Ai, Ai_1, Bj, Bj_1; /* Ai_... 阅读全帖
k****n
发帖数: 369
49
来自主题: JobHunting版 - G题讨论

int *p;
int *sz;
void init() {
p = (int *) malloc(sizeof(int)*(MAX_INT));
sz = (int *) malloc(sizeof(int)*(MAX_INT));
memset(p, 0, sizeof(int)*MAX_INT);
memset(sz, 0, sizeof(int)*MAX_INT);
}
void clean() {
free(p); free(sz);
}
int find(int x) {
if (x == p[x]) return x;
p[x] = find(p[x]);
return p[x];
}
int union(int x, int y) {
int rx = find(x);
int ry = find(y);
if (rx == ry) {
return sz[rx];
} else if (sz[rx] > sz[ry]) {
p[ry] = rx;
sz[rx] += sz[ry];
re... 阅读全帖
k****n
发帖数: 369
50
来自主题: JobHunting版 - G题讨论

int *p;
int *sz;
void init() {
p = (int *) malloc(sizeof(int)*(MAX_INT));
sz = (int *) malloc(sizeof(int)*(MAX_INT));
memset(p, 0, sizeof(int)*MAX_INT);
memset(sz, 0, sizeof(int)*MAX_INT);
}
void clean() {
free(p); free(sz);
}
int find(int x) {
if (x == p[x]) return x;
p[x] = find(p[x]);
return p[x];
}
int union(int x, int y) {
int rx = find(x);
int ry = find(y);
if (rx == ry) {
return sz[rx];
} else if (sz[rx] > sz[ry]) {
p[ry] = rx;
sz[rx] += sz[ry];
re... 阅读全帖
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)