Hello i have a little problem.
I want to create a char array and want to store a 2 bytes long number (unsigned short) into it at the declaration time.
The number is not known, it is calclulated or changed time to time.
this is the code,
1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <sys/types.h>
/*-- PREPROCESSOR SUBSTITIUTIONS --*/
// Core Basics
#define MEMSIZE 50000 // Size of the Memory.
#define MAS 4 // Minimum Allocation Size Permited in Bytes.
#define MALLOCDATA 6 // Size of the data section of the Memory in Bytes.
/*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
char mem[MEMSIZE] = {?, ?, 0, 0, 0, 0, ?, ?};
In the places that i have put question(?) marks there should be the 2Byte numbers. as you can see i want to write the number MALLOCDATA at the first two byts (mem[0],mem[1]) of the array and calculated number (MEMSIZE - MALLOCDATA) in the 7th and 8th bytes (mem[6],mem[7]).
I dont want to change the array type as char gives me the ability to manipulate every byte of it seperately.
I also dont want to use some initialization function because i want this to be very efficient.
I cannot put this code inside of any function.
I am using C language not C++.
Thanks.
OK so if i understand you correctly, you want the first two bytes to represent an unsigned short that has the value 6, and the last two to represent 49994 (50000 - 6). if you replace the first two question marks with 6 and then 0, then the second two with 74 and 195 you will get the correct result. You can calculate these numbers yourself by converting the original numbers to 16-bit binary, then splitting that into two eight bit sections, and converting those 8 bit sections back into decimal to store in your char array. You can check for yourself by using a little bit of code like this:
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
unsignedshort num;
char mych[] = {6, 0, 74 ,195};
memcpy(&num, mych, 2);
printf("the first number is: %d\n", num);
memcpy(&num, mych + 2, 2);
printf("the second number is: %d\n", num);
getchar();
}
yes my friend, i know that. but thats why i say the number can change. so i dont want to go grab calculater and turn decimal to hexa and replace them.
i think there is a way to do this at least with macros.
do you know a way to do this with macro, i mean ,
break the hexa decimal number
0x0006 to 0x06 , 0x00
and
0xC350 to 0x50 , 0xC3
#include <stdio.h>
#include <sys/types.h>
/*-- PREPROCESSOR SUBSTITIUTIONS --*/
// Core Basics
#define MEMSIZE 50000 // Size of the Memory.
#define MAS 4 // Minimum Allocation Size Permited in Bytes.
#define MALLOCDATA 6 // Size of the data section of the Memory in Bytes.
// Pointers in Data Section
#define FREEHEAD 0 // Works as the Head Pointer of the linked list of free spaces
#define PREVIOUS 2 // Previous Pointer
#define CURRENT 4 // Current Pointer
/*-- MEMORY DEFINITION AND DATA SECTION INITIALIZATION --*/
// Little Endian Architecture assumed
char mem[MEMSIZE] = {(unsignedshort)(MALLOCDATA + 2 ) & 255, (unsignedshort)(MALLOCDATA + 2 ) >> 8,\
0, 0, 0, 0, (unsignedshort)(MEMSIZE - MALLOCDATA) & 255,\
(unsignedshort)(MEMSIZE - MALLOCDATA) >> 8, 2, 5 };
void traverse()
{
// Pre requisitions for the traversal.
mem[PREVIOUS] = 0; // Set The,
mem[PREVIOUS+1] = 0; // Previous => 0
mem[CURRENT] = mem[FREEHEAD]; // Set The,
mem[CURRENT+1] = mem[FREEHEAD+1]; // Current => Head
// Traverse and locate for an appropiate location.
while( mem[CURRENT] || mem[CURRENT+1] ) // Untill Current is not NULL
{
/*- Go to the next location -*/
// Previous = Current
mem[PREVIOUS] = mem[CURRENT];
mem[PREVIOUS+1] = mem[CURRENT+1];
// Current = *Previous
(unsignedshort)mem[CURRENT] = (unsignedshort)mem[ mem[PREVIOUS] ];
}
}
i have stroked-through the line that i want to do... the stroked-through line shall give you the idea which i want.....
How can i retreave 2byte number from this char array??
i know hot to do it by creating a short * Pointer....
but i have to do this without creating any more variables, but i am allowed to use any space in the char array as variable space...
That's non-portable, as it would not work on big-endian systems.
Just reverse the initial process: mem[CURRENT]<<8 | mem[CURRENT+1];
I also have the feeling that you need to have a look at bitwise operators again.
ya.. i am not very used to bitwise operators in C.. Thanks Athar...
but i have a question,
Why is it not going to work on Big Endians..?
When i was writing the line 17, i thought that this will only work for little endians... am i wrong...?
can your code work on both..?
Please, can you explain..
and again i have to say,
Your code is awsome.. i was thinking the other way around, with NASM in my head, that i should try to make the size of the operation to 16 bits..