Search Results

Search found 1466 results on 59 pages for 'sizeof'.

Page 1/59 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • sizeof float (3.0) vs (3.0f)

    - by kumar
    Hi, What is the difference between sizeof(3.0) and sizeof(3.0f) I was expecting both of them to give the same result (sizeof float)..but its different. In 32 bit machine,gcc compiler, sizeof(3.0f) =4 sizeof(3.0) = 8 Why so?

    Read the article

  • Questions on usages of sizeof

    - by Appu
    Question 1 I have a struct like, struct foo { int a; char c; }; When I say sizeof(foo), i am getting 8 on my machine. As per my understanding, 4 bytes for int, 1 byte for char and 3 bytes for padding. Is that correct? Given a struct like the above, how will I find out how many bytes will be added as padding? Question 2 I am aware that sizeof can be used to calculate the size of an array. Mostly I have seen the usage like (foos is an array of foo) sizeof(foos)/sizeof(*foos) But I found that the following will also give same result. sizeof(foos) / sizeof(foo) Is there any difference in these two? Which one is preffered? Question 3 Consider the following statement. foo foos[] = {10,20,30}; When I do sizeof(foos) / sizeof(*foos), it gives 2. But the array has 3 elements. If I change the statement to foo foos[] = {{10},{20},{30}}; it gives correct result 3. Why is this happening? Any thoughts..

    Read the article

  • sizeof abuse : get the size of a const table

    - by shodanex
    When declaring a const table, it is possible to get the size of the table using sizeof. However, once you stop using the symbol name, it does not work anymore. is there a way to have the following program output the correct size for table A, instead of 0 ? #include <stdio.h> struct mystruct { int a; short b; }; const struct mystruct tableA[] ={ { .a = 1, .b = 2, }, { .a = 2, .b = 2, }, { .a = 3, .b = 2, }, }; const struct mystruct tableB[] ={ { .a = 1, .b = 2, }, { .a = 2, .b = 2, }, }; int main(int argc, char * argv[]) { int tbl_sz; const struct mystruct * table; table = tableA; tbl_sz = sizeof(table)/sizeof(struct mystruct); printf("size of table A : %d\n", tbl_sz); table = tableB; tbl_sz = sizeof(tableB)/sizeof(struct mystruct); printf("size of table B : %d\n", tbl_sz); return 0; } Output is : size of table A : 0 size of table B : 2

    Read the article

  • How does sizeof calculate the size of structures

    - by Gearoid Murphy
    I know that a char and an int are calculated as being 8 bytes on 32 bit architectures due to alignment, but I recently came across a situation where a structure with 3 shorts was reported as being 6 bytes by the sizeof operator. Code is as follows: #include <iostream> using namespace std ; struct IntAndChar { int a ; unsigned char b ; }; struct ThreeShorts { unsigned short a ; unsigned short b ; unsigned short c ; }; int main() { cout<<sizeof(IntAndChar)<<endl; // outputs '8' cout<<sizeof(ThreeShorts)<<endl; // outputs '6', I expected this to be '8' return 0 ; } Compiler : g++ (Debian 4.3.2-1.1) 4.3.2. This really puzzles me, why isn't alignment enforced for the structure containing 3 shorts?

    Read the article

  • Using sizeof with a dynamically allocated array

    - by robUK
    Hello, gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char *buffer = malloc(10240); /* Check for memory error */ if(!buffer) { fprintf(stderr, "Memory error\n"); return 1; } printf("sizeof(buffer) [ %d ]\n", sizeof(buffer)); However, the sizeof(buffer) always prints 4. I know that a char* is only 4 bytes. However, I have allocated the memory for 10kb. So shouldn't the size be 10240? I am wondering am I thinking right here? Many thanks for any suggestions,

    Read the article

  • Buffer size: N*sizeof(type) or sizeof(var)? C++

    - by flyout
    I am just starting with cpp and I've been following different examples to learn from them, and I see that buffer size is set in different ways, for example: char buffer[255]; StringCchPrintf(buffer, sizeof(buffer), TEXT("%s"), X); VS char buffer[255]; StringCchPrintf(buffer, 255*sizeof(char), TEXT("%s"), X); Which one is the correct way to use it? I've seen this in other functions like InternetReadFile, ZeroMemory and MultiByteToWideChar.

    Read the article

  • sizeof derived already from base

    - by Oops
    Hi, is it possible to return the sizeof a derived class already from base class/struct? imho the size of a class is a kind of property of itself, like the weight of a human being. But I don't want to write the same function in every class. many thanks in advance Oops

    Read the article

  • sizeof(class) c++

    - by badkya
    How is it possible for sizeof(class) to return two different values depending on context? The class itself should have the same size right irrespective of where I ask this question? I see a smaller value when querying class size from main() and another when I query class size from an extended class.

    Read the article

  • sizeof, size_t and dtddef.h

    - by yCalleecharan
    Hi, if I'm using the sizeof operator and making use of size_t in my code, do I have necessarily have to include the preprocessor directive stddef.h ? I haven't included the stddef.h and my code compiles without warning with both MVS2008 and with Borland C++ BuilderX. Thanks a lot...

    Read the article

  • sizeof continues to return 4 instead of actual size

    - by Guest
    #include <iostream> using namespace std; int main() { cout << "Do you need to encrypt or decrypt?" << endl; string message; getline(cin, message); int letter2number; for (int place = 1; place < sizeof(message); place++) { letter2number = static_cast<int>(message[place]); cout << letter2number << endl; } } Examples of problem: I type fifteen letters but only four integers are printed. I type seven letters but only four integers are printed. The loop only occurs four times on my computer, not the number of characters in the string. This is the only problem I am having with it, so if you see other errors, please don't tell me. (It is more fun that way.) Thank you for your time.

    Read the article

  • Sizeof struct in GO

    - by Homer J. Simpson
    I'm having a look at Go, which looks quite promising. I am trying to figure out how to get the size of a go struct, for example something like type Coord3d struct { X, Y, Z int64 } Of course I know that it's 24 bytes, but I'd like to know it programmatically.. Do you have any ideas how to do this ?

    Read the article

  • base class , inheritate class sizeof()

    - by user1279988
    why sizeof(X) is 4 and sizeof(Y) is 8? and another question, in class X, only the int(i) count as sizeof() 4? member function does take any memory space? Plz tell me, thanks! class X { int i; public: X() { i = 0; } void set(int ii) { i = ii; } int read() const { return i; } int permute() { return i = i * 47; } }; class Y : public X { int i; // Different from X's i public: Y() { i = 0; } int change() { i = permute(); // Different name call return i; } void set(int ii) { i = ii; X::set(ii); // Same-name function call } }; cout << "sizeof(X) = " << sizeof(X) << endl; cout << "sizeof(Y) = "

    Read the article

  • When should an array name be treated as a pointer and when does it just represent the array itself? [duplicate]

    - by user1087373
    This question already has an answer here: When is an array name or a function name 'converted' into a pointer ? (in C) 4 answers I just made a test program after reading the book and the result turned out confusing: #include <stdio.h> int main(void) { char text[] = "hello!"; printf("sizeof(text):%d sizeof(text+2):%d sizeof(text[0]):%d \n",(int)sizeof(text), sizeof(text+2), sizeof(text[0])); printf("text:%p sizeof(text):%d &text:%p sizeof(&text):%d \n",text, sizeof(text), &text, sizeof(&text)); printf("text+1:%p &text+1:%p \n", text+1, &text+1); return 0; } The result: sizeof(text):7 sizeof(text+2):4 sizeof(text[0]):1 text:0xbfc8769d sizeof(text):7 &text:0xbfc8769d sizeof(&text):4 text+1:0xbfc8769e &text+1:0xbfc876a4 What makes me feel confused are: why the value of 'sizeof(text)' is 7 whereas 'sizeof(text+2)' is 4 what's the difference between 'text' and '&text'?

    Read the article

  • how does sizeof work with array in C++

    - by skydoor
    Hi, I was told the array name is a pointer char a[] = "a" but when sizeof(a) = 2 why not it is a size of pointer here? however when I define it like this char* a ="a"; we get sizeof(a) =4; Well, I think I need more information about how does sizeof work with the array names.... Can anyone elaborate that?

    Read the article

  • Is this a correct syntax (c code found on wikipedia)?

    - by m4design
    I just found this code on wikipedia. Link: http://en.wikipedia.org/wiki/Sizeof#Use The code: /* the following code illustrates the use of sizeof * with variables and expressions (no parentheses needed), * and with type names (parentheses needed) */ char c; printf("%zu,%zu", sizeof c, sizeof(int)); It states that: "The z prefix should be used to print it, because the actual size can differ on each architecture." I tried it on my compiler, but it gives the following result: 'zu,zu'

    Read the article

  • C++: sizeof for array length

    - by Rosarch
    Let's say I have a macro called LengthOf(array): sizeof array / sizeof array[0] When I make a new array of size 23, shouldn't I get 23 back for LengthOf? WCHAR* str = new WCHAR[23]; str[22] = '\0'; size_t len = LengthOf(str); // len == 4 Why does len == 4? UPDATE: I made a typo, it's a WCHAR*, not a WCHAR**.

    Read the article

  • Pointer Implementation Details in C

    - by Will Bickford
    I would like to know architectures which violate the assumptions I've listed below. Also I would like to know if any of the assumptions are false for all architectures (i.e. if any of them are just completely wrong). sizeof(int *) == sizeof(char *) == sizeof(void *) == sizeof(func_ptr *) The in-memory representation of all pointers for a given architecture is the same regardless of the data type pointed to. The in-memory representation of a pointer is the same as an integer of the same bit length as the architecture. Multiplication and division of pointer data types are only forbidden by the compiler. NOTE: Yes I know this is nonsensical. What I mean is - is there hardware support to forbid this incorrect usage? All pointer values can be casted to a single integer. In other words, what architectures still make use of segments and offsets? Incrementing a pointer is equivalent to adding sizeof(the pointed data type) to the memory address stored by the pointer. If p is an int32* then p+1 is equal to the memory address 4 bytes after p. I'm most used to pointers being used in a contiguous, virtual memory space. For that usage, I can generally get by thinking of them as addresses on a number line. See (http://stackoverflow.com/questions/1350471/pointer-comparison/1350488#1350488).

    Read the article

  • Passing array to function with pointer loses array size information!

    - by Narek
    If I write int main() { int a[100] = {1,2,3,4,}; cout<<sizeof(a)/sizeof(a[0])<<endl; return 0; } I get 400! If I write void func(int *a); int main() { int a[100] = {1,2,3,4,}; func(a); return 0; } void func(int *a) { cout<<sizeof(a)/sizeof(a[0])<<endl; } Then I get 400! So why passing array to function with pointer loses array size information?

    Read the article

  • the sizeof why ??!!

    - by hiba salem
    hi I hve this code struct Student { char name[48]; float grade; int marks[10,5]; char gender; }; Student s; Now I have to get the sizeof s so i added printf("%d",sizeof(s)); now when I hit compile the result showing is 256 and its wrong because it shoud be 253 as because the size of char name[48]; ---- 48 and float grade; ----- 4 and int marks[10,5]; ------ 200 and char gender; -------1 so 48+4+200+1 = 253 so why its telling me 256 ??

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >