This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 7 years ago.
I made this minimal working example of a larger piece of code I have.
The problem is, that sizeof prints for the first call 16, which is the correct size, and for the 2nd call only 8. The wierd thing is, it always prints 8, independent of the size of the struct, whether there's only one value or 10 values in it.
struct test_struct
{
int32_t val1;
int32_t val2;
int32_t val3;
int32_t val4;
};
unsigned char * StructToChar(test_struct structy)
{
unsigned char returnval[sizeof(structy)];
memcpy(returnval, &structy, sizeof(structy));
return returnval;
}
int main()
{
test_struct sendstruct = {};
unsigned char *test_array = StructToChar(sendstruct);
unsigned char returnval[sizeof(sendstruct)];
memcpy(returnval, &sendstruct, sizeof(sendstruct));
printf("%d\n", sizeof(returnval));
printf("%d\n", sizeof(test_array));
return 0;
}
sizeof(test_array) is sizeof(unsigned char *) so size of a pointer.
Related
This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Why does sizeof show 4 bytes for a char? [duplicate]
(1 answer)
Closed last year.
Why size of operator is showing 12 bytes instead of 9 for the below code in c++.
As int occupies 4 bytes, float occupies 4 bytes and character occupies 1 byte, then sizeof should give 9 bytes as output instead of 12.
Object stores memory for data members only not member functions (methods).
#include <iostream>
using namespace std;
class Sample{
public :
int i;
float a;
char ch;
public:
Sample(int j, float b, char dh){
i=j; a=b; ch=dh;
}
};
int main(){
Sample s1(10,3.14f, 'A');
Sample s2(20,6.28f,'B');
cout<<sizeof(s1)<<endl;
cout<<sizeof(s2)<<endl;
return 0;
}
This question already has answers here:
Returning pointer to local function variable [duplicate]
(2 answers)
Closed 4 years ago.
First of all, I am using C++ and I am a newbie on it. I have an interface with exposes a method unsigned char * readRequest(). All of my classes inherit this interface and implement the unsigned char * readRequest() in its way (dealing with float, string, int, double, char), but return unsigned char *. Below are some of my implementations of readRequest() that converts float to unsigned char *.
Edited: I am returning a pointer to a local variable and this is an undefined behavior. How do I return the real value in C++?
unsigned char * readRequest() {
float preCent = distanceCentimeters();
std::cout << preCent << std::endl; // print correct result
unsigned char buf[4];
memcpy(buf, &preCent, sizeof(preCent));
float result;
memcpy(&result, buf, sizeof(result));
std::cout << result << std::endl; // print correct result
return buf;
}
int main(void) {
unsigned char *readRequestArray;
readRequestArray = (unsigned char*) readRequest();
float converted_back;
memcpy(&converted_back, readRequestArray, sizeof converted_back);
std::cout << converted_back; // ERROR due to return a pointer to local variable
}
Output and error:
24.2882
jN�A
Segmentation fault
You are returning a pointer to local variables. This doesn't work and will be raised by your compiler in the warnings.
Allocated data and return a unique pointer, or just simply use a string, as you are passing memory management to the caller. You are in C++, use C++ style code, not C (printf and others).
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
I made a function in C++ to find the length of an array. I find the sizeof the array passed in the argument and divide it by the sizeof the variable type. This should work but it always returns 1! Am I missing something obvious? Or does this have to do with pointers and memory? This is my code:
#include <iostream>
using namespace std;
int lengthOf(int arr[]);
int main() {
int x[] = {1,2,3,0,9,8};
int lenX = lengthOf(x);
cout << lenX;
return 0;
}
int lengthOf(int arr[]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
Output (should be 6 instead of 1):
4
1
I am fairly new so excuse me if this is a bad question.
When passing an array as parameter, it always decays into a pointer. If you want to see the size of the array, you need to pass the array by reference, e.g.:
template <int Size>
int lengthOf(int (&array)[Size]) {
return Size;
}
You should use the pointer.
(sizeof(arr)/sizeof(*arr))
Even though int arr[] looks like you are passing an array, you are actually passing a pointer. int arr[] is equivalent to int* arr when used as a function parameter, this comes from C.
In C++, if you want to pass an array, the proper way is to do it by reference:
template <int N>
int lengthOf(int (&arr)[N]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t sizeof for a struct equal to the sum of sizeof of each member?
How is the size of a C++ class determined?
When I check the size of the class with single char variable, it's size is 1 Byte. But if we add an integer, suddenly it goes to 8. Can you please explain Why?
class Char
{
char b;
};
class Int
{
int a;
};
class A
{
int a;
char b;
};
int main()
{
Char Cobj;
cout<<"Char size: "<<sizeof(Cobj)<<endl;
Int Iobj;
cout<<"Int size: "<<sizeof(Iobj)<<endl;
A aobj;
cout<<"A size: "<<sizeof(aobj)<<endl;
return 0;
}
The output is:
Char size: 1
Int size: 4
A size: 8
Because of padding - 3 dummy bytes will be added after A::b.
This is done to properly align A in, say, an array - the first member of A is an int, which has to have a specific alignment (4 or 8 bytes probably). So if you have
A arrayOfA[10];
the objects themselves have to be aligned to 4 or 8 bytes.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)
I have the following code
int myfunc(char *arr)
{
const int sizearr = sizeof( arr ) / sizeof(char);
}
This yields the size of the pointer, not the size of the char array[17] I passed to the function.
I know that I may use strlen and solve the problem, but is there any way to convert the char*arr pointer back to the char array[17] I passed to the function in order to use the sizeof(char array[17]) ?
If you want to pass an array of 17 chars, you need to pass it by reference (C++ only):
int myfunc(char (&arr)[17])
{
unsigned int const sizearr = sizeof arr;
// ...
}
Or make a template to deduce the size:
template <unsigned int sizearr>
int myfunc(char (&arr)[sizearr])
{
// ...
}
In C, you cannot pass arrays as arguments at all, so it is your responsibility to communicate the array size separately.
Update: As #David suggests, you can fix the array type in C by passing a pointer to the array:
int myfunc(char (*arr)[17])
{
unsigned int const sizearr = sizeof *arr;
// ...
}
int main()
{
char s[17];
return myfunc(&s);
}
No, there is no such way. But in C++ you can make function template parametrized by array size and pass array by reference:
template <size_t size> void myfunc(char (&arr)[size])
{
for(size_t i = 0; i < size; ++i) std::cout << arr[i];
}
// Usage
char array[] = "My char array";
myfunc(array);
No.
There is no such way. The type char * does not contain any information about the "source", i.e. the array. This is why it's said that the array "decays" into a pointer; the pointer has less information than the array.
Also, sizeof (char) is 1.
Not really, because once the array has decayed to a pointer all information regarding its "original" form has been lost. You can always cast the pointer to a char[17] but that's bad practice.
If you really need to have an array, change the function signature to accept an array.