由买买提看人间百态

topics

全部话题 - 话题: sizeof
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
Y********f
发帖数: 410
1
来自主题: JobHunting版 - heap里面delete一个非root的节点
测试过的代码:
#include
#include
using namespace std;
void deleteNode(int heap[], int n, int pos)
{
// put the last elements to where the node removed
heap[pos] = heap[n-1];
// adjust elements above pos
int i = pos;
while(i != 0 && heap[i] < heap[(i - 1) / 2])
{
swap(heap[i], heap[(i - 1) / 2]);
i = (i - 1) / 2;
}
// adjust elements below pos
i = pos;
while(true)
{
if (2 * i + 1 > n -2)
{
//al... 阅读全帖
i**********e
发帖数: 1145
2
来自主题: JobHunting版 - 从水木上看到个数组题
Already found a bug in your code.
--> int length=sizeof(input)/sizeof(int);
This will always set lenght as 1. Because input is an int* pointer that's
passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
i**********e
发帖数: 1145
3
来自主题: JobHunting版 - 从水木上看到个数组题
Already found a bug in your code.
--> int length=sizeof(input)/sizeof(int);
This will always set lenght as 1. Because input is an int* pointer that's
passed into the function, therefore sizeof(input) = sizeof(int*) = 4.
q******0
发帖数: 15
4
来自主题: JobHunting版 - 请教C/C++小
See the code below. I am confused that "len = 5" from main function, "len =
4" from RemoveDuplicate function. When does terminate '\0' count? Thanks.
#include
using namespace std;
void RemoveDuplicate(char str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
int main()
{
char a[] = "abcd";
int len = sizeof(a)/sizeof(a[0]);
cout << "len = " << len << endl;
RemoveDuplicate(a);
return 0;
}
i**********e
发帖数: 1145
5
来自主题: JobHunting版 - 请教C/C++小
Try this and figure out why:
void foo(int str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
int main()
{
int a[] = {1,2,3,4};
int len = sizeof(a)/sizeof(a[0]);
cout << "len = " << len << endl;
foo(a);
return 0;
}
w****x
发帖数: 2483
6
来自主题: JobHunting版 - 谷歌面经
struct NODE
{
string str;
NODE* pLft;
NODE* pRgt;
NODE(const char* szStr = "") : str(szStr), pLft(NULL), pRgt(NULL) {}
};
void serialize(NODE* pNode, char*& p)
{
if (p == NULL) return;
if (pNode == NULL)
{
*((int*)p) = 0;
p += sizeof(int);
return;
}

int nLen = pNode->str.length();
*((int*)p) = 1;
p += sizeof(int);
strcpy(p, pNode->str.c_str());
p += nLen+1;

serialize(pNode->pLft, p);
serialize(pNode->pRgt... 阅读全帖
h****n
发帖数: 1093
7
来自主题: JobHunting版 - 请教一个指针的面试题
我特意跑了一下程序
void foo(int *p1, int val1, int val2)
{
int j;
int *p2, *p3;
int * buf1 = (int*)malloc(sizeof(int)*20);
for(int j =0;j<20;j++)
buf1[j] = 1;
p2 = (int *)buf1;
//val1放到0x12fec8地址也就是当前p2指针指向的位置
*p2 = val1;
//把p1指向val1地址所指向的位置
p1 = (int *)*p2;
for (int i=1; i<10; i++)
{
p2 = (int *)buf1;
*p2 = val1;
p3 = (int *)*p2;
val1+=sizeof(int);
*p3 = val2;
}
printf("buf1:");
for(j =0;j<20;j++)
... 阅读全帖
w****x
发帖数: 2483
8
来自主题: JobHunting版 - 做了一下Google的切木头
后来想了一下是不是可以用greedy算法来做,仔细分析了一下发现不可行,果然写了一
个不能得到最优解。
int _inner_greedy(int a[], int n)
{
if (n <= 2)
return 0;
int nMinIndex = 1;
for (int i = 1; i < n-1; i++)
{
if (abs((a[i] - a[0]) - (a[n-1] - a[i])) < abs((a[nMinIndex] - a[0])
- (a[n-1] - a[nMinIndex])))
nMinIndex = i;
}
return a[n-1] - a[0] + _inner_greedy(a, nMinIndex+1) + _inner_greedy(a+
nMinIndex, n-nMinIndex);
}
int getMinCostGreedy(int a[], int n)
{
if (NULL == a || n <= 1)
... 阅读全帖
w****x
发帖数: 2483
9
来自主题: JobHunting版 - 做了一下Google的切木头
后来想了一下是不是可以用greedy算法来做,仔细分析了一下发现不可行,果然写了一
个不能得到最优解。
int _inner_greedy(int a[], int n)
{
if (n <= 2)
return 0;
int nMinIndex = 1;
for (int i = 1; i < n-1; i++)
{
if (abs((a[i] - a[0]) - (a[n-1] - a[i])) < abs((a[nMinIndex] - a[0])
- (a[n-1] - a[nMinIndex])))
nMinIndex = i;
}
return a[n-1] - a[0] + _inner_greedy(a, nMinIndex+1) + _inner_greedy(a+
nMinIndex, n-nMinIndex);
}
int getMinCostGreedy(int a[], int n)
{
if (NULL == a || n <= 1)
... 阅读全帖
j*****y
发帖数: 1071
10
如果是 定义 成 int A[10][10]的形式的话
可以用 sizeof(A[0]) / sizeof(int) 得到列数
sizeof(A) /sizeof(int) 得到总数
b******7
发帖数: 92
11
数组作参数要带上长度
void func(int a[],size_t len)
c/c++不能直接用参数传递数组,而将其转化为指针传递
上面的定义等价于void func(int * a,size_t len)
测试如下的case就可知道
void func(int a[], size_t len)
{
cout< }
int main()
{
int a[] = {1,2};
cout< func(a,sizeof(a)/sizeof(int));
return 0;
}
r***e
发帖数: 29
12
来自主题: JobHunting版 - BBC 编程风格面试题,求指教
最后个人标准答案
#ifndef _ROMON_HEADER_
#define _ROMON_HEADER_
#define _DEBUG_
#include
#include
//ROMON digits
const std::string ROMON_DIGITS[] = {"I","IV","V","IX", "X","XL","L","XC","C"
,"CD","D","CM","M" };
//ROMON scale
const int ROMON_SCALE[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
1000};
const int ROMON_MAX = 3999;
const int ROMON_MIN = 1;
class RomanNumeralGenerator
{
public:
virtual std::string generator(int num) = 0;
};
class CRoman : public RomanNumeralGene... 阅读全帖
b******7
发帖数: 92
13
#define BLOCK_SIZE 1024
#define ALIGN(size,unit) (((size)+(unit-1))/(unit)*(unit))
struct ChunkHead{
ChunkHead * next;
};
#define CHUNK_SIZE ALIGN(sizeof(ChunkHead) + BLOCK_SIZE, sizeof(int))
ChunkHead * firstChunk;
void initChunk(void * addr, size_t size)
{
firstChunk = NULL;
Chunk * pre = NULL;
for(int i = 0; i < size/CHUNK_SIZE; i++)
{
ChunkHead * cur = (ChunkHead *)(addr + i* CHUNK_SIZE);
if( pre != NULL)
... 阅读全帖
R*****i
发帖数: 2126
14
来自主题: JobHunting版 - 一道老题目, 求最快捷解法
这个题目建议用红黑树做可能最快捷有效。红黑树每一次插入,删除都是log(k),求平
均值非常简单,就是根部的那个节点。所以总的计算量是O(nlog(k))。
以下是c/c++代码(需要修改数组,以便一个一个输入)。
#include
#include
#include
#define INDENT_STEP 4
enum rbtree_node_color {RED, BLACK};
typedef struct rbtree_node_t {
int value;
struct rbtree_node_t* left;
struct rbtree_node_t* right;
struct rbtree_node_t* parent;
enum rbtree_node_color color;
} *rbtree_node;
typedef rbtree_node node;
typedef enum rbtree_node_color color;
typedef struct... 阅读全帖
p****o
发帖数: 46
15
来自主题: JobHunting版 - 狗家面经
cool. 什么编程语言?第二题, 上个c++:
using namespace std;
list intersect(list intList1, list intList2){
list::const_iterator it1 = intList1.begin();
list::const_iterator it2 = intList2.begin();
list intList3;
while((it1 != intList1.end()) && (it2 != intList2.end())) {
if (*it1 == *it2) {
intList3.push_back(*it1);
++it1;
++it2;
} else if (*it1 < *it2) {
++it1;
... 阅读全帖
s******d
发帖数: 424
16
来自主题: JobHunting版 - 一道onsite面试题
5个里面选三个
#include
#include
#include
#include
#include
int main()
{
int values[] = { 1, 2, 3, 4, 5};
int elements[] = { 1, 1, 1, 0, 0};
const size_t N = sizeof(elements)/sizeof(elements[0]);
assert(N == sizeof(values)/sizeof(values[0]));
std::vector selectors(elements, elements + N);
int count = 0;
do
{
std::cout << ++count << ": ";
for (size_t i = 0; i < selectors.size(); ++i)
{
if (selectors[i])
{
... 阅读全帖
H**********5
发帖数: 2012
17
后天一个on site,
刷了3天的C语言,
遇到这个题,感觉很有趣,
写一个统一适用的swap:# define swap(x,y) /
{ void *_tmp=malloc(sizeof(x)); /
void *_x=&x; void *_y=&y; /
memcpy(tmp,_x,sizeof(x));/
memcpy(_x,_y,sizeof(y));/
memcpy(_y,_tmp,sizeof(x));}
这个表面上看上合理,
但一遇到含有指针元素的结构体,感觉就挂了吧,
请问有没有办法实现这种任何任意种类元素的swap,用C实现。
i*****o
发帖数: 105
18
来自主题: JobHunting版 - 一道google面经难题
#include
#include
int x[10][10] = {
{ 1, 1, 1, 1, 1, 1, 5, 1, 1, 1,},
{ 1, 1, 1, 1, 1, 1, 4, 1, 1, 1,},
{ 1, 1, 1, 1, 1, 1, 8, 1, 1, 1,},
{ 1, 1, 1, 1, 5, 4, 10, 1, 1, 1,},
{ 1, 1, 1, 1, 4, 1, 10, 10, 1, 1,},
{ 1, 1, 1, 1, 5, 5, 10, 10, 10, 10,},
{ 1, 1, 5, 6, 1, 5, 11, 1, 1, 1,},
{ 1, 1, 3, 1, 1, 1, 10, 1, 1, 1,},
{ 1, 2, 2, 1, 1, 1, 1, 1, 1, 1,},
{ 2, 1, 10, 10,... 阅读全帖
t*****d
发帖数: 525
19
来自主题: JobHunting版 - 老码农骂小码农的强文。 (转载)
【 以下文字转载自 PDA 讨论区 】
发信人: weidong (伊拉克学习小组副组长), 信区: PDA
标 题: 老码农骂小码农的强文。
发信站: BBS 未名空间站 (Mon Nov 2 19:52:49 2015, 美东)
Linus Tovalds骂瞎写代码的小码农,荡气回肠,应该下发所有码农学习。
Christ people. This is just sh*t
.The conflict I get is due to stupid new gcc header file crap. But what
makes me upset is that the crap is for completely bogus reasons.
This is the old code in net/ipv6/ip6_output.c:
mtu -= hlen + sizeof(struct frag_hdr);
and this is the new “improved” code that uses fancy stuff that wants
magical built-in... 阅读全帖
a****o
发帖数: 686
20
来自主题: CS版 - sorting问题求教。 (转载)
O(N+XlogX) and no extra space is required.
your asymptotic upper limit is either O(N) or O(XlogX), depends on ratio of X/N.
//
#include
#include
#include
#include
#include
using namespace std;
template
void top_X_elements(vector& v, int X){
nth_element( v.begin(),v.begin()+X, v.end(),greater() );//O(N)
sort(v.begin(), v.begin()+X);//O(XlogX)
}
int main(int argc, char* argv[]){
int a[]={1,2,3,4,5,6,7,8,9,10,11,12};
ran... 阅读全帖
r****y
发帖数: 26819
21
来自主题: Linux版 - 看看这个 C 代码
not exactly original but can simulate:
/*
* sizeof.c - Implementation of sizeof operator
* http://faq.zanvar.in
*/
#include
#include
/* Find the size of an variable */
#define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var)))
/* Find the size of a data type */
#define sizeof_type( type ) (size_t)((type*)1000 + 1 )-(size_t)((type*)1000)
int main ( void )
{
int a;
int b[10];
printf ( "%lu\n", sizeof ( b ) );
printf ( "%lu\n", sizeof ( b+0 ) );
printf ( "%lu\n", sizeof_va
Q**g
发帖数: 183
22
来自主题: Programming版 - a simple question for C++ class
whenever you pass an array as an argument to a function call, it is actually
treated as a pointer. The size information is lost to the function.
try this simple one:
#include
using namespace std;
void f(char *array[]){
cout<<"in function, sizeof(array)="< }
int main(){
char *a[20] = {"abc","def","ghik"};
cout<<"in main, sizeof(a)="< f(a);
return 0;
}
c*****i
发帖数: 25
23
来自主题: Programming版 - 初学C,对什么该free一直搞不明白
我知道这些问题显得都很笨。请表拍我。谢谢您的指教。
比如说下面这段code
struct a {
char *b;
int c;
};
int n, m, i;
struct a *pa = (struct a*)malloc(n, sizeof(struct a));
for(i = 0; i < n; i++) {
pa[i].b = (char*)malloc(m, sizeof(char));
}
//需要把每一个pa[i].b都free吗?
for(i = 0; i < n; i++) {
free(pa[i].b);
}
//还是只有这句就够了?
free(pa);
还有一段code: :)
char **s;
int n, m, i;
s = (char**)malloc(n, sizeof(char*));
for(i = 0; i < n; i++) {
s[i] = (char*)malloc(m, sizeof(char));
}
// 需要把每一个s[i]都free吗?
for(i = 0; i < n; i++) {
fre
d*******d
发帖数: 2050
24
来自主题: Programming版 - 来,出个题
非常非常简单。
先自己写下结果,然后运行试试。
void f(char a[]){
cout << sizeof(a) << endl;
cout << sizeof(*a) << endl;
}
main(){
char a[10];
cout << sizeof(a) << endl;
cout << sizeof(*a) << endl;
f(a);
}
g*********s
发帖数: 1782
25
来自主题: Programming版 - STL map变量的实际memory usage估算
查了源代码。_Rb_tree_node_base确实有alignment问题。另外Rb_tree还有一个size_type的字段,一个compare字段。
所以sizeof(map<>) = sizeof(_Rb_tree_node_base) + sizeof(size_type) + sizeof(void *) = 32 + 16 = 48.

16。
s***e
发帖数: 122
26
来自主题: Programming版 - ask a question about struct in C programming
看来是因为这一句:
fread(&sacbin, sizeof(header), 1, fpr);
要改成
fread(&sacbin, sizeof(struct header), 1, fpr);
如果你是用C编译器的话。
当然你也可以定义struct header为typedef struct header {...} HEADER; 那样你就可以用sizeof(HEADER)了。
下面这段代码是可以编译运行的。
#include
struct header {
int hd;
};
struct sac
{
struct header hdr;
float * data;
};
void test2() {
const char * const fn = "test2.txt";
FILE* fpw = fopen(fn, "w");
struct header hdr;
hdr.hd = 18;
fwrite(&hdr, sizeof(hdr), 1, fpw);
fclose(fpw
c**m
发帖数: 30
27
来自主题: Programming版 - C++ Interview Question
that's obvious.
but question asks for difference behind the scene, no mention of anything
specific about memory p1 pointed to and p2 (candidate is supposed to figure
that out). and there're very big differences depending on where they appear
. besides, there's nothing stopping me from making var a pointer to an
integer at location 5:
int var =5;
*((int*)var) = 5;
also, I always thought sizeof(int) == sizeof(void*) but apparently not
because visual studio 64-bit sizeof(int) == 4 and sizeof(voi
i****d
发帖数: 255
28
来自主题: Programming版 - 再问:关于多维数组的malloc
// 定义二维数组的指针
double **zzz;
int i, j, irow = 100, icol = 4;
// 有两种方式malloc
// 第一种
zzz = (double**) malloc( sizeof(double) * irow * icol) );
for(i=0; i zzz[i] = (double *) malloc( sizeof(double) * icol) );
// 第二种
zzz = (double**) malloc( sizeof(double*) * irow) );
for(i=0; i zzz[i] = (double *) malloc( sizeof(double) * icol) );
*******************************************
感觉第二种方式好理解:先定义一个指针数组zzz[irow];然后让里面的各个指针指向
一个个一维数组,即zzz[0]存储第0列的首地址,等等。
但第一种方式不好理解:感觉开辟了两份内存。
请指教!
l*******G
发帖数: 1191
29
来自主题: Programming版 - 再问:关于多维数组的malloc
can we use the second method to define multiple-dimension arrays (>2)?
i.e. something like
int idim1=100;
int idim2=200;
int idim3=300;
int idim4=10;
zzz = (double**) malloc( sizeof(double*) * idim1) );
for(i=0; i zzz[i] = (double **) malloc( sizeof(double) * idim2) );
for(j=0; j zzz[i][j]=(double **) malloc( sizeof(double) * idim3) );
for(k=0; k zzz[i][j][k]=(double **) malloc( sizeof(double) * idim4) );
}
}
}
v****s
发帖数: 1112
30
来自主题: Programming版 - 怎么得到char *分配空间的大小?
const int MAX = 500;
char *src = (char*)malloc(MAX*sizeof(char));
for( int i = 0; i printf("%d\n",sizeof(src));
结果是4,而不是500,而用 char src[MAX] 就可以得到这个正确的sizeof(src)。
char *src 和 char src[] 都是分配在heap上面的么? sizeof()函数具体是怎么实现
的?
谢谢!!
y**b
发帖数: 10166
31
来自主题: Programming版 - char s[]和char *ps的不同
void func() {
char s[]="abc"; // 在stack上分配空间存储"abc",sizeof(s)等于4
int i[]={1,2,3}; //同上, sizeof(i)等于12
char *ps="def"; // 在常量区存储"def",sizeof(ps)==8
int *pi=new int[3]; // 在heap分配空间存储,sizeof(pi)==8
delete [] pi;
}
以上对否?
这个常量存储区同初始化数据段/非初始化数据段是什么关系?
看网上有人把c/c++内存布局描述如下:
1、栈区(stack)
2、堆区(heap)
3、全局区(静态区):初始化数据段/非初始化数据段
4、文字常量区
5、程序代码区
l***y
发帖数: 4671
32
结果:
sizeof(int(*)[1000][1000][1000]): 8
sizeof(double(*)[1000][1000][1000]): 8
sizeof (int(*)): 8
sizeof (double(*)): 8
t*******s
发帖数: 7
33
#include
#include // malloc ,free
#include //memcpy
typedef double data_t ;
typedef struct {
size_t dims; //number of dimensions
size_t *dim_define; //dim1=10, dim2=10, dim3=20, etc.
data_t *data; //one dimension storage
size_t size ;//size of (data) -optional
}matrix_t ;
matrix_t * matrix_new( size_t dims, size_t *dim_define)
{
size_t i, size=1;

for(i=0; i size*=dim_define[i];
}
matrix_t *ret=malloc( siz... 阅读全帖
d****n
发帖数: 233
34
来自主题: JobHunting版 - how to solve this google interview question
I think the optimization in Kwn's solution could have bugs. Here is mine
without any optimization:
int FindSubSeqWithMaxMinDistance(int *X, int N, int K)
{
if ( N < 2 || K < 2 || K > N ) return 0;
// Alloc buffers
int **F = (int **)malloc(K*sizeof(int *)+ K*N*sizeof(int));
int *temp = (int *)(F + K);
for (int i = 0; i < K; i++)
{
F[i] = temp + N*i;
}
memset(temp, 0, K*N*sizeof(int));

for(int j = 0; j < N; j++)
{
F[0][j] = INT_MAX;
}
// Assume A
w******1
发帖数: 520
35
写函数实现一个数组找2个数的和是一个固定值。
code:bool has_num(int *integers, int target)
{
for (int i=0;i {
if(num_hashtable(target-*(integers+i))
{
return true;
}
}
return false;
}
====================
1. 边界条件是否需要做个判断呢? 比如*integers 如果是NULL? int target 如果长
度小于等于0?
2.
sizeof(char *) = 4 bytes 或者2 BYTES 或者其他数字,根据不同的编译环境。
sizeof(integers) 可能得到的结果不是你想要的, 结果也许就是指针的SIZE =4
h*****g
发帖数: 944
36
来自主题: JobHunting版 - C++里get array size的问题
C++里是不是现在没有现成的get array size/length的function??
如果没有的话,我在网上看到了一个code
#include
template char (&array(T(&)[N]))[N];
#define length(a) (sizeof a / sizeof a[0])
int main()
{
int a[10];
std::cout << sizeof array(a) << '\n';
std::cout << length(a) << '\n';
}
请问这个code对所有的data type都work吗? 我试试了,好象是可以的。中间有什么错
误吗?
c**********e
发帖数: 2007
37
来自主题: JobHunting版 - C++ Q21: size of virtual table
#include
class A {
public: virtual ~A() {}
};
class B : public A {
public:
virtual void f() {}
};
Referring to the sample code above, on a typical platform that uses virtual
tables to implement virtual function lookup, what do you expect "sizeof(B)"
to be?
a) 4
b) sizeof(void *)
c) 2*sizeof(void *)
a****n
发帖数: 1887
38
来自主题: JobHunting版 - C++问题3
5.
memset (str,0,sizeof(str));
memset (str,1,sizeof(str));
memset (str,0xff00,sizeof(str));
有没有问题?
这三个语句初始化后的内存是什么样子的
w**********y
发帖数: 1691
39
来自主题: JobHunting版 - [C++问题]请教关于几种size of class
////////////////////////////
class A{ }; A a;
那么 sizeof(a) is 1;
class X{
void doNothing(){}
char a;
};
sizeof(x) 咋还是1,不是 1+1 =2 呢?
////////////////////////////
class Y{
virtual void doNothing(){}
};
sizeof(y) is 4..这个应该是指向virtual table的地址大小吧?
class Y{
virtual void doNothing(){}
virtual void doNothing2(){}
char a;
};
为啥是8不是4+1=5啊? 而且再添加一个member: char b..还是8..
如果换成char* b就成了12...
如果换成char a[2],结果是8
如果换成char a[10],结果是16
非常不理解..
z****o
发帖数: 78
40
来自主题: JobHunting版 - Google及其它面经 (长,慎入)
存储city之间的距离的题目,可以有一下两个假设:
1. city1 到 city2 = city2 到 city1
2. city1 到 city2 距离是非负的。
这样的话,可以计算 cityi 到 cityj 的最短路径的边集S(i,j) = { e1,e2,e3..}
S = S(0,1) U S(0,2) U S(0,3) ..... U S(1,2) U S(1,3) U ..... U S(i,i+1) U S
(i,i+2) U ...
最后存下来S就可以了。这种存法的空间是 |S|*( 2*sizeof(int) + 1* sizeof(double
) );
这个空间去和 ((n-1)+(n-2)+(n-3)+...+2+1)*sizeof(double) 的矩阵存法比较一下,用
空间使用比较省的一种来存。|S|够小的时候用第一种比较好,比如所有的city在一条
线上。
|S|不小的时候用第二种比较省,比如city均匀分布在圆上。
a***r
发帖数: 93
41
memory O(n)
time O(nlogn)
//~ Given a array,find out if there exist a subarray such its sum is zero
#include
#include
using namespace std;
static void swap(int *p, int i, int j) {
int t=p[i]; p[i]=p[j];p[j]=t;
}
static int partition(int *p,int left,int right) {
int j=left;
for(int i=left;i if(p[i] }
swap(p,j,right);
return j;
}
static void binary_sort(int *p, int left, int right) {
if(left>=r... 阅读全帖
a******u
发帖数: 239
42
来自主题: JobHunting版 - c interview question
I think my answers to Question 7 and 8 are correct, but they said they are
not correct, why?
I already tested the code by Visual C++ (c compiler).
Thank you very much.
Line in file Contents
30 int * someIDs, theFirst, *r;
110 someIDs =GetSomeIDs(); /* defined below */
111 theFirst = someIDs [0];
112 r= ReorderIDs(someIDs);
113-150 /* we want to use ‘theFirst’ and ‘r’ here*/

499 /*-------- GetSomeIDs-----*/
500 int * GetSomeIDs()
501 {
502 int ids[8];
503-5... 阅读全帖
r*****k
发帖数: 1281
43
来自主题: JobHunting版 - 请教C/C++小
void RemoveDuplicate(char str[])
{
int len = sizeof(str)/sizeof(str[0]);
cout << "len = " << len << endl;
}
这上面这段code中,sizeof(str)是计算“the size of the pointer represented by
the array identifier”, which is always 4.
a**U
发帖数: 115
44
来自主题: JobHunting版 - 一个C++的问题!
我也打印出来,sizeof(A)=4, sizeof(B)=4, sizeof(C) = 8, 我用的ubuntun, 编译是
g++ cast.cpp 出来 a.out. 运行a.out,没有问题。奇怪的很!
g****e
发帖数: 172
45
char *words[] = {"stately", "plump", "buck", "mulligan"};
// calculate how many elements in words
size_t words_size = sizeof(words)/sizeof(char *);
为什么不是直接 size_t words_size = sizeof(words)?
d*******u
发帖数: 186
46
来自主题: JobHunting版 - how to implement malloc?
From c programming language:
typedef long Align; /* for alignment to long boundary */
union header { /* block header */
struct {
union header *ptr; /* next block if on free list */
unsigned size; /* size of this block */
} s;
//The Align field is never used; it just forces each header to be
aligned on a worst-case boundary.
Align x; /* force alignment of blocks */
};
typedef union header Header;
static Header base; /* empty list to get started */
static Header *fr... 阅读全帖
f*****7
发帖数: 92
47
我个人不喜欢swap的方法,自己没法理解
排序,然后跳过重复元素
希望能帮上你
class Solution {
public:
vector > permuteUnique(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
int len = num.size();
int* path = (int*) malloc(sizeof(int) * len);
bool* visited = (bool*) malloc(sizeof(int) * len);
memset(visited, false, sizeof(bool) * len);
vector > bigList;
int... 阅读全帖
b*******7
发帖数: 907
48
来自主题: JobHunting版 - 几个C语言的题目
1. what's the output? why?
#include
#include
int main()
{
while(1)
{
fprintf(stdout,"hello-std-out");
fprintf(stderr,"hello-std-err");
sleep(1);
}
return 0;
}
2. what's the output?
#include
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", (int)a);
printf("%d\n", *(int *)&a);
return 0;
}
3. can it be built? if yes, what's the result?
file1.c:
int arr[80];
file2.c:
extern int *arr;
int main(... 阅读全帖
r*******n
发帖数: 3020
49
来自主题: JobHunting版 - 请教一个题,不太容易,要O(n)
可以,
result = []
1 left, right = partition // the elements in right > that of left
2 if sizeof(right) > k
drop the left, recursively call on the right.
else if sizeof(right) < k
k = k - sizeof(right)
append the right to the result.
recursively call on the left.
else
append the right to the result.
the end.
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)