不用sizeof 能否求出int[] 数组的长度

神奇大自然 2018-08-02 08:43:19
求教大神指点下,或者sizeof的实现方法。
...全文
1406 37 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
37 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongchengli 2018-09-07
  • 打赏
  • 举报
回复
int a[66] = {0}; int nSize = (int)&(((a*)0)[1]); (a*)0 将0强制转换为a类型指针 ((a*)0)[1] 引用一个值 &(((a*)0)[1]) 读取引用的地址 将读取地址转换为int类型即数组长度
赵4老师 2018-09-07
  • 打赏
  • 举报
回复
其实电脑开机后物理内存的每个字节中都有值且都是可读写的,从来不会因为所谓的new、delete或malloc、free而被创建、销毁。区别仅在于操作系统内存管理模块在你读写时是否能发现并是否采取相应动作而已。操作系统管理内存的粒度不是字节而是页,一页通常为4KB。
ForestDB 2018-09-06
  • 打赏
  • 举报
回复
用了sizeof也算不出来。
如果是C就老老实实用额外的变量存数组d长度;
如果是C++还可以用模板来挽救下。
BOSSYUCHAO 2018-08-24
  • 打赏
  • 举报
回复
可以定义一个指针指向数组的首地址然后依次往后移,直到指针为空
AlbertS 2018-08-24
  • 打赏
  • 举报
回复
引用 1 楼 weixin_36010922 的回复:
补充下,int[66] 求出这个数组的长度。


int arr[66];
int len = (char*)&arr[66] - (char*)&arr[0];

青红zz 2018-08-22
  • 打赏
  • 举报
回复
不用sizeof求 可以用指针来求

#include <stdio.h>

int main()
{

int a[66]={0};
printf("len=%d",*(&a+1)-a);


return 0;
}
Jacky_Lu 2018-08-22
  • 打赏
  • 举报
回复
数组大小 在数组定义时候 已经被 编译器知道了
sizeof 只是 将 编译器 里面的数据 变量化
所以必须要用这个,
你是想另外一个文件里面 获取 变量数组的 大小吧
最好 自己定义一个宏定义
#define PeopleSize 16


在文件1 定义 int people[PeopleSize ];

在文件2 利用 PeopleSize
sevancheng 2018-08-22
  • 打赏
  • 举报
回复
sizeof 是编译时行为,不是运行时,求不出
weixin_42944368 2018-08-11
  • 打赏
  • 举报
回复
length?
hongwenjun 2018-08-10
  • 打赏
  • 举报
回复

template <typename T, int N>
int arr_size(T (&arr)[N])
{
return N;//函数直接返回作为参数传入的数组的长度。
}

《imperfect C++》可以采用一个小技巧求出一个数组长度的函数, 用了个template把长度直接截取,因为传入的是一个数组的引用(注意数组引用作为参数的语法),所以能够在编译期把指针识别出来。


hongwenjun 2018-08-10
  • 打赏
  • 举报
回复

CSDN 自己跳出来的答案
hongwenjun 2018-08-10
  • 打赏
  • 举报
回复
引用 26 楼 zhouchongzxc 的回复:

#include <iostream>

template<typename T,int N>
int pp(T (&__arr)[N]){
return N;
}

int main(){
int a[7];
int b[] = {1,5,8};

std::cout<<pp(a)<<std::endl;
std::cout<<pp(b)<<std::endl;
}


这么模版函数 能解释一下吗, C++的模版没学好
ChongQingJin28 2018-08-10
  • 打赏
  • 举报
回复
把得分给我吧
ChongQingJin28 2018-08-10
  • 打赏
  • 举报
回复

#include <iostream>

template<typename T,int N>
int pp(T (&__arr)[N]){
return N;
}

int main(){
int a[7];
int b[] = {1,5,8};

std::cout<<pp(a)<<std::endl;
std::cout<<pp(b)<<std::endl;
}
hongwenjun 2018-08-10
  • 打赏
  • 举报
回复
引用 24 楼 zhouchongzxc 的回复:
[quote=引用 23 楼 hongwenjun 的回复:]

int size = std::end(ia) - std::begin(ia);


// std::initializer_list support -*- C++ -*-

// Copyright (C) 2008-2015 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// GCC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

/** @file initializer_list
* This is a Standard C++ Library header.
*/

#ifndef _INITIALIZER_LIST
#define _INITIALIZER_LIST

#pragma GCC system_header

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else // C++0x

#pragma GCC visibility push(default)

#include <bits/c++config.h>

namespace std
{
/// initializer_list
template<class _E>
class initializer_list
{
public:
typedef _E value_type;
typedef const _E& reference;
typedef const _E& const_reference;
typedef size_t size_type;
typedef const _E* iterator;
typedef const _E* const_iterator;

private:
iterator _M_array;
size_type _M_len;

// The compiler can call a private constructor.
constexpr initializer_list(const_iterator __a, size_type __l)
: _M_array(__a), _M_len(__l) { }

public:
constexpr initializer_list() noexcept
: _M_array(0), _M_len(0) { }

// Number of elements.
constexpr size_type
size() const noexcept { return _M_len; }

// First element.
constexpr const_iterator
begin() const noexcept { return _M_array; }

// One past the last element.
constexpr const_iterator
end() const noexcept { return begin() + size(); }
};

/**
* @brief Return an iterator pointing to the first element of
* the initializer_list.
* @param __ils Initializer list.
*/
template<class _Tp>
constexpr const _Tp*
begin(initializer_list<_Tp> __ils) noexcept
{ return __ils.begin(); }

/**
* @brief Return an iterator pointing to one past the last element
* of the initializer_list.
* @param __ils Initializer list.
*/
template<class _Tp>
constexpr const _Tp*
end(initializer_list<_Tp> __ils) noexcept
{ return __ils.end(); }
}

#pragma GCC visibility pop

#endif // C++11

#endif // _INITIALIZER_LIST


std::end 内应该有更简单的方法

[/quote]

好像是有 size函数
ChongQingJin28 2018-08-10
  • 打赏
  • 举报
回复
引用 23 楼 hongwenjun 的回复:
int size = std::end(ia) - std::begin(ia);


std::end 内应该有更简单的方法

hongwenjun 2018-08-10
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <algorithm> //using end begin

int main()
{
int ia[] = { 1, 2, 3, 4, 5, 6, 7, 8 };

int size = std::end(ia) - std::begin(ia);

printf("%d\n", size);
return 0;
}
自信男孩 2018-08-09
  • 打赏
  • 举报
回复
引用 21 楼 ckc 的回复:
[quote=引用 20 楼 cfjtaishan 的回复:]
[quote=引用 19 楼 ckc 的回复:]
sizeof不是宏,也不是函数,也不算什么运算符吧
这个应该是编译器在编译的时候就可以预先计算的常量

宏就是在编译的时候预先计算的[/quote]
宏是在编译的时候展开的,并不一定是常量。而sizeof编译之后是一个常量。[/quote]
搜一下sizeof,很多描述是sizeof是运算符。
ckc 2018-08-09
  • 打赏
  • 举报
回复
引用 20 楼 cfjtaishan 的回复:
[quote=引用 19 楼 ckc 的回复:] sizeof不是宏,也不是函数,也不算什么运算符吧 这个应该是编译器在编译的时候就可以预先计算的常量
宏就是在编译的时候预先计算的[/quote] 宏是在编译的时候展开的,并不一定是常量。而sizeof编译之后是一个常量。
自信男孩 2018-08-08
  • 打赏
  • 举报
回复
引用 19 楼 ckc 的回复:
sizeof不是宏,也不是函数,也不算什么运算符吧
这个应该是编译器在编译的时候就可以预先计算的常量

宏就是在编译的时候预先计算的
加载更多回复(18)

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧