由买买提看人间百态

topics

全部话题 - 话题: sizeof
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
I**********s
发帖数: 441
1
来自主题: Programming版 - 请教一个C的关于调用matrix的问题
use dynamic allocation.Example:
#define E_TYPE int
E_TYPE ** matrix(int h, int w) {
E_TYPE ** p;
int i, j;
p = (E_TYPE **) malloc(sizeof(E_TYPE *) * w);
for (i = 0; i < w; i ++) {
p[i] = (E_TYPE *) malloc(sizeof(E_TYPE) * h);
}
return p;
}
m******t
发帖数: 4077
2
来自主题: Programming版 - 问个简单的memory allocation 的问题。
1, 以下小程序里没有给b allocate space, 为什么编译器不报错呢?g++
2, a[5]到底在内存里面占几个字节?是5个吗?但是最后一个已经给了 '\0', 对吗?
3, a[5]是在stack中还是在heap中啊?如果用new 给b分配地址的话是在heap中,对吧?
#include
using namespace std;
int main()
{
char * b;
char a[5] = "1234";
strcpy(b, a);
cout << a < cout << b << endl;
cout << "size of b " << sizeof(b)< cout << "size of a " << sizeof(a)< return 0;
}
s*****r
发帖数: 773
3
来自主题: Programming版 - 这段代码为何要这样做?
一个整数转化为字符串的函数
不太理解里面为什么要用到log
希望大家指点一二
char* itoa(int n) {
char *s =NULL;
char *p;
int nz;

int mf = 0;
if(n>=0) {
nz=log(n)/log(10);

s=(char*)calloc(nz+1,sizeof(char));
p=s;
}else{
if(n==INT_MIN) {
mf=1;
n=-INT_MAX;
}
nz=log(-n)/log(10);
n=-n;
s=(char *)calloc(nz+2,sizeof(char));
p=s;
*p='-';
p++;
}
int pt=pow(10,nz);

while(pt>=1){

if(pt==1&&mf)
*p=(n/pt)+'0'+1;
else
*p=(n/pt)+'0';
p++;
b******a
发帖数: 215
4
来自主题: Programming版 - Array in C
actually &abc and abc point to the same address.
the different is when you set
char (*p1)[30]=&abc;
p1+=1;
char* p2=abc;
p2+=1;
p1+1 and p2+1 will point to different address,
say p2=p1=0x000010, p1+1 will make p1 point to 0x00010+ 1E*sizeof(char);
p2+1 will point to 0x00010+1*sizeof(char)
g*******m
发帖数: 42
5
来自主题: Programming版 - ask a simple question about int pointer.
size = sizeof num /sizeof *num;
k*k
发帖数: 508
6
来自主题: Programming版 - ask a simple question about int pointer.
仔细看原题
#include
int GetSizeOfMemory(int *a) {
return sizeof(a)/sizeof(int);
}
int main() {
int a[10];
printf("%d\n", GetSizeOfMemory(a));
}
输出 1.
b******g
发帖数: 54
7
来自主题: Programming版 - ask a simple question about int pointer.
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);
}
t*********n
发帖数: 278
8
来自主题: Programming版 - code question
the sample from programming pearls. But, I got complier error at this line
qsort(a, n, sizeof(char *), pstrcmp). How can I fix this one? Thanx.
#include
#include
#include
int pstrcmp(char **p, char **q)
{ return strcmp(*p, *q); }
#define MAXN 5000000
char c[MAXN], *a[MAXN];
int main()
{ int i, ch, n = 0, maxi, maxlen = -1;
while ((ch = getchar()) != EOF) {
a[n] = &c[n];
c[n++] = ch;
}

c[n] = 0;
qsort(a, n, sizeof(char *), pstrcmp);
}
k****f
发帖数: 3794
9
来自主题: Programming版 - 这个C++程序为什么不能运行
C老问题拉,去精华区看看吧
sizeof(a)和sizeof(b)是不一样的
c*********n
发帖数: 128
10
Let's say I have
double a[N][M] = { .......};
How to transform a into a 2D vector
vector< vector > v
?
I know for 1d case, you can do the following:
int myints[] = {16,2,77,29};
vector fifth (myints, myints + sizeof(myints) / sizeof(int) );
But what about 2d?
g*********s
发帖数: 1782
11
来自主题: Programming版 - STL map变量的实际memory usage估算
比如声明这样一个变量
using namespace std;
map *nameMap = new(map);
... // 在nameMap里插入了1000个(string, int) pair
现在需要估算这时heap的size增加了多少?
估算如下:
本身sizeof(map<...,...>)是48个字节。
一个string s的size是 sizeof(string) + s.size(),假定字符串长度都是8,则是16。
一个int是4字节。
这样至少是48+1000*(4+16)=2048 bytes。
但是map是用rb_tree实现,加上开销是多少呢?看了一下source code,不太确定是不
是算对了。
每个node包含color, left, right, parent, key, value,其中(key, value)前面算过。
color 1 byte,三个指针,64位机器上,24字节,这样就是25byte。总的字节是:
48+1000*(4+16+1+24)=4548。
如果node是按8-byte
t******r
发帖数: 209
12
来自主题: Programming版 - reinterpret cast的问题
看到一个将类的成员serialize到文件中:
out.write(reinterpret_cast(&year),sizeof(int));
out.write(reinterpret_cast(&salary),sizeof(int));
year, salary是int类型
请问为啥一定要把他们用reinterpret_cast输出呢?直接out << year << salary不行?
多些!!!
b***y
发帖数: 2799
13
来自主题: Programming版 - [合集] 偶写的itoa
☆─────────────────────────────────────☆
OverCloud (天马行空) 于 (Tue Mar 27 16:55:30 2007) 提到:
用的os没有itoa. 只好自己写了一个.
char * itoa(unsigned i, char *str, int redex)
{
static const char *LOOKUP = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char holder;
char *p1, *p2;

if(redex > 0 && redex < sizeof(LOOKUP)/sizeof(char))
{
if (i == 0)
{
str[0] = '0';
str[1] = '\0';
}
else
{
p1 = str;

m*t
发帖数: 7490
14
例如已经分配了20bytes的一个指针
char *p=(char *)malloc(sizeof(char)*20);
这时用sizeof(p)只能返回 这个指针变量自身的空间大小,32位系统是4,而不是20
不知道有没有什么函数能够返回20这个值?
r*****s
发帖数: 985
15
是的,
还有一个弱智问题,
wchar_t wszTypeDef[512];
请问直接给
cchTypeDef=512;

cchTypeDef=sizeof(wszTypeDef)/sizeof(wszTypeDef[0]);
应该没什么区别吧?
实在搞不懂怎么corrupted的
g*********s
发帖数: 1782
16
来自主题: Programming版 - fread/fwrite有big/small endian问题吗?
For example:
big endian machine,fwrite(&x, sizeof(int), 1, output).
small endian machine, fread(&y, sizeof(int), 1, input).
Here input and output point to the same file.
Will fread/write transparently handle this issue?
s********f
发帖数: 3924
17
来自主题: Programming版 - 请教一个fseek和_fseeki64的问题
offset如果是-1*sizeof(float), 好像自动就换算成2^32-4了,如果写成-1*4,就是-4
. 如果用fseek,两个结果都一样,如果改成_fseeki64, 结果就不同了。请问如果不把
sizeof(float)换成4,还有其他方法解决吗?谢谢
i***h
发帖数: 12655
18
来自主题: Programming版 - how to get this size?
a general solution is:
sizeof(MetricsTitleStr)/sizeof(char*)
I*******e
发帖数: 1879
19
来自主题: Programming版 - [合集] 请教一个calloc的问题
☆─────────────────────────────────────☆
silentwolf (沉默的狼) 于 (Wed May 20 16:43:55 2009) 提到:
请问calloc最大能分配多少内存?我用calloc分配一个360M*sizeof(float)的内存,结
果没成功。试了一下300M*sizeof(float)可以。我google一下,calloc好像应该可以分
配4G,我这里只有1.5G左右,请问为啥会有问题? 谢谢
我用的是winxp 32位,vc++ 2008. Ram是4G.
☆─────────────────────────────────────☆
thrust (祝阳阳早日康复) 于 (Wed May 20 18:34:57 2009) 提到:
这个跟OS有关的
winxp 32位的进程用户空间缺省是2G
你一下子分1.5, 是很紧张
启动时用/3GB可以把用户空间长到3G
4G是不可能的, 你被骗了, 64位还差不多

☆─────────────────────────────────────☆
silent
l**t
发帖数: 64
20
来自主题: Programming版 - 关于placement new
用户可以自定义(重载)new,delete操作符
void* operator new(size_t); //当new T或operator new(sizeof(T))+T的构造函数
时调用该函数
void* operator new[](size_t); //当new T[n]或operator new[](n*sizeof(T))+每个
T的构造函数 时调用该函数
X****r
发帖数: 3557
21
来自主题: Programming版 - 问个程序问题
以下程序没测试过,仅供参考。
/**
* @param input The input file.
* @param size A pointer to store result size.
* @return The pointer to the result data,
* or NULL for not enough memory.
*/
int *read_all(FILE *input, int *size) {
int n = 128, i;
int *data = (int *)malloc(n * sizeof(int));
for (i = 0; data && fscanf(input, "%d", data + i); i++) {
if (i == n) {
data = (int *)realloc(data, (n *= 2) * sizeof(int));
}
}
if (size) {
*size = i;
}
return data;
}
r*****n
发帖数: 120
22
来自主题: Programming版 - 问几个C++的题
A: sizeof(arry)/sizeof(element); can not
A: dynamic size of linked list may exceed size of array
A: go through every linked list data and put them into a set, judge its
second attribute is false or not
X****r
发帖数: 3557
23
来自主题: Programming版 - 怎么得到char *分配空间的大小?
对,sizeof(指针)返回这个指针变量本身的大小,不是它指向的内容或数组的大小。
sizeof(数组)返回整个数组的大小。
v****s
发帖数: 1112
24
来自主题: Programming版 - 怎么得到char *分配空间的大小?
对!这个我上面已经验证过了。
所以,sizeof(指针)返回4,说明我用的是32位系统。
我想知道的是,这个sizeof()是怎么implement的?而且区别了array和pointer?
z****e
发帖数: 2024
25
来自主题: Programming版 - 大侠们救命, C++ operator new 问题
代码如下:
#include
using namespace std;
class Foo{
public:
Foo(){cout<<"ctr"< void* operator new(size_t){
return pool;
}
private:
static char pool[ ];
};
char Foo::pool[sizeof(Foo)];
void* rbv(){
static char pool[sizeof(Foo)];
return pool;
}
int main(int argc, char* argv[]){
Foo *x=new Foo;//正确,输出 “ctr”
Foo *y=rbv();//错误:invalid conversion from ‘void*’ to ‘Foo*’
}
我就奇怪了,一样是返回一块内存,怎么必须是operator new()返回的就行?而一般
global函数返回的就不行呢?
t****t
发帖数: 6806
26
来自主题: Programming版 - 大侠们救命, C++ operator new 问题
(new Foo) is not equivalent to (Foo::operator new).
new Foo will do following:
call Foo::operator new(sizeof(Foo)). if not found, call ::operator new(
sizeof(Foo)). [5.3.4, 8]
then call Foo::Foo() for the allocated space.[5.3.4, 15].
then returns Foo*.

方?
d****p
发帖数: 685
27
来自主题: Programming版 - 一个C的void指针的问题
He mentioned macro for C.
Sth like:
#define PROCESS_ARRAY(T,p,n) {\
T* p2 = p;\
int i;\
for (i = 0; i < n; ++i)\
printf("%d", p2[i];\
}
in your code...
typedef int MyType;
MyType a[] = {1,2,3};
PROCESS_ARRAY(MyType, a, sizeof(a)/sizeof(MyType))
w******h
发帖数: 16
28
Func1 用来create 一个指针数组。每个指针指向一个struct node myNode
void Func1 (myNode*** p)
{
// len is a global value
myNode** tmp;
tmp = (myNode**) malloc(sizeof(myNode*) * len);
p = &tmp;
……
for (int i=0; i {
(*p)[i] = (myNode*)malloc(sizeof(myNode));
……
}
}
Func2用来处理这个指针数组和回收memory
void Func2 (void)
{
myNode** p;
Func1 (&p);
for (int i=0; i {
if (p[i])
{
// Handle data
free (p[i]);
}
}
free (p);
}
Questions
i***1
发帖数: 147
29
来自主题: Programming版 - 关于数组size的问题
5=sizeof(arr)/sizeof(int)
e******d
发帖数: 310
30
来自主题: Programming版 - C++指针问题 int (*) [10]
int (*) [ ] 是不是一个类型(没有size)?int (*) [5]和 int (*) [10]是不是一样
的类型?
no,
give you an example,
int a[][5] ={{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int b[][10] ={{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
int (*c)[5] = a[0];
int (*d)[10] = b[0];
//note (c+1) - c is different from (d+1) - d
// (c+1) - c should be sizeof(int)*5
//(d+1) - d should be sizeof(int)*10
X****r
发帖数: 3557
31
来自主题: Programming版 - 10个数所有的组对可能, 怎么解?
如果不考虑效率的话(比如你这里只有10个数)可以直接利用C++的next_permutation
(如果你不用C++的话next_permutation的代码也很简单),滤掉所有重复的就行了:
#include
template bool next_pairs(T begin, T end) {
while (std::next_permutation(begin, end)) {
T p;
for (p = begin; p != end; p += 2) {
if (*p > *(p+1) || p+2 != end && *p > *(p+2)) {
break;
}
}
if (p == end) {
return true;
}
}
return false;
}
用法:
int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int x_end = x + sizeof(x) / sizeof(*x)
X****r
发帖数: 3557
32
来自主题: Programming版 - a c++ question.
What I just said is on the syntactic level.
On the semantic level, a is an array -- it contains all its elements.
It is not the same as a pointer to its first element. The latter is
'&a[0]'. For example, sizeof(a) is 10, which is the length of the array
times size of its individual element; while sizeof(&a[0]) is 4 on
32-bit architecture, which is the size of a 'char *' pointer.
Back to your problem: 'f' requires a 'char ** input'. Now assuming
you could just pass '&a' in. What would happen if f
K*****n
发帖数: 65
33
来自主题: Programming版 - 问一个C++文件读取的问题
Solution One:
1) Replace
fstream OUTPUT( "RESULT.dat", ios::out | ios::in );
with
fstream OUTPUT( "RESULT.dat", ios::trunc| ios::out | ios::in );
2) Replace
OUTPUT.seekg( 2*sizeof(int), ios::beg );
with
OUTPUT.seekg( 1 + 2*sizeof(int), ios::beg );
Solution Two:
Replace
fstream OUTPUT( "RESULT.dat", ios::out | ios::in );
with
fstream OUTPUT( "RESULT.dat", ios::binary | ios::trunc| ios::out | ios::in );
z****e
发帖数: 2024
34
来自主题: Programming版 - 怎样将array^转换成string?
unsigned char a[ ]="asfsadfs";
string s(a,a+sizeof a/sizeof *a);
X****r
发帖数: 3557
35
来自主题: Programming版 - Question about data member offset
You did it wrong when trying to print out these values of
pointer-to-member type. Try this way:
(assuming sizeof(float Point3d::*) == sizeof(unsigned long) here;
if not, change 'unsigned long' to appropriate integral type)
float Point3d::*p = &Point3d::x;
std::cout << "&Point3d::x = " << *(unsigned long*)&p << std::endl;
The reason why you got '1's if you try to print out
pointer-to-member values directly is because they can be
automatically convert to boolean (true for not null, false for
null)
k********n
发帖数: 182
36
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
ifstream is(filename);
char bufz[1024];
memset(bufz, 0, sizeof(bufz));
is.getline(bufz, sizeof(bufz)-1);
诡异之一,memset完之后,bufz[0] = 1
诡异之二,getline完之后,从bufz+4开始才是文件的内容,前面多出了4个字节
用的是VS2005。另外,在另一个工程里用这些都没问题。有人知道是为什么吗?
Thanks!
w***g
发帖数: 5958
37
来自主题: Programming版 - 电话面试题一问 (转载)
1. 不加return返回值就不确定了。
2. C++要求一定要int ::main,这是C++标准规定的。C语言或者某些非标准的C++编译
器下可能"double main ()"能编译通过,但是因为sizeof(double)一般不等于sizeof(
int),这么做可能会引起程序退出时崩溃。
g*******g
发帖数: 431
38
来自主题: Programming版 - 腆着脸在问一道
Write a C++ function prototype and a function definition called minimum that
receives an array of type double, and the number of elements in the array.
It returns the minimum value in the array.
#include
#include
using namespace std;
void minimum ();
int main()
{
minimum();
return 0;
}
{
char str[];
double arr[];
int min_value, num, i;
//get array from console
cout<<"Enter a string:";
cin>>str;
//find number of element in the array
num = sizeof (str)/sizeof([str]);
//find min
B*******g
发帖数: 1593
39
来自主题: Programming版 - 问个virtual table 的问题
class A{
public:
virtual void f(){}
};
class B: public A
{
virtual void g() {}
};
B objB;
我的理解是以上代码只生成一个v table 里面只有一个函数 A::f()
但如果class B: virtual public A
好像就生成两个v table, 第二个v table 里面是 B::g()
virtual public A中的virtual 到底起到了什么作用?而且如果我comment out B::g()
的话
sizeof(objB) for :virtual pubic A is greater than sizeof(objB) for :pubic
A, 这多余的内存用来干嘛的?
先谢了
e******d
发帖数: 310
40
来自主题: Programming版 - A question about class size
Given class A and class B, class A is an empty class and class B has one data member whose type is char; their definitions are as follows:
============================
class A
{
};
class B
{
private:
char c;
};
int n1 = sizeof(A); //n1 = 1;
int n2 = sizeof(B); //n2 = 1;
================================
The question is why n1 is equal to 1.
Thank you a lot.
p***o
发帖数: 1252
41
来自主题: Programming版 - C++里get array size的问题 (转载)
Most likely they won't work under the situation LZ needs them to work,
e.g. after passing the array to a function ...
BTW, I don't see the advantage of the template approach compared to
the sizeof/sizeof approach. Is it more robust?
p***o
发帖数: 1252
42
来自主题: Programming版 - C++里get array size的问题 (转载)
Beginners usually write code like follows ;)
void func(int array[N])
{
int size = sizeof(array)/sizeof(array[0]);
...
}

a
or
b*****e
发帖数: 474
43
来自主题: Programming版 - a careercup question
Great! Learned something today!
But:int struct CONVERT, should use sizeof(A)*8 instead of sizeof(A), haha
Traditional way:
class BitCounter {
public:
static int convert(int i, int j) { return count(i^j); }
static int count(int k) {
int n = 0;
for (; k != 0; k &= (k-1) ) n++;
return n;
}
};
O*******d
发帖数: 20343
44
来自主题: Programming版 - 我最近写的一个屏保程序
说一下我怎么做的antialiase。 先把图形计算好,比最后的图像多出宽三列高三行。
图形是一个浮点数的点阵。 两个相邻的点之间的距离是一个单位。 然后用三次方程插
值。每个插值要用图形中4X4的值来计算。 在四个点组成的方格中均匀插入5X5=25个点
(单核机)或7X7=49个点(多核机),给每个插值赋予颜色,最后把一个方格中所有的
颜色平均,就是一个像素的颜色。
由于一组插值用的是同一套三次方程系数,所以写了一个class来做。 其实是一个
template。 做插值时,有两个套着的循环,分别在X方向移动和Y方向移动。 内循环是
在Y方向从上往下移动,这样每次移动一步,只需要计算最后一列的三次方程系数。下
边是我写的Cubic Interpolator. 是一个recursive template。For bicubic
interpolation, N = 2. 所有的重复计算都尽可能的避免了。 这个templete的计算
速度,在N==2时,是单独bicubic interpolation的速度的三倍。
template
class Cu... 阅读全帖
p***o
发帖数: 1252
45
来自主题: Programming版 - C++ Function Pointer Array 的问题
sizeof(seq_array)/sizeof(seq_array[0])

列5
elem,
z****e
发帖数: 2024
46
来自主题: Programming版 - C++ Function Pointer Array 的问题
an alternative to save a little typing
sizeof(seq_array)/sizeof(*seq_array)
P********e
发帖数: 2610
47
你这个不 make sense
算size
sizeof(size_t)在32,64下是变量,所以有问题啊。
sizeof(int/double/long)都一样,所以没问题啊。

size_
调用
的长
X****r
发帖数: 3557
48
Try this yourself:
sizeof_long.c:
#include
int main() {
printf("sizeof(long) = %d\n", (int)sizeof(long));
}
gcc -m32 sizeof_long.c
./a.out
gcc -m64 sizeof_long.c
./a.out
b********e
发帖数: 58
49
If I were you, I may have the following code (assuming it's C):
typedef struct {
int row;
int column;
int* data;
} IArray;
IArray * IArrayInit(int row, int col)
{
IArray *ptr;
ptr = malloc(sizeof(IArray));
ptr->row = row;
ptr->column = col;
ptr->data = malloc(sizeof(int)*row*col);
}
void IArrayFree(IArray *ptr)
{
assert(ptr);
if (ptr->data ) free(ptr->data);
free(ptr);
}
In your main
IArray *myArray;
myArray = IArrayInit(5, 6)
/* do whatever you want with myArray... 阅读全帖
M**u
发帖数: 10158
50
来自主题: Programming版 - another tougth pointer example
这个tough个啥啊
sizeof (float *) 要存一个指针大小
sizeof (float)要存一个float大小
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)