Les Partages de Memiks
Tag cloud
Picture wall
Daily
RSS Feed
  • RSS Feed
  • Daily Feed
  • Weekly Feed
  • Monthly Feed
Filters

Links per page

  • 20 links
  • 50 links
  • 100 links

Filters

Untagged links
3 results tagged sizeof  ✕
getting size of array from pointer c++ - Stack Overflow https://stackoverflow.com/questions/19894686/getting-size-of-array-from-pointer-c/19894884
Sat Feb 6 14:03:38 2021 archive.org
QRCode
thumbnail

C++ is based on C and inherits many features from it. In relation to this question, it inherits something called "array/pointer equivalence" which is a rule that allows an array to decay to a pointer, especially when being passed as a function argument. It doesn't mean that an array is a pointer, it just means that it can decay to one.

void func(int ptr); int array[5]; int ptr = array; // valid, equivalent to 'ptr = &array[0]' func(array); // equivalent to func(&array[0]);

This last part is the most relevant to your question. You are not passing the array, you are passing the address of the 0th element.

In order for your function to know how big the incoming array is, you will need to send that information as an argument.

static const size_t ArraySize = 5; int array[ArraySize]; func(array, ArraySize);

Because the pointer contains no size information, you can't use sizeof.

void func(int* array) { std::cout << sizeof(array) << "\n"; }

This will output the size of "int*" - which is 4 or 8 bytes depending on 32 vs 64 bits.

Instead you need to accept the size parameters

void func(int* array, size_t arraySize); static const size_t ArraySize = 5; int array[ArraySize]; func(array, ArraySize);

Even if you try to pass a fixed-sized array, it turns out this is syntactic sugar:

void func(int array[5]);

http://ideone.com/gaSl6J

Remember how I said that an array is NOT a pointer, just equivalent?

int array[5]; int* ptr = array; std::cout << "array size " << sizeof(array) << std::endl; std::cout << "ptr size " << sizeof(ptr) << str::endl;

array size will be 5 sizeof(int) = 20 ptr size will be sizeof(int ) which will be either 4 or 8 bytes.

sizeof returns the size of the type being supplied, if you supply an object then it deduces the type and returns the size of that

If you want to know how many elements of an array are in the array, when you have the array and not a pointer, you can write

sizeof(array) / sizeof(array[0])

or sizeof(array) / sizeof(*array)

cpp pointer sizeof memory
getting size of array from pointer c++ - Stack Overflow https://stackoverflow.com/questions/19894686/getting-size-of-array-from-pointer-c/19894884
Sat Feb 6 14:03:38 2021 archive.org
QRCode
thumbnail

I am writing a simple function that returns the largest integer in an array. The problem I am having is finding the number of elements in the array.

Here is the function header:

int largest(int ...

cpp pointer sizeof memory
Developing USB Device Drivers from Userspace using Libusb - Linux Forums http://www.linuxforums.org/forum/linux-tutorials-howtos-reference-material/10865-developing-usb-device-drivers-userspace-using-libusb.html
Thu Oct 20 07:09:02 2011 archive.org
QRCode
thumbnail

Developing USB Device Drivers from Userspace using Libusb Linux Tutorials, HOWTO's & Reference Material

Developing USB Device Drivers Userspace Libusb device printf libusb linux 125 123 connected kernel system driver program bmattributes devices usbfs platform 40i interface programming i void binterval developing manufacturer filesystem bendpointaddress wmaxpacketsize bsynchaddress string drivers howto brefresh structures 02xhn develop developers stringn language fetch product sizeof 40string http sourceforge net usb get simple 40udev developer userspace reveals 40ret serial computer NOTE if read tutorial are still experiencing difficulties would like help asked start new topic on forums Please do NOT reply this thread ask technical question Replies THIS should be corrections enhancements only Thanks advance for co operation LinuxForums org Linux Programming Interface
4759 links, including 1673 private
Shaarli - The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community - Theme by kalvn