标签: sizeof

C - > sizeof string总是8

#include "usefunc.h" //don't worry about this -> lib I wrote
int main()
{
  int i;
  string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array
  given[0] = "a";
  printf("Please enter words separated by RETs...\n");
  for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++)
    {
      given[i] = GetLine();
      /*
      if (sizeof(given[i]) > sizeof(longest))
    {
      longest = given[i];
    }
      */
      printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!!
    }
  printf("%s", longest);
}
Run Code Online (Sandbox Code Playgroud)

它为什么总是返回8 ???

c string sizeof string-literals

-1
推荐指数
3
解决办法
6931
查看次数

为什么sizeof运算符无法在函数模板中工作?

我正在尝试学习C++函数模板.我传递一个数组作为指向我的函数模板的指针.在那,我试图找到一个数组的大小.这是我使用的功能模板.

template<typename T>
T* average( T *arr)
{
    T *ansPtr,ans,sum = 0.0;    

    size_t sz = sizeof(arr)/sizeof(arr[0]);
    cout<<"\nSz is "<<sz<<endl;

    for(int i = 0;i < sz; i++)
    {
        sum = sum + arr[i];
    }
    ans = (sum/sz);
    ansPtr = &ans;
    return ansPtr;
}
Run Code Online (Sandbox Code Playgroud)

即使我将指针传递给整数数组,该cout语句也会显示arras 的大小.现在我知道这可能是我之前提到的问题的可能重复,但我需要对此有更好的解释.15

我唯一能想到的是,因为模板是在运行时调用的,并且sizeof是一个编译时运算符,所以编译器只是忽略了这一行

   int sz = sizeof(arr)/sizeof(arr[0]);
Run Code Online (Sandbox Code Playgroud)

因为在实际调用函数之前它不知道arr的确切类型.这是正确的还是我错过了这里的东西?将指针发送到函数模板也是可靠的吗?

c++ templates sizeof

-1
推荐指数
1
解决办法
700
查看次数

功能"fread"导致分段错误

我对此感到不安fread; 这是非常基础的,但我无法找到答案.

#include <stdio.h>
#include <stdlib.h>
void main () 
{
    int i;
    FILE *fp;
    char *re;
    fp = fopen("/net2/192.168.42.151/stud3/2013/dobo1298/data.txt","r");
    i = sizeof(char);
    printf("%d",i);
    re = calloc(10,i);
    fread(re,i,1,fp);
    printf("%s",*re);
}
Run Code Online (Sandbox Code Playgroud)

c sizeof fread

-1
推荐指数
1
解决办法
90
查看次数

我的代码输出如何是842?

#include <stdio.h>

int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d\t",sizeof('a'));

return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译我的代码时,输​​出将是:"842".有人可以解释为什么我得到这个输出?

c 32-bit sizeof 16-bit

-1
推荐指数
1
解决办法
185
查看次数

为什么sizeof("string")打印的长度超过"string"的长度?

# include <stdio.h>
int main()
{
        int a = sizeof("string");
        printf("%d", a );
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码打印7作为输出,而字符串的长度只有6.有人可以解释一下吗?

c c++ sizeof

-1
推荐指数
1
解决办法
1278
查看次数

sizeof(struct)返回的SMALLER小于预期值吗?

我有这个c ++结构:

struct Packet
{
    uint32 MessageCount; 
    uint32 Length; 
    uint32 FieldValue;

    union PacketHeader
    {
        uint32 typeInfo; 
        struct MagicVersion
        {
            uint8 MagicNumber[3];
            uint8 Version; 
        };
     };

    Data * Payload(void) { return reinterpret_cast< Data * >(this + 1U); }
    Data const * Payload(void) const { return reinterpret_cast< Data const * >(this + 1U); }

    Packet * nextPacket(void) { return reinterpret_cast< Packet * >(this + 1U) + Length; }
    Packet const * nextPacket(void) const { return reinterpret_cast< Packet const * >(this + 1U) …
Run Code Online (Sandbox Code Playgroud)

c++ struct sizeof

-1
推荐指数
1
解决办法
128
查看次数

C++中类对象的大小

在什么基础上,类对象的大小显示为12?

class testvector
{
    public : vector<int> test;
};
int main()
{
    testvector otestvector;
    cout<<"size :"<<sizeof(otestvector)<<"\n";
    cout<<"size of int :"<<sizeof(int);
}
Run Code Online (Sandbox Code Playgroud)

输出:

size :12
size of int :4
Run Code Online (Sandbox Code Playgroud)

c++ sizeof

-1
推荐指数
1
解决办法
3223
查看次数

malloc,sizeof和strlen函数可能存在冲突吗?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct _person
{
    char *fname;
    char *lname;
    bool isavailable;
}Person;


Person *getPersonInstance(void)
{
    Person *newPerson = (Person*) malloc(sizeof(Person));
    if(newPerson == NULL)
        return NULL;
    return newPerson;
}

void initializePerson(Person *person, char *fname, char *lname, bool isavailable)
{
    person->fname = (char*) malloc(strlen(fname)+1);
  /*problematic behaviour if i write: person->fname = (char*) malloc (sizeof(strlen(fname)+1)); */

    person->lname = (char*) malloc(strlen(lname)+1);
 /*problematic behaviour if i write: person->lname = (char*) malloc (sizeof(strlen(lname)+1)); */

    strcpy(person->fname,fname);
    strcpy(person->lname,lname);
    person->isavailable = isavailable; …
Run Code Online (Sandbox Code Playgroud)

c malloc sizeof strlen

-1
推荐指数
1
解决办法
150
查看次数

为什么sizeof(a?true:false,a)运算符打印一个字节?

根据这个问题,sizeof(true)或者sizeof(false)4字节,因为truefalse是宏并在#include <stdbool.h>头文件中定义.

但是,这里有趣的程序输出.

#include <stdio.h>
#include <stdbool.h>

int main() 
{
    bool a = true;
    printf("%zu\n", sizeof(a ? true : false, a));
                             /* ^^ -> expresion2 */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(现场演示):

1
Run Code Online (Sandbox Code Playgroud)

C11 6.5.15条件运算符(P4):

第一个操作数被评估; 在其评估与第二或第三操作数的评估之间存在一个序列点(以评估者为准).仅当第一个操作数不等于0时才评估第二个操作数; 仅当第一个操作数比较等于0时才评估第三个操作数; 结果是第二个或第三个操作数的值(无论哪个被评估),转换为下面描述的类型.110)

这里a是一个bool类型和赋值true,然后在sizeof运算符内,运算conditional符执行expression2因为a是a true.

因此,comma运算符部分(表达式3)未根据标准进行评估,而表达式2是宏.所以,根据那个问题,宏的输出是4字节,但在这里,程序的输出是1字节.

那么,为什么 只sizeof(a ? …

c boolean sizeof comma conditional-operator

-1
推荐指数
2
解决办法
157
查看次数

通过sizeof将会发生什么

我有这个问题我不明白为什么我得到不正确的结果:

    if(sizeof(int) > -1)
        cout << "true" << endl;
    else
        cout << "false" << endl;
Run Code Online (Sandbox Code Playgroud)

上面也总是打印错误!但下面是正确的:

    int n = sizeof(int);

    // correct
    if(n > -1)
        cout << "true" << endl;
    else
        cout << "false" << endl;
Run Code Online (Sandbox Code Playgroud)

谁能向我解释发生了什么?谢谢.

c++ sizeof

-1
推荐指数
2
解决办法
90
查看次数