C :: Structure Padding To 16 Bytes For Use With Atomic Instructions
Jun 30, 2013
I am using atomic instructions on x64 and variables so used must be 16 byte aligned.
I use a number of structures where their members are so operated upon.
The structures accordingly needs must be 16 byte aligned and padded - their internal members must be on 16 byte boundaries and, crucially, there must be tail padding to a 16 byte boundary, so I can allocate arrays of these structures and use pointer math to iterate. (I am naturally using aligned malloc).
The problem I am finding is that it is not apparent to me how to achieve this end. Here below we have a test structure (currently I'm working with the latest Amazon Linux GCC, 4.6.3, on x64);
The problem manifest is that sizeof(struct test_element) is 40 bytes! So the second element does not begin on a 16 byte boundary and we all fall down. Printing the addresses of the first element in the test element array, I see the following;
So we see fe->next is the first element and so is correctly aligned curtsey of aligned malloc, where fe->next is 16 bytes, fe->user_data is correctly aligned, but then te->thread_number is misaligned and te->datum is given eight bytes rather than four, leaving us in the end without correct tail padding to a 16 byte boundary.
So, what gives? how *am* I supposed to indicate to the compiler it must pad structures to 16 byte boundaries?
Write a program that will encode/decode any message based on the given secret instructions.
Input The input shall be composed of 3 parts: A string, maximum length of 255 characters) in a single input line (i.e., terminated by the end-of-line or carriage return) representing the message to be encoded or decoded, A single character identifying the operation to be performed–‘E’ for encode or ‘D’ for decode, and An integer giving the number of columns to be (or that was) used for encoding called the key.
Output The program shall display the encoded/decoded message. Since the algorithm fails if a sequence of spaces, at least as long as the key, is formed at, and subsequently deleted from, the end of the last column, replace spaces by periods in the output.
Input Validation None.
Sample Runs
Enter the message: meet me in the park tonight at seven. (E)ncode or (D)ecode? E Enter number of columns (key): 5 Encoded message reads: mm.pth.neetaots.e.hrn.e.tiekiav..n..gte.
Consider how to implement a mutex lock using an atomic hardware instruction. Assume that the following structure defining the mutex lock is available:
Code:
typedef struct { int available; } lock; (available == 0)
indicates that the lock is available, and a value of 1 indicates that the lock is unavailable. Using this struct, illustrate how the following functions can be implemented using the test and set() and compare and swap() instructions:
I am working on pset4 in the CS50 online coarse. The goal is to resize a bmp image. I have it working except adding the padding back to new resized image. The image I am using uses 3 bites of padding. If the factor I resize the image by works out to have 3 bites of padding it works perfect and if the image has no padding it works perfectly I need a fresh pair of eyes. I am not sure how to add the images so here is the address to download the images and the skelton of the source code they gave to modify. [URL] .... The image that requires the padding is labled as small.bmp.
#include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4)
The following piece of code is supposed to output the binary representation of a given integer and it does exactly that. However, if the given integer is 2, then output is 01. Is there a way to make the program output 0001. I am working on a C program that outputs 4-bit gray code.
#include <stdio.h> #include <math.h> int main(void) { long int n=2; while (n) { if (n & 1) printf("1");
Im using some motors which run off PWM pins.Theres a High byte and Loq byte register (PWMH,PWML).I have an int which i need to put into these registers but i dont know how???so for example
I need to read repeatedly data from a MPEG2 file to the buffer of 188 bytes and analyse data bit by bit.
I have the problem with correct bytes reading from file. In my code listed below I have two methods for that.
First one is lossing this bytes which in hex_base mode have 0 at the begining, eg: 03, 0F, etc.
The second method based on read function which need to have buffer as a char (lenght > 1 byte). Because of that I receive different values from that from file in some cases.
How can I properly read such file?
Code: #include <iostream> #include <fstream> #include <stdlib.h> #include <windows.h> using namespace std; int main() { fstream plik_in;
I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.
This is being done by opening a pointer to the file with fopen, reading in each byte of data as a char, and then writing this char to the screen.
I have some image files (e.g. "image.jpg"), whose structure I know, so that I can test my program.
When I print out the chars, they are initially correct, and follow the structure of the file as expected.
However, after about 40 bytes, I find that every subsequent character is ' ' i.e. a blank character.
I then created a CharToBin function, which allows me to print out the actual bits in the char. When doing this, it shows that all the bits are 1 for the characters. i.e. most of the file is represented by 1's, which is clearly not correct.
This happens on all the image files I have tested, and furthermore, on several other non-image files. They all start printing out ' ' after a while. However, all these files are fine and not corrupted, e.g. the image files display correctly.
Code: #include <fstream> #include <iostream> #include <sstring> #include <string> #include <stdio.h> using namespace std; string CharToBin(char ch) { bool bits[8]; for (int i = 0; i < 8; i++)
I have a FILE stream, and I want to create a function that streams a specified number of bytes (up to four bytes) and returns the value of this, as an integar.
For example, supposing that my FILE has the following data, in hex: 74 C8 02 33 F2 7B....... I then want to stream three bytes and store this as an integar. So, I want to stream "04 08 02". The data stored in the integar should then be "00 74 C8 02", because I have not streamed anything into the first byte. By converting the hex to dec, the integar should then be of the value 7653378 (if it is unsigned).
To try to achieve this, I have written the following function. I create an integar and initialise it to zero, then take each byte from the stream, and OR it with my integar. Then, I shift the integar left by 8, and read the next byte, and so on.
The problem is, when I convert "c" to "c_int", it adds on a load of 1's to the left of the "c" data. This then means that the OR comparison changes all those bits in my integar to 1.
How to solve this? I am also wondering whether there is a much more simple way of doing this, rather than having to write my own function....
Code: int StreamFileToInt(FILE *fp, int num_bytes) { char c; int c_int; int x = 0x0000; for (int i = 0; i < num_bytes; i++) {