这可能是一个愚蠢的问题,但是当你没有传入数组中的元素数量时,sizeof运算符如何知道数组操作数的大小.我知道它不会返回数组中的总元素,而是以字节为单位返回大小,但为了得到它,它仍然必须知道数组何时结束.只是好奇这是如何工作的.
正如标题所暗示的,我的问题是如何获得字符串的大小C.sizeof如果我在没有malloc它的函数中声明它(字符串),那么使用它是否合适?或者,如果我已将其声明为指针?如果我用它初始化malloc怎么办?我想做一个详尽的回复.
可能重复:
如何查找sizeof(指向数组的指针)
我知道sizeof运算符在编译时被评估并替换为常量.鉴于此,如何在程序的不同点传递不同数组的函数是否可以计算它的大小?我可以将它作为参数传递给函数,但如果我不是必须的话,我宁愿不必添加另一个参数.
这是一个例子来说明我的要求:
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF(a) ( sizeof a / sizeof a[0] )
void printarray( double x[], int );
int main()
{
double array1[ 100 ];
printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
printf( "The size of array1 = %ld.\n", sizeof array1 );
printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );
printarray( array1, SIZEOF( array1 ) );
return EXIT_SUCCESS;
}
void printarray( double p[], int s )
{
int i;
// …Run Code Online (Sandbox Code Playgroud) 我无法找到指针数组的长度.比方说我有:
char array[40] = "hello"; // size 40
int length = sizeof array / sizeof array[0]; // no problem returns 40
Run Code Online (Sandbox Code Playgroud)
//如何仅使用指向该数组中第一个元素的指针来获取数组的长度?
char* pchar = array;
Run Code Online (Sandbox Code Playgroud)
//如果
std::strlen(pchar); // this returns the length of 5... i want length 40
Run Code Online (Sandbox Code Playgroud)
//如果
int count = 0;
while(true)
{
if(*(pchar + count) == '\0') // returns 5...
break;
count++;
}
Run Code Online (Sandbox Code Playgroud)
如何从指向数组中第一个元素的指针返回长度40?
我发现我可以做到这一点.
int count = 0;
while(true)
{
if(*(pchar + count) == '\0' && *(pchar + count + 1) != '\0')
break;
count++; …Run Code Online (Sandbox Code Playgroud) 它们是以编程语言还是数学家命名的?
Pascal字符串的定义特征是什么?在Wikipedia关于字符串的文章中,似乎定义的特征是在第一个字节中存储字符串的长度.在另一篇文章中,我得到的印象是字符串的内存布局也很重要.
在阅读一个不相关的SO线程时,有人提到Pascal字符串使Excel快速运行.Pascal字符串优于以null结尾的字符串有什么优势?或者更一般地说,Pascal字符串在什么情况下表现优异?
Pascal字符串是否以其他语言实现?
最后,我是否将两个单词("Pascal Strings")或仅第一个("Pascal字符串")大写?我是技术作家......
可能重复:
如何查找sizeof(指向数组的指针)
我正在学习如何在C中创建动态数组,但遇到了一个我无法弄清楚的问题.
如果我使用代码:
int num[10];
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
Run Code Online (Sandbox Code Playgroud)
我得到输出:
sizeof num = 40
sizeof num[0] = 4
Run Code Online (Sandbox Code Playgroud)
这就是我期望发生的事情.但是,如果我malloc数组的大小如下:
int *num;
num = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
Run Code Online (Sandbox Code Playgroud)
然后我得到输出:
sizeof num = 8
sizeof num[0] = 4 …Run Code Online (Sandbox Code Playgroud) 在Google V8项目中读取globals.h时遇到以下宏定义.
// The expression ARRAY_SIZE(a) is a compile-time constant of type
// size_t which represents the number of elements of the given
// array. You should only use ARRAY_SIZE on statically allocated
// arrays.
#define ARRAY_SIZE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
Run Code Online (Sandbox Code Playgroud)
我的问题是后半部分:static_cast<size_t>(!(sizeof(a) % sizeof(*(a))))).我想到的一件事是:由于后一部分将始终求1值为(类型)size_t,整个表达式将被提升为size_t.
如果这个假设是正确的,那么还有另一个问题:由于sizeof运算符的返回类型是size_t,为什么需要这样的推广?以这种方式定义宏有什么好处?
在给定void *指针的情况下,如何判断在给定地址上分配的块的大小(先前使用malloc在Linux和Windows中分配)?我希望两个系统肯定会在某处存储这种信息.也就是说,malloc_sizeOSX/Darwin上存在替代方案.如果它有帮助,使用gcc/mingw.
我知道C中的字符串只是字符数组。因此,我尝试了以下代码,但给出了奇怪的结果,例如垃圾输出或程序崩溃:
#include <stdio.h>
int main (void)
{
char str [5] = "hello";
puts(str);
}
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
它可以用干净地编译gcc -std=c17 -pedantic-errors -Wall -Wextra。
注意:对于在声明字符串时未能为NUL终止符分配空间而引起的问题,本帖子旨在用作规范的FAQ。
我有一些代码是这样的(这不是生产代码.只是一个示例代码)
char *inbuf = NULL;
inbuf = buf; //buf is some other valid buffer of size 100.
func(&inbuf);
.....
void func(char **p)
{
...
(*p)++;
...
}
Run Code Online (Sandbox Code Playgroud)
Coverity Tool表示"使用&inbuf获取地址会产生单身人士".我听说过关于C++的单例一词.但是,单例指针在C方面意味着什么呢?