FAQ Database Discussion Community


What's the relationship between size of integer and complex objects?

python,memory,sizeof,complex-numbers
I recently heard about Python integer cache. After having searched on internet, I have found this well-written article : http://www.laurentluce.com/posts/python-integer-objects-implementation. It explains what I wanted to know about this subject. In this article, it explains why a integer object is not only 16 or 32 bits wide, as in C....

In C++11, why does int16_t have a size of 4 when declared after a float inside a struct?

c++11,alignment,structure,sizeof,uint16
I have a data structure like this: struct mystruct { float f; int16_t i; }; sizeof(int16_t) gives 2, but sizeof(mystruct) gives 8 instead of 6. Why is that? How can I declare an int16_t variable of 2 bytes inside my data structure?...

Why is my string truncated when copied?

c,string,pointers,malloc,sizeof
I'm trying to find whether less is installed using C (with no use of system calls). But there is something wrong with my copying of the variable. The contents of the string is truncated: int ret; char * pathValue; char * pathValue2; char *token3; char * new_str ; int pathlength;...

Difference sizeof in C++ Classes with virtual parameter

c++,class,virtual,sizeof
I have this code: #include <stdio.h> class CoolClass { public: virtual void set(int x){x_=x;}; virtual int get(){return x_;}; private: int x_; }; class PlainOldClass { public: void set(int x){x_=x;}; int get(){return x_;}; private: int x_; }; int main () { printf("CoolClass:\t %ld \nPlainOldClass:\t %ld \n", sizeof(CoolClass),sizeof(PlainOldClass)); return 0; } And...

Size of a pointer in C [duplicate]

c,pointers,sizeof
This question already has an answer here: Get size of pointer in C 3 answers How can I find the size of the type "pointer?" For example, if I want to know the size of an integer, I can use "sizeof(int)". What is the equivalent for a pointer? Is...

Why sizeof(Point) is 8?

c#,.net,sizeof
i wrote code and get strange result- integer i is 8: unsafe { int i = sizeof(Point); } After checking struct Point i found this fields: public bool IsEmpty { get; } public int X { get; set; } public int Y { get; set; } bits math: 32+32+1 =...

How does `sizeof` distinguish a pointer from an array?

c,sizeof
Note that if a is an array name, then sizeof(a) will yields the size of the entire array a and not the size of a pointer to one of its elements. So for example, how does sizeof distinguish an array a and a pointer b? #include <stdio.h> #include <stdlib.h> int...

unexpected output while using strlen function

pointers,sizeof,calloc
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char *p; p=calloc(10,sizeof(char)); printf("the address of pointer is %p and size of the string is %d",p,strlen(p)); return 0; } I allocated 10 bytes of memory in heap using calloc and i expect the output of size will be 10 but the output is the...

Is sizeof a keyword in Java [closed]

java,sizeof
I think this is a simple question: Is sizeof is a keyword in Java? Some people also say call it an operator, some call it a method, and some call it a command. What is it?

sizeof- function or macro? [duplicate]

c,sizeof
This question already has an answer here: Why is sizeof considered as an operator? 10 answers In c, we are using the sizeof() for getting the size of the datatypes. So how it is defined. It is a macro or a function. Because we can use that as two...

Why is the structure showing less memory than actual memory in the following programming [closed]

c,pointers,struct,sizeof
I wrote a program include<stdio.h> struct record { int i; char ch[20]; }; int main() { struct record a,*b; b=&a; printf("intial pointer is %p and final is %p",b++,b); return 0; } The size of a should be 20+4=24 but the output is intial pointer is 0x7fffb0455e40 and final is 0x7fffb0455e58...

The size of an initialized array as element of the array (USB descriptor)

c++,arrays,sizeof
I want to have a construct similar to this (invalid) one: const uint8_t uArray[] = { uint8_t(sizeof(uArray)), 1, 2, 3 }; and the uArray[0] should be 4 and be filled automatically at the compile time, this is the situation for USB descriptors definition that are usually expressed as an array...

Is there any chance the sizeof operator returns 0?

c,jni,sizeof
I require to return a value of sizeof(some_t) from C to Java. JNIEXPORT jint JNICALL blar(blar) { #ifndef some_t return ?; #else return sizeof(some_t); #endif As you can see I must return some value for the case the type is not even defined. So what's the best value for that...

How is sizeof(char *) and sizeof(char) different?

c,arrays,string,pointers,sizeof
#include<stdio.h> main() { char *str[]= {"Frogs","do","not","die","they","croak"}; printf("%d %d %d",sizeof(str),sizeof(str[0]),sizeof(char)); } output is: 48 8 1 According to size of char being 1 byte and there are six character variables so total size of array should be 6 instead of 48!...

Swift calculates wrong size of struct

xcode,swift,struct,sizeof
Today I encountered a problem with Swift structs. See the following struct for example: struct TestStruct { var test: UInt16 var test2: UInt32 } I would assume that the size of this struct in total is 6 bytes, because UInt16 is 2 bytes and UInt32 is 4 bytes large. The...

Malloc fails with errno 12

c,file-io,malloc,sizeof
I read/write data into a custom raw file where I used to write version (int) data I implemented a more generic way to write my data and now the expected format is headersize (size_t) header (headersize) data Therefore, I manually added 4 (sizeof(int)) to an old raw file (with ghex)...

c++ gets different array length when passing the array as a arguments to a fucntion [duplicate]

c++,arrays,sizeof
This question already has an answer here: Sizeof array passed as parameter 3 answers In the below simple c++ program, I'm trying to get the length of an array by using sizeof, for same array, why when passing the array as an argument to a function the array length...

strlen sometimes equal to sizeof for null-terminated strings

c,string,null,sizeof,strlen
I know that strlen counts the number of characters up until (and excluding) the null character '\0' (or 0) and that sizeof gives the amount of space needed to store the string including the null character, but am confused with the output of my code. Question: I expect the result...

sizeof applied to array types

c,arrays,sizeof,c11
The c11 standard says that sizeof, "when applied to an operand that has array type, the result is the total number of bytes in the array" (6.5.3.4, bullet 4). The foot note (103) says: "When applied to a parameter declared to have array or function type, the sizeof operator yields...

Understanding memory alignment constraints and padding bytes in C

c,struct,padding,sizeof
I have the following code snippet. #include<stdio.h> int main(){ typedef struct{ int a; int b; int c; char ch1; int d; } str; printf("Size: %d \n",sizeof(str)); return 0; } Which is giving output as follows Size: 20 I know that size of the structure is greater than the summation of...

Sizeof(char[]) in C

c,arrays,sizeof
Consider this code: char name[]="123"; char name1[]="1234"; And this result The size of name (char[]):4 The size of name1 (char[]):5 Why the size of char[] is always plus one?...

The standard way to get sizeof(promoted(x)) [duplicate]

c++,sizeof,type-promotion
This question already has an answer here: Type trait to obtain default argument promotions 3 answers Is there a standard way to get the size of the type a variable would be promoted to when passed as a variadic argument? auto x = ...; auto y = sizeof(promoted(x)); The...

size of a node in linked list

c++,malloc,sizeof
Program: #include <iostream> #include <stdlib.h> using namespace std; struct node { int data; struct node *next; }; int main() { struct node* head = NULL; head = (struct node*)malloc(sizeof(struct node)); cout<<sizeof(struct node)<<"\n"<<sizeof(head)<<"\n"<<sizeof(int); return 0; } Output: 8 4 4 Why is sizeof(struct node) different than sizeof(head) ? Wouldn't have malloc...

How best to prevent unused variable warnings in custom assert without sizeof?

c++,c++11,lambda,assert,sizeof
Based on advice in http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ I've been working with my own version of assert (called emp_assert) for a while now. As such, when NDEBUG is set, my assert looks like: #define emp_assert(EXPR) ((void) sizeof(EXPR) ) This definition assures that any variables in EXPR still count as "used" in the compiler,...

Inconsistency for size_t and sizeof

c++,sizeof
Isn't it bothersome that the result of a native operator cannot be defined without including a header file? According to this page, size_t is defined in headers cstddef, cstdio, cstring, ctime, and cstdlib. Thus, if neither of those headers is included then size_t should be undefined. However, the following program...

Why padding are added, if char comes after int?

c,struct,byte,padding,sizeof
For example, there is a structure struct A { char a; int i; }; In this case, we have a[1 byte] + padding[3 byte] + int[4 byte] = 8. Now let's make little update into struct above, struct A { int i; char a; }; In this case char comes...

size of array allocated with malloc is showing one less than expected

c,arrays,pointers,malloc,sizeof
I have this: alloc(Btree *bt, uint8_t *keylen, int16_t n) { bt->node[n].key = malloc(sizeof(int16_t)*(*keylen)); { Where bt->node[n].key is a pointer to int16_t. With the debugger running, I can verify that keylen is 5. But if I put: int kl = sizeof(bt->node[n].key) / sizeof(bt->node[n].key[0]) kl is 4. What did I do wrong?...

Why this C programming code output is False? expected output is true

c,if-statement,sizeof
We know that sizeof(int) = 4 and 4 > -1 is true so the expected output of the following piece of code is true. However, it's printing "False". Why is wrong? #include <stdio.h> main(){ if (sizeof(int) > -1) printf("True"); else printf("False"); } ...

Size of pointer, pointer to pointer in C

c,pointers,sizeof
How can I justify the output of the below C program? #include <stdio.h> char *c[] = {"Mahesh", "Ganesh", "999", "333"}; char *a; char **cp[] = {c+3, c+2, c+1, c}; char ***cpp = cp; int main(void) { printf("%d %d %d %d ",sizeof(a),sizeof(c),sizeof(cp),sizeof(cpp)); return 0; } Prints 4 16 16 4 Why?...

How to get the length of the line being read in from fgets and loop over it

c,for-loop,sizeof,fgets
I'm trying to loop over each character of a line within fgets, but I am running into errors, as I'll explain below. int main(int argc, char* argv[]) { char arr[200] int currS = sizeof(arr) / sizeof(char); // call readIt function } void readIt(char* argv[], char arr[], int currS) { FILE*...

Dynamic Array Size in C++

c++,arrays,dynamic,int,sizeof
I am looking for a way to dynamically set the size of an integer array depending on the passed parameter. For example this in pseudocode: int MyFunction(int number) { int myarr[amount of digits in number]; } So when the input is 13456 then the int array[] size should be 5....

C++ address value and sizeof

c++,c,sizeof
On my computer. When I test the code: int main() { int i=123; return 0; } using g++ -g test.cpp -o test I found when I enter: print &i output: 0x7fffffffe18c print sizeof(&i) output: 8 I was confused, the address of i is 6 byte, why sizeof(&i)==8? Thanks a lot...

How is C sizeof() returning these values

c,arrays,pointers,sizeof
I've got the code below in trying to understand memory allocation under C. The comments next to the line indicate the output I got. Apart from the char size that comes up I'm confused, though. Is this output standard? If so, how is it coming to these returns? char *pk[]...

What determines object size here?

c++,sizeof
I have created simple object containing integer and few methods, integer primitive variable alone and compare their sizes. "sizeof()" for both returned value of "4". Why is that - shouldn't object that is composite type and contains information about methods take more space? #include <iostream> class Person{ private: int a;...

Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

c++,language-lawyer,sizeof,undefined-behavior,null-pointer
First of all, I've seen this question about C99 and the accepted answer references operand is not evaluated wording in the C99 Standard draft. I'm not sure this answer applies to C++03. There's also this question about C++ that has an accepted answer citing similar wording and also In some...

Do parentheses make a difference when determining the size of an array?

c,arrays,language-lawyer,sizeof,parentheses
The following program prints the same number twice on gcc 4.8.2: #include <stdio.h> int main() { char a[13]; printf("sizeof a is %zu\n", sizeof a ); printf("sizeof(a) is %zu\n", sizeof(a)); } According to this reddit post, gcc is not standard-conformant in this respect, because a parenthesized expression is not on the...