I am generating arrays which are dynamic size. The part of the code that I am showing is grabbing value of the array the way it does, and it works.
The problem is I have no idea how this work. I don't see why both cast and pre-cast have pointer in it?
How to code something similar correctly?
Example: *(double*)&j;
I also noticed that *(int*)&column_sum[i] + 1; won't add the 1 to the result. Also I have no idea why...
double val = 1000 * multiplier;
double *column_sum = malloc(val * sizeof *column_sum);
double *p = malloc(val * val * sizeof *p);
printf("Rows/Columns: %.0f", val);
for (i = 0; i < val; i++){
column_sum[i] = 0.0;
for (j = 0; j < val; j++){
int index = i * (int)val + j;
p[index] = *(double*)&j; // here
int offsetI = *(int*)&column_sum[i] + 1; // here
int offsetJ = *(int*)&p[index] + 1; // here
printf("%d->", offsetI);
printf("%d,", offsetJ);
}
printf("\n");
}
What does it do:
&var // get the pointer of the variable
(type*)&var // cast the pointer to an other pointer type
*(type*)&var // Dereferencing the casted pointer so "interpret" the variable as a "type"
What is important here, is that it is interpret and not cast.
We can see the difference in this example:
float a = 0.5;
int b = (int)a;
int c = *(int*)&a;
printf("%f %08x %08x\n", a, b, c);
// Output:
// 0.500000 00000000 3f000000
// 3f000000 is the way 0.5 is encoding following standard IEEE 754
It's usefull if you want to work on representation of floats for example:
float a = 1.5;
int b = *(int*)&a;
b &= 0xF;
a = *(float*)&b;
For example it's the reason of the use of this syntax here: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code, for manipulating bit of representation of double.
Related
I have this code:
int * generate_code(int *bits, int Fs, int size, int *signal_size, float frameRate)
{
int sign_prev, i;
int bit, t, j=0;
int *x;
float F0, N, t0, prev_i, F1;
int temp = 0, temp1, temp2;
F0 = frameRate * BITS_PER_FRAME; // Frequency of a train of '0's = 2.4kHz
F1 = 2*F0; // Frequency of a train of '1's = 4.8kHz
N = 2*(float)Fs/F1; // number of samples in one bit
sign_prev = -1;
prev_i = 0;
x = (int *)malloc(sizeof(int));
for( i = 0 ; i < size ; i++)
{
t0 = (i + 1)*N;
bit = bits[i];
if( bit == 1 )
{
temp1 = (int)round(t0-N/2)-(int)round(prev_i+1)+1;
temp2 = (int)round(t0)-(int)round(t0-N/2+1)+1;
temp =j + temp1 + temp2;
//printf("%d\n", (int)temp);
x = realloc(x, sizeof(int)*temp); // 1
for(t=(int)round(prev_i+1); t<=(int)round(t0-N/2); t++)
{
*(x + j) = -sign_prev;
j++;
}
prev_i = t0-N/2;
for(t=(int)round(prev_i+1); t <= (int)round(t0); t++)
{
*(x + j) = sign_prev;
j++;
}
}
else
{
// '0' has single transition and changes sign
temp =j + (int)round(t0)-(int)round(prev_i);
//printf("%d\n",(int)temp);
x = realloc(x, sizeof(int)*(int)temp); // 2
for(t=(int)round(prev_i); t < (int)round(t0); t++)
{
*(x + j) = -sign_prev;
j++;
}
sign_prev = -sign_prev;
}
prev_i = t0;
}
*signal_size = j;
return x;
}
Both realloc lines, marked with //1 and //2 on the previous code, give me this error message:
assigning to int * from incompatible type void *
Because I don't want this code behaving weirdly or crashing on me, obviously, I ask will: I have some problem in the future if I simply cast it to int * by doing
x = (int*)realloc(x, sizeof(int)*(int)temp);
Thanks
In C, a value of type void* (such as the value returned by realloc) may be assigned to a variable of type int*, or any other object pointer type. The value is implicitly converted.
The most likely explanation for the error message is that you're compiling the code as C++ rather than as C. Make sure the source file name ends in .c, not .C or .cpp, and make sure your compiler is configured to compile as C rather than as C++.
(Casting the result of realloc or malloc is considered poor style in C. In C++, the cast is necessary, but you normally wouldn't use realloc or malloc in C++ in the first place.)
This should work in C. Are you perhaps using a C++ compiler to compile this? For example, some big company based in Redmond refuses to properly support a contemporary C implementation. Their compiler is C++ by default and needs some option to whack it into a C compiler.
You have stdlib.h included? Then you don't need the casts. In fact, it is best practice to not cast the malloc return.
All alloc-style functions in C return memory addresses with the most strict alignment, so the cast can't give a pointer that isn't a valid int pointer.
Consider the following:
double fact(int n)
{
int i;
double res = 1;
for (i = 1; i <= n; i++)
res *= i;
return res;
}
double f = 1;
for (int i = 0; i < 16; i++)
{
printf("%lf \n", fact(2*i + 1));
f *= (f + 1)*(f + 2);
printf("%lf \n", f);
}
Why does fact(2*i+1) results a correct value while f results a weird value of 1.#INF00?
Because it overflows.
The value of f after 16 iterations is bigger than if your code looked like this and your initial f was 2:
f *= f*f;
Which is the same as
f = f*f*f
So you take a cube 16 times - this is HUGE!
2^3 = 8
8^3 = 512
512^3 = 134217728
...
On the topic of undefined behaviour, the l length modifier in %lf is only defined for conversion specifiers using integer types. If you meant to use %Lf, then your argument should be a long double. Perhaps you meant to use %f, which corresponds to a double argument (floats end up promoted to double when passing them to variadic functions such as printf).
As Peter Ivanov explained, your calculations cause an overflow, which IIRC is also undefined behaviour.
As you've probably guessed, you might find a solution to your problem by using the long double type (and the corresponding %Lf format specifier) throughout your code...
I want do the two's complement of a float data.
unsigned long Temperature ;
Temperature = (~(unsigned long)(564.48))+1;
But the problem is that the cast loses information, 564 instead of 564.48.
Can i do the two's complement without a loss of information?
That is a very weird thing to do; floating-point numbers are not stored as 2s complement, so it doesn't make a lot of sense.
Anyway, you can perhaps use the good old union trick:
union {
float real;
unsigned long integer;
} tmp = { 564.48 };
tmp.integer = ~tmp.integer + 1;
printf("I got %f\n", tmp.real);
When I tried it (on ideone) it printed:
I got -0.007412
Note that this relies on unspecified behavior, so it's possible it might break if your compiler does not implement the access in the most straight-forward manner. This is distinct form undefined behavior (which would make the code invalid), but still not optimal. Someone did tell me that newer standards make it clearer, but I've not found an exact reference so ... consider yourself warned.
You can't use ~ over floats (it must be an integer type):
#include <stdio.h>
void print_binary(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char *) ptr;
unsigned char byte;
int i, j;
for (i = size - 1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = b[i] & (1 << j);
byte >>= j;
printf("%u", byte);
}
}
printf("\n");
}
int main(void)
{
float f = 564.48f;
char *p = (char *)&f;
size_t i;
print_binary(sizeof(f), &f);
for (i = 0; i < sizeof(float); i++) {
p[i] = ~p[i];
}
print_binary(sizeof(f), &f);
f += 1.f;
return 0;
}
Output:
01000100000011010001111010111000
10111011111100101110000101000111
Of course print_binary is there for test the result, remove it, and (as pointed out by #barakmanos) print_binary assumes little endian, the rest of the code is not affected by endiannes:
#include <stdio.h>
int main(void)
{
float f = 564.48f;
char *p = (char *)&f;
size_t i;
for (i = 0; i < sizeof(float); i++) {
p[i] = ~p[i];
}
f += 1.f;
return 0;
}
Casting a floating-point value to an integer value changes the "bit contents" of that value.
In order to perform two's complement on the "bit contents" of a floating-point value:
float f = 564.48f;
unsigned long Temperature = ~*(unsigned long*)&f+1;
Make sure that sizeof(long) == sizeof(float), or use double instead of float.
I am trying to sort a 2d array based on a particular column using qsort in C. I am attaching a minimal working code I am using. Essentially I am passing the pointer to the rows of the array to qsort, and based on the column number I want to sort, I modify the element to compare inside the compare function. Now, according to C convention, if I have 2 columns, I expect colnum=0 and colnum=1 to correspond to columns 1 and 2. But, in my implementation, I get the correct result if colnum=1 means column 1 and colnum=2 means column 2. I am stumped as to why this should be ? (I have also included the array allocation function I use).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "myfun.h"
static int colnum = 0;
int cmp(const void * a,const void * b);
int main(){
int i;
double **z1;
z1=matrix(5,2);
for (i=0; i<5; i++){
z1[i][1]=-i-1; z1[i][2]=16*i+10;
printf("before sort z1 %lf %lf \n",z1[i][1],z1[i][2]);
}
colnum=2;
qsort(z1,5,sizeof(double*),cmp);
for (i=0; i<5; i++){
printf("after sort z1 %lf %lf \n",z1[i][1],z1[i][2]);
}
getchar();
}
int cmp(const void * a,const void * b)
{
double** x = (double**) a;
double** y = (double**) b;
double xval, yval;
xval = *(*(x)+colnum);
yval = *(*(y)+colnum);
printf("%lf %lf \n",xval,yval);
if (xval < yval )
{
return 1;
}
else if (xval > yval)
{
return -1;
}
else
{
return 0;
}
}
double** matrix(int rows,int cols){
int k;
double **m;
m = (double **)malloc(rows * sizeof(double *));
for (k=0; k<rows; k++){
m[k] = (double *)malloc(cols * sizeof(double));
}
return m;
}
Your program has undefined behavior, because your are accessing memory beyond the boundary allocated by your inner allocation loop of matrix():
m = (double **) malloc(rows * sizeof(double *));
for (k = 0; k < rows; k++) {
m[k] = (double *) malloc(cols * sizeof(double));
}
Since cols has the value 2, malloc() only returns memory for 2 elements of type double. But, your code is initializing and reading a non-existing third element instead.
Since doing so is undefined, producing the output you expect is within the realm of possible behaviors. However, it is incorrect since you run the risk of corrupting the heap, and reading invalid data. Running your program under valgrind produces "Invalid write" and many "Invalid read" errors due to this problem in your program.
The correct approach is to store the values in their proper 0 and 1 column indexes in your initialization, set colnum to 1 to sort by the second column, and read from the proper 0 and 1 indexes when you print the array values.
z1 = matrix(5, 2);
for (i = 0; i < 5; i++) {
z1[i][0] = -i - 1;
z1[i][1] = 16 * i + 10;
printf("before sort z1 %lf %lf \n", z1[i][0], z1[i][1]);
}
colnum = 1;
qsort(z1, 5, sizeof(double *), cmp);
for (i = 0; i < 5; i++) {
printf("after sort z1 %lf %lf \n", z1[i][0], z1[i][1]);
}
As a side note, when I was formatting your code for this answer, I noticed that you used an old C anachronism, probably unintentionally:
z1[i][1]=-i-1; /*...*/
The =- construct was the original C's (pre C.89) way of spelling the -= operator. It is highly unlikely you will end up using a compiler that will honor that operator without a diagnostic, but you should be wary of this syntax, and separate the = and the - tokens to remove the ambiguity.
z1[i][1] = -i - 1; /*...*/
I wrote this code to do the IEEE 754 floating point arithmetic on a 4byte string.
It takes in the bytes, converts them to binary and with the binary I get the sign, exponent, and mantissa and then do the calculation.
It all works just about perfectl, 0xDEADBEEF gives me 6259853398707798016 and the true answer is 6.259853398707798016E18, now these are same values and I wont have anything this large in the project I'm working with, all other smaller values put the decimal in the correct place.
Here is my code:
float calcByteValue(uint8_t data[]) {
int i;
int j = 0;
int index;
int sign, exp;
float mant;
char bits[8] = {0};
int *binary = malloc(32*sizeof *binary);
for (index = 0;index < 4;index++) {
for (i = 0;i < 8;i++,j++) {
bits[i] = (data[index] >> 7-i) & 0x01;
if (bits[i] == 1) {
binary[j] = 1;
} else {
binary[j] = 0;
}
}
printf("\nindex(%d)\n", index);
}
sign = getSign(&(binary[0]));
mant = getMant(&(binary[0]));
exp = getExp(&(binary[0]));
printf("\nBinary: ");
for (i = 0;i < 32;i++)
printf("%d", binary[i]);
printf("\nsign:%d, exp:%d, mant:%f\n",sign, exp, mant);
float f = pow(-1.0, sign) * mant * pow(2,exp);
printf("\n%f\n", f);
return f;
}
//-------------------------------------------------------------------
int getSign(int *bin) {
return bin[0];
}
int getExp (int *bin) {
int expInt, i, b, sum;
int exp = 0;
for (i = 0;i < 8;i++) {
b = 1;
b = b<<(7-i);
if (bin[i+1] == 1)
exp += bin[i+1] * b;
}
return exp-127;
}
float getMant(int *bin) {
int i,j;
float b;
float m;
int manBin[24] = {0};
manBin[0] = 1;
for (i = 1,j=9;j < 32;i++,j++) {
manBin[i] = bin[j];
printf("%d",manBin[i]);
}
for (i = 0;i < 24;i++) {
m += manBin[i] * pow(2,-i);;
}
return m;
}
Now, my teacher told me that there is a much easier way where I can just take in the stream of bytes, and turn it into a float and it should work. I tried doing it that way but could not figure it out if my life depended on it.
I'm not asking you to do my homework for me, I have it done and working, but I just need to know if I could of done it differently/easier/more efficiently.
EDIT: there are a couple special cases I need to handle, but it's just things like if the exponent is all zeros blah blah blah. Easy to implement.
The teacher probably had this in mind:
char * str; // your deadbeef
float x;
memcpy(&x, str, sizeof(float));
I would advise against it, for the issues with endianness. But if your teacher wants it, he shall have it.
I think you want a union - just create a union where one member is a 4 character array, and the other a float. Write the first, then read the second.
Looking at what your code does then the "4 byte string" looks like it already contains the binary representation of a 32 bit float, so it already exists in memory at the address specified by data in big endian byte order.
You could probably cast the array data to a float pointer and dereference that (if you can assume the system you are running on is big endian and that data will be correctly aligned for the float type on your platform).
Alternatively if you need more control (for example to change the byte order or ensure alignment) you could look into type punning using a union of a uint8_t array and a float. Copy the bytes into your union's uint8_t array and then read the float member.
Here is my working code:
unsigned char val[4] = {0, 0, 0xc8, 0x41};
cout << val << endl;
cout << "--------------------------------------------" << endl;
float f = *(float*)&val;
cout << f << endl;
return 0;