PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label sizeof. Show all posts
Showing posts with label sizeof. Show all posts

Wednesday, October 19, 2022

[FIXED] Why does a C-Array have a wrong sizeof() value when it's passed to a function?

 October 19, 2022     arrays, c, c++, function, sizeof     No comments   

Issue

Complete example:

#include <stdio.h>

void test(int arr[]) {
    int arrSize = (int)(sizeof(arr) / sizeof(arr[0]));
    printf("%d\n", arrSize); // 2 (wrong?!)
}

int main (int argc, const char * argv[]) {
    int point[3] = {50, 30, 12};

    int arrSize = (int)(sizeof(point) / sizeof(point[0]));
    printf("%d\n", arrSize); // 3 (correct :-) )

    test(point);

    return 0;
}

Before passing it to a function, sizeof gives me the correct value. Doing the exact same thing on the exact same array in the function gives weird results. There's one element missing. Why?


Solution

When you pass an array into a function in C, the array decays into a pointer to its first element. When you use sizeof on the parameter, you are taking the size of the pointer, not the array itself.

If you need the function to know the size of the array, you should pass it as a separate parameter:

void test(int arr[], size_t elems) {
   /* ... */
}

int main(int argc, const char * argv[]) {
   int point[3] = {50, 30, 12};
   /* ... */
   test(point, sizeof(point)/sizeof(point[0]));
   /* ... */
}

Also note that, for a similar reason (taking the sizeof a pointer), the sizeof(point)/sizeof(point[0]) trick doesn't work for a dynamically allocated array, only an array allocated on the stack.



Answered By - Nick Meyer
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, July 10, 2022

[FIXED] Why does the sizeof a class give different output in C++?

 July 10, 2022     c++, reference, sizeof     No comments   

Issue

According to the cppreference,

When applied to a reference type, the result is the size of the referenced type.

But in the following program, compiler is giving different output.

#include <iostream>    
using namespace std;

class A 
{
    private:
        char ch;
        const char &ref = ch;
};

int main()
{
    cout<<sizeof(A)<<endl;
    return 0;
}

Output:

16

Here ch is of a character type and the reference is also of type character. So output would be 2 bytes instead of 16 bytes.

Online compiler: GDB


Solution

Firstly you're asking for the size of the object, not of the reference type itself.

sizeof(A::ref) will equal 1:

class A 
{
    public:
        char ch;
        const char &ref = ch;
};

int main()
{
    cout<<sizeof(A::ref)<<endl;
    return 0;
}

The object size is 16 because:

  1. The actual size taken up by the reference type inside the object is equal to the size of a pointer (8 in this case).
  2. Because the object alignment has increased to 8 due to the reference type, the char now also takes up 8 bytes even though it only really uses 1 byte of that space.

I.e. If you were to change char ch to char ch[8], sizeof(A) would still equal 16:

class A 
{
    private:
        char ch[8];
        const char &ref = ch[0];
};

int main()
{
    cout<<sizeof(A)<<endl;
    return 0;
}


Answered By - Tom Clarke
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, July 6, 2022

[FIXED] How can I get the length of an array that was passed to a function?

 July 06, 2022     arrays, c++, function-definition, pass-by-reference, sizeof     No comments   

Issue

Given the following function:

template<class T>
void foo(T* ar)
{
    std::cout << sizeof(ar) / sizeof(ar[0]) << std::endl;
    for (int i = 0; i < 6; i++)
        std::cout << ar[i] << " ";
}

And following array:

int ar[] = { 5, 1, 6, 8, 9, 3 };
foo(ar);

I expected the program to print '6' and then the contents of the array. But for some reason, sizeof(ar) / sizeof(ar[0]) evaluates to 1 (since both sizeof(ar) and sizeof(ar[0]) evaluate to '4'), but the print works fine (meaning passed array does contain all 6 elements).

This only happens to arrays passed to functions (I tried evaluating length in main(), where array was declared and it worked fine). How can I get the length of an array inside a function?


Solution

Just declare the parameter as having a referenced type.

For example

template<class T>
void foo( const T &ar )
{
    size_t n = sizeof(ar) / sizeof(ar[0]);
    std::cout << n << std::endl;
    for (int i = 0; i < n; i++)
        std::cout << ar[i] << " ";
}

Or you may use the following declaration

template<class T, size_t N>
void foo( const T ( &ar )[N] )
{
    std::cout << N << std::endl;
    for (int i = 0; i < N; i++)
        std::cout << ar[i] << " ";
}

Otherwise the function deals with a pointer. In this case you need specify a second parameter that denotes the length of the array like

template<class T>
void foo( const T* ar, size_t n )
{
    std::cout << n << std::endl;
    for (int i = 0; i < n; i++)
        std::cout << ar[i] << " ";
}

As the passed array is not being changed in the function then the corresponding parameter should have the qualifier const.

Pay attention to that in the C++ 17 Standard there is introduced standard function std::size declared in the header <iterator> that can be used instead of the expression sizeof(ar) / sizeof(ar[0])

So you could write for example in the first shown function

size_t n = std::size(ar)`;


Answered By - Vlad from Moscow
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 28, 2022

[FIXED] Why do I get a warning every time I use malloc?

 April 28, 2022     c, gcc, malloc, sizeof, warnings     No comments   

Issue

If I use malloc in my code:

int *x = malloc(sizeof(int));

I get this warning from gcc:

new.c:7: warning: implicit declaration of function ‘malloc’  
new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’

Solution

You need to add:

#include <stdlib.h>

This file includes the declaration for the built-in function malloc. If you don't do that, the compiler thinks you want to define your own function named malloc and it warns you because:

  1. You don't explicitly declare it and
  2. There already is a built-in function by that name which has a different signature than the one that was implicitly declared (when a function is declared implicitly, its return and argument types are assumed to be int, which isn't compatible with the built-in malloc, which takes a size_t and returns a void*).


Answered By - sepp2k
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing