sizeof

sizeof(struct) returns unexpected value

久未见 提交于 2019-12-17 13:51:29
问题 This should be simple but I have no clue where to look for the issue: I have a struct: struct region { public: long long int x; long long int y; long long int width; long long int height; unsigned char scale; }; When I do sizeof(region) it gives me 40 when I am expecting 33 . Any ideas? (mingw gcc, win x64 os) 回答1: It's padding the struct to fit an 8-byte boundary. So it actually is taking 40 bytes in memory - sizeof is returning the correct value. If you want it to only take 33 bytes then

iphone利用AudioQueue播放wav(PCM码)

烂漫一生 提交于 2019-12-17 10:54:52
续上一篇 iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等) 绝对原创,转载请注明出处: http://www.cnblogs.com/xuanyuanchen/admin/EditPosts.aspx?postid=2450169 1、ffmpeg解码音频流并且保存成wav文件。  这一步比较简单,只要熟悉ffmpeg解码音频的流程,将解码出的pcm码,保存到本地文件中,并实时统计解码的pcm的字节长度,最后解码完成之后再添加44字节的wav文件头。 save_audio.c View Code 1 #include <stdio.h> 2 #include "libavformat/avformat.h" 3 #include "libavcodec/avcodec.h" 4 #include "libavutil/avutil.h" 5 static void writeWavHeader(AVCodecContext *pCodecCtx,AVFormatContext *pFormatCtx,FILE *audioFile) { 6 int8_t *data; 7 int32_t long_temp; 8 int16_t short_temp; 9 int16_t BlockAlign; 10 int bits=16; 11 int32

How can I find the number of elements in an array?

隐身守侯 提交于 2019-12-17 10:36:15
问题 I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I'm not sure how to use it exactly. 回答1: If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements: #define NUM_OF_ELEMS 10 int arr[NUM_OF_ELEMS]; size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]); If you receive an array as a function argument or allocate an array in heap you can not determine

sizeof class with int , function, virtual function in C++?

只愿长相守 提交于 2019-12-17 10:23:30
问题 This is an online C++ test question, which has been done. #include<iostream> using namespace std; class A { }; class B { int i; }; class C { void foo(); }; class D { virtual void foo(); }; class E { int i ; virtual void foo(); }; class F { int i; void foo(); }; class G { void foo(); int i; void foo1(); }; class H { int i ; virtual void foo(); virtual void foo1(); }; int main() { cout <<"sizeof(class A) : " << sizeof(A) << endl ; cout <<"sizeof(class B) adding the member int i : " << sizeof(B)

Why is (sizeof(int) > -1) false? [duplicate]

别来无恙 提交于 2019-12-17 10:06:12
问题 This question already has answers here : Why should be there the involvement of type promotion in this code? (1 answer) Comparison operation on unsigned and signed integers (7 answers) Closed 4 years ago . Can You justify the below code: #include<stdio.h> int main() { if(sizeof(int) > -1) { printf("\nTrue\n"); } else { printf("\nFALSE\n"); } } The output is FALSE .....suggest me the reason 回答1: sizeof(int) has type size_t , which is an unsigned integer type. -1 has type int , which is a

Using sizeof() on malloc'd memory [duplicate]

左心房为你撑大大i 提交于 2019-12-17 09:43:27
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: newbie questions about malloc and sizeof I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code: void *mallocated = malloc(100); printf("sizeof(mallocated) = %d\n", sizeof(mallocated)); According to my program, the size of mallocated was 8 , even though I allocated 100 bytes for it. Because of this, whenever I try to store a string

矿场搭建(codevs 1996)

瘦欲@ 提交于 2019-12-17 06:24:56
题目描述 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图。为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处。于是矿主决定在某些挖煤点设立救援出口,使得无论哪一个挖煤点坍塌之后,其他挖煤点的工人都有一条道路通向救援出口。 请写一个程序,用来计算至少需要设置几个救援出口,以及不同最少救援出口的设置方案总数。 输入描述 Input Description 输入文件有若干组数据,每组数据的第一行是一个正整数N(N≤500),表示工地的隧道数,接下来的N 行每行是用空格隔开的两个整数S 和T,表示挖煤点S 与挖煤点T 由隧道直接连接。输入数据以0 结尾。 输出描述 Output Description 输入文件中有多少组数据,输出文件中就有多少行。每行对应一组输入数据的结果。其中第i 行以Case i: 开始(注意大小写,Case 与i 之间有空格,i 与:之间无空格,:之后有空格),其后是用空格隔开的两个正整数,第一个正整数表示对于第i 组输入数据至少需要设置几个救援出口,第二个正整数表示对于第i 组输入数据不同最少救援出口的设置方案总数。输入数据保证答案小于2^64。输出格式参照以下输入输出样例。 样例输入 Sample Input 9 1 3 4 1 3 5 1 2 2 6 1 5 6 3 1 6 3 2 6 1 2 1 3 2 4

Can you resize a C++ array after initialization? [duplicate]

北慕城南 提交于 2019-12-17 06:16:14
问题 This question already has answers here : How to resize array in C++? (5 answers) Closed last year . I'm learning to program, and C++ is my first language. Don't bother using pointers to show me - I don't understand them yet, and won't bother until I have more free time to dedicate to this. int mergeSort() { const int n = 9; int originalarray[n] = {1, 3, 5, 7, 9, 2, 4, 6, 8}; const int halfelements = (sizeof(originalarray) / sizeof(int)) / 2; int farray[halfelements]; int sarray[halfelements];

What is guaranteed about the size of a function pointer?

我的梦境 提交于 2019-12-17 04:32:20
问题 In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures: the size of a void* is the same size as a function pointer? the size of the function pointer does not differ due to its return type? the size of the function pointer does not differ due to its parameter types? I assume the answer is yes to all of these, but I want to be sure. For context, I'm calling sizeof(struct mystruct) and nothing more. 回答1: From C99

“C” sizeof with a type or variable

旧时模样 提交于 2019-12-17 03:41:48
问题 Recently saw someone commending another user on their use of sizeof var instead of sizeof(type). I always thought that was just a style choice. Is there any significant difference? As an example, the lines with f and ff were considered better than the lines with g and gg: typedef struct _foo {} foo; foo *f = malloc(count * sizeof f); foo *g = malloc(sizeof(foo) * count); foo **ff = malloc(count * sizeof *ff); foo **gg = malloc(sizeof(foo*) * count); In my opinion, the first set is just a