I have to copy Mac address stored as an array with a pointer reference to another. I want to perform a swap but, i dont know if i am copying right.
uint8_t * custom_mac_store;
memcpy(custom_mac_store, ehdr->ether_shost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, custom_mac_store, sizeof(ehdr->ether_shost));
ehdr->ether_shost refers to a Mac address which is 48 bits long and stored in 6 arrays as 8 bits.
struct sr_ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
uint16_t ether_type; /* packet type ID */
} __attribute__ ((packed)) ;
Thanks in advance!!!
custom_mac_store is a pointer, not an array, you should allocate the memory doing the following:
uint8_t * custom_mac_store = malloc(sizeof(ehdr->ether_shost) * sizeof(uint8_t));
Then do the memcpy
This way you can erase or overwrite ehdr->ether_shost but you already copied your array to the new array.
#alk You are right you need to delete the memory
uint8_t * custom_mac_store = malloc(sizeof(ehdr->ether_shost) * sizeof(uint8_t));
memcpy(custom_mac_store, ehdr->ether_shost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, custom_mac_store, sizeof(ehdr->ether_shost));
free(custom_mac_store); //< Destroys everything in the array
custom_mac_store = NULL; //< NULL must be defined in a macro preferanly DEFINE NULL 0
or you could use c++
uint8_t * custom_mac_store = new uint8_t[sizeof(ehdr->ether_shost)];
std::copy ( ehdr->ether_shost, ehdr->ether_shost+sizeof(ehdr->ether_shost), custom_mac_store );
std::copy ( ehdr->ether_dhost, ehdr->ether_dhost+sizeof(ehdr->ether_shost), ehdr->ether_shost );
std::copy ( custom_mac_store, custom_mac_store+sizeof(ehdr->ether_shost), ehdr->ether_dhost );
delete[] custom_mac_store; //< Destroys everything in the array
or you could use the stack instead of the heap (this should be faster, just don't break the stack)
const std::size_t size_custom_mac_store = 48000;
uint8_t custom_mac_store[size_custom_mac_store]; //< The size must be either a const or a macro
std::copy ( ehdr->ether_shost, ehdr->ether_shost+sizeof(ehdr->ether_shost), custom_mac_store );
std::copy ( ehdr->ether_dhost, ehdr->ether_dhost+sizeof(ehdr->ether_shost), ehdr->ether_shost );
std::copy ( custom_mac_store, custom_mac_store+sizeof(ehdr->ether_shost), ehdr->ether_dhost );
delete[] custom_mac_store; //< Destroys everything in the array
Good luck.
PS: Love memory management, it's the only way
You can define ethernet length as 6 then making use of a temporary array, just perform memcpy as below,
#define ETH_ALEN 6
uint8_t tmp[ETH_ALEN];
memcpy(tmp, eth->ether_shost, ETH_ALEN);
memcpy(eth->ether_shost, eth->ether_dhost, ETH_ALEN);
memcpy(eth->ether_dhost, tmp, ETH_ALEN);
It might work if some memory was allocated for your temporary buffer custom_mac_store.
Like that it's just an uninitialized pointer waiting to cause a crash.
You can either allocate explicitly some memory to it, or use directly a 6 byte array instead.
Related
I am working on a micro controller, so, no malloc. Actually, I want to create a memory manager, so I am kinda implementing the malloc function for later use and using the BLOCK strategy to get it, like FreeRTOS does.
typedef struct BLOCK {
unsigned char used; // If 1 block is used, if 0 it's free to use
unsigned long offset; // Offset of the starting block
unsigned long size; // Size of this block
struct BLOCK * next; // Pointer to the next block
} BLOCK_t;
#define MAX_PROGRAMS 3
#define BLOCKS_NEEDED (MAX_PROGRAMS * 2) + 1
BLOCK_t blocks[BLOCKS_NEEDED]; // Not allocating 7 * sizeof(BLOCK)
This BLOCK is a linked list and I want to create (allocate) a fixed amount of them and set the first blocks[0] only. The next ones will be created in execution time, when memory is allocated.
Thanks in advance.
EDIT: In case the title is not clear enough, I want to compiler to assign some memory space to my array (fixed location and size) but I don't want to initialize it with data because I will get the data in run-time, so I want an array of 7 BLOCKs with empty data. The code above shows my attempt to do it, I declared the array, but I assume that declaring an array doesn't give you the space needed. How can I achieve this ? How can I get the compiler to give me that space ?
EDIT 2: This would be tha Java code to do it.
private static int MAX_PROGRAMS = 3;
private static int BLOCKS_NEEDED = (MAX_PROGRAMS * 2) + 1:
Block myBlockList[] = new Block[BLOCKS_NEEDED];
This get the space for myBlockList even though the list is empty and each item is uninitialized, but I have the space already.
All you want to do is allocate memory automatically on the stack.
#include <stdio.h>
#define blockcontent_size 1024
#define blockcount 3
typedef struct
{
unsigned char used;
unsigned long offset;
unsigned long size;
unsigned data[blockcontent_size];
} BLOCK;
BLOCK blocks[blockcount];
int main()
{
printf("memory available in one block %u\n", sizeof(blocks[0].data));
printf("memory used for one block %u\n", sizeof(BLOCK));
printf("memory used for all blocks %u\n", sizeof(blocks));
return 0;
}
You actually do not need a linked list, you can just use the index.
Is this close to what you are asking?
#LPs quote:
Using c writing BLOCK_t blocks[BLOCKS_NEEDED]; you are declaring the array and sizeof(BLOCK_t)*BLOCKS_NEEDED bytes are occupied by the array.
So my statement :
BLOCK_t blocks[BLOCKS_NEEDED]; // Not allocating 7 * sizeof(BLOCK)
was false, it actually does allocate the space.
I am trying to copy a byte array to my struct, then serialize my struct to a byte array again.
But, after I serialize my struct array, I cant get my data value (0x12, 0x34, 0x56) again, instead i get some rubbish data.
What is wrong here?
#pragma pack(push, 1)
typedef struct {
uint8_t length;
uint8_t *data;
} Tx_Packet;
#pragma pack(pop)
static void create_tx_packet(uint8_t *packet, uint8_t *src, int length);
int main(void)
{
uint8_t packet[32];
uint8_t data[] = { 0x12, 0x34, 0x56 };
create_tx_packet(packet, data, 3);
//i check using debugger, i cant get the data value correctly
//but i could get length value correctly
return 0;
}
static void create_tx_packet(uint8_t *packet, uint8_t *src, int length)
{
Tx_Packet *tx_packet = malloc(sizeof(*tx_packet ));
tx_packet->length = length;
tx_packet->data = (uint8_t *)malloc(length);
memcpy(tx_packet->data, src, length);
memcpy(packet, tx_packet, sizeof(*tx_packet));
}
Right now, your create_tx_packet() function copies a Tx_Packet struct created in the function to a uint8_t array. That struct contains the length and a pointer to the data, but not the data itself. It's actually not necessary to use the struct as an intermediate step at all, particularly for such a simple packet, so you could instead do:
static void create_tx_packet(uint8_t *packet, uint8_t *src, int length)
{
*packet = length; /* set (first) uint8_t pointed to by packet to the
length */
memcpy(packet + 1, src, length); /* copy length bytes from src to
the 2nd and subsequent bytes of
packet */
}
You still need to make sure packet points to enough space (at least length + 1 bytes) for everything (which it does). Since the version above doesn't dynamically allocate anything, it also fixes the memory leaks in your original (which should have freed tx_packet->data and tx_packet before exiting).
--
If you do want to use a struct, you can (since the data is at the end) change your struct to use an array instead of a pointer for data -- then extra space past the size of the struct can be used for the data, and accessed through the data array in the struct. The struct might be:
typedef struct {
uint8_t length;
uint8_t data[];
} Tx_Packet;
and the function becomes (if a temporary struct is used):
static void create_tx_packet(uint8_t *packet, uint8_t *src, int length)
{
/* allocate the temporary struct, with extra space at the end for the
data */
Tx_Packet *tx_packet = malloc(sizeof(Tx_Packet)+length);
/* fill the struct (set length, copy data from src) */
tx_packet->length = length;
memcpy(tx_packet->data, src, length);
/* copy the struct and following data to the output array */
memcpy(packet, tx_packet, sizeof(Tx_Packet) + length);
/* and remember to free our temporary struct/data */
free(tx_packet);
}
Rather than allocate a temporary struct, though, you could also use struct pointer to access the byte array in packet directly and avoid the extra memory allocation:
static void create_tx_packet(uint8_t *packet, uint8_t *src, int length)
{
/* Set a Tx_Packet pointer to point at the output array */
Tx_Packet *tx_packet = (Tx_Packet *)packet;
/* Fill out the struct as before, but this time directly into the
output array so we don't need to allocate and copy so much */
tx_packet->length = length;
memcpy(tx_packet->data, src, length);
}
If you use memcpy(packet, tx_packet, sizeof(*tx_packet)); you are copying the memory representation of tx_Packet into packet, starting with tx_packet->length.
Additionally when mallocating tx_packet that size should be sizeof(*packet)+sizeof(uint8_t) (length of packet plus length field)
And again when copying the tx_packet back to packet you are writing out of the boundaries of packet.
EDIT:
I forgot to mention that depending on your compiler memory alignment parameter you could get any length for the fields (including tx_packet->length) to accelerate memory operation. On 32bits machine it could be 4 and padded with rubbish.
When you serialize your struct with
memcpy(packet, tx_packet, sizeof(*tx_packet));
you're copying the length and the pointer to the data, but not the data itself. You'll probably need two memcpy calls: one of sizeof(uint8_t) to copy the length field, and one of length to copy the data.
This line:
Tx_Packet *tx_packet = malloc(sizeof(*packet));
only allocates one byte for the packet header, which you then immediately write off the end of, causing undefined behavior. You probably meant
Tx_Packet *tx_packet = malloc(sizeof(*tx_packet));
I am trying to make a struct for a circular buffer that contains an array of type "quote." However, the quote array must start out at a size of 10. I am trying to figure out if I declare the size of 10 in my .h file or in my .c file. My two files are as follows:
.h file:
typedef struct{
unsigned int time;
double rate;
}quote;
typedef struct{
unsigned int testNum;
quote quoteBuffer[];
}cbuf;
cbuf* cbuf_init();
.c file:
cbuf* cbuf_init(){
cbuf *buffer = (cbuf *)calloc(1,sizeof(cbuf));
buffer->testNum = 1;
quote newQuote = {1,1.00};
buffer->quoteBuffer[1] = newQuote;
return buffer;
}
These are obviously just test values, however if I wanted to specifically make the quote array in the cbuf struct start out at a size of 10, would I declare that in the .h file as:
typedef struct{
unsigned int testNum;
quote quoteBuffer[10];
}cbuf;
or in the .c file some other way?
There are two ways of having dynamic arrays in structures. The obvious is of course to have it as a pointer, and dynamically allocate (or reallocate) when needed.
The other is to have an array of size 1, and then allocate a larger size than the structure, to accommodate for the array:
typedef struct {
unsigned int testNum;
quote quoteBuffer[1];
} cbuf;
cbuf *cbuf_init(const size_t num_quotes) {
/* Allocate for the `cbuf` structure, plus a number of `quote`
* structures in the array
*/
cbuf *buffer = malloc(sizeof(cbuf) + (num_quotes - 1) * sizeof(quote));
/* Other initialization */
return buffer;
}
/* If more quotes are needed after initial allocation, use this function */
cbuf *cbuf_realloc(cbuf *buffer, const size_t new_num_quotes) {
buffer = realloc(buffer, sizeof(cbuf) + (new_num_quotes - 1) * sizeof(quote));
/* Other initialization */
return buffer;
}
Now you can use the array as a normal array:
cbuf *buffer = cbuf_init();
buffer->quoteBuffer[5].time = 123;
Note: I only allocate extra space for 9 quote structures, but state that I allocate ten. The reason is that the cbuf structure already contains one quote structure in its array. 1 + 9 = 10. :)
Note 2: I put the quote array in the cbuf structure with one entry already in it for backwards compatibility. Having an array without a size in the structure is quite new in the C world.
you can also do this if you want 10 quotes in a cbuf but a statically allocated like quote buffer[10] would work too:
cbuf* cbuf_init(int numQuotes)
{
cbuf *b = calloc(1, sizeof(cbuf) + numQuotes * sizeof(quote));
return b;
}
If you want a statically sized circular buffer then your could declare the size in the header file. Using a #define for the buffer size will make the code more readable and maintainable, as you'll reference the size elsewhere in your code.
If you want the circular buffer to be growable then define the size in your C file. You'll then have to take care of tracking the size and destroying the memory that you will have to allocate dynamically.
In your example, I think you need to allocate more room for your quote structs...
cbuf *buffer = (cbuf *)calloc(1,sizeof(cbuf) + NUM_QUOTES*sizeof(struct quote));
---------------------------------
The reason for this is that in your struct def...
quote quoteBuffer[];
... quoteBuffer doesn't add size to the struct. quoteBuffer will point to one byte past the end of the struct, hence the need to allocate memory for the struct + memory for the array.
EDIT: Daniel Fischer's comment (thanks Daniel) - quoteBuffer may, in some cases, add size to the struct if it introduces padding. The reason is that the compiler will probably strive to get the most optimal alignment for quoteBuffer. For example, ints normally aligned of 4-byte boundaries. E.g. a struct like:
struct {
char a;
int b;
}
is probably changed by compiler to
struct {
char a;
char pad[3]; // compiler adds padding behind the scenes
int b; // align b on a 4-byte boundary
}
This probs doesn't apply in your case as your struct leaves quoteBuffer[] on a 4 byte boundary.
The reason that the compiler does this is two fold.
1. On some architectures (not so common nowadays I think?), unaligned accesses aren't supported.
2. Aligned accesses are more efficient, even if architecture allows non-aligned accesses as it is one memory read as opposed to two memory reads plus a manipulation.
I have following code :
/* Window size in bytes. */
static uint32_t size = 0;
/* Window address. */
static uint32_t address = 0;
/* Memory Base Address */
static uint8_t *sharedMemory=NULL;
sharedMemory = memalign(size, size);
void rioShardMemoryWindowGet (uint8_t *baseAddr,uint32_t *memorySize,uint32_t *windowAddress )
{
*baseAddr=(int)sharedMemory;
printf("sharedMemory: #%x",sharedMemory);
*memorySize=size;
*windowAddress=address;
}
rioShardMemoryWindowGet(&baseAddr0, &baseSize, &(Addrs.virtualBaseAddr));
printf("baseAddr0 : #%x",baseAddr0);
I have no clue why baseAddr0 is 0 in the second printf, while in the first sharedMemory is 0x500000.
Ok I think I understand your problem.
You're trying to store the address number in baseAddr0, am I right? (not sure the reasons but this is the only thing I came up with).
The reason that a 0x500000 is showing as a 0 is because a uint8_t has not enough bits to represent an address and so it's "culling it down" to only 1 byte (therefore showing a 0).
Change the baseAddr to a uint32_t and voila, everything works.
Anyways, the reason the other posters are telling you to use a pointer to pointer is because what you seem to be doing is weird, unless you're planning on using the address for something special such as displaying it or using as an offset, perhaps?
p.s.: you're also going to need to change this line
*baseAddr=(uint32_t)sharedMemory;
edit: your code should look like this to get what you want:
/* Window size in bytes. */
static uint32_t size = 0;
/* Window address. */
static uint32_t address = 0;
/* Memory Base Address */
static uint8_t *sharedMemory=NULL;
sharedMemory = memalign(size, size);
void rioShardMemoryWindowGet (uint32_t *baseAddr,uint32_t *memorySize,uint32_t *windowAddress )
{
*baseAddr=(uint32_t)sharedMemory;
printf("sharedMemory: #%x",sharedMemory);
*memorySize=size;
*windowAddress=address;
}
rioShardMemoryWindowGet(&baseAddr0, &baseSize, &(Addrs.virtualBaseAddr));
printf("baseAddr0 : #%x",baseAddr0);
The reason why you NEED an uint32 to store the numeric address is because addresses are 32 bits, and that's the reason why you see a 0 using an 8 bit value, because 0x500000 maps to 0x00 to a byte
rioShardMemoryWindowGet should accept uint8_t **baseAddrPtr if you want it to modify baseAddr0. Then you'll have *baseAddr = sharedMemory without a cast.
You must pass a pointer to pointer as a function argument.
Only then you will be able to store the value in it. In your case, you tried to store the address of sharedMemory in baseAddr[0] location.
/* Window size in bytes. */
static uint32_t size = 0;
/* Window address. */
static uint32_t address = 0;
/* Memory Base Address */
static uint8_t *sharedMemory=NULL;
sharedMemory = memalign(size, size);
void rioShardMemoryWindowGet (uint8_t **baseAddr,uint32_t *memorySize,uint32_t *windowAddress )
{
*baseAddr=sharedMemory;
printf("sharedMemory: #%x",sharedMemory);
*memorySize=size;
*windowAddress=address;
}
uint8_t *baseAddr0;
rioShardMemoryWindowGet(&baseAddr0, &baseSize, &(Addrs.virtualBaseAddr));
printf("baseAddr0 : #%x",baseAddr0);
After using malloc() to initialize 5000 bytes of memory, how would I reference the bytes in this memory space? For example, if I need to point to a starting location of data within the memory, how would I go about that?
EDIT: Does it matter what I use to point to it? I mean I am seeing people use bytes/int/char? Is it relevant?
Error I get:
You can use the subscript array[n] operator to access the index you are interested in reading/writing, like so:
uint8_t* const bytes = (uint8_t*)malloc(5000);
bytes[0] = UINT8_MAX; // << write UINT8_MAX to the first element
uint8_t valueAtIndexZero = bytes[0]; // << read the first element (will be UINT8_MAX)
...
free(bytes), bytes = 0;
char * buffer = malloc(5000);
buffer[idx] = whatever;
char * p = buffer + idx;
*p = whatever;
Malloc doesn't initialize the bits allocated by it. Use calloc() rather.
int *p = malloc (5000); // p points to the start of the dynamically allocated area.
As has been mentioned by others, you could do something like this:
int nbytes = 23; // number of bytes of space to allocate
byte *stuff = malloc(nbytes * sizeof stuff[0]);
stuff[0] = 0; // set the first byte to 0
byte x = stuff[0]; // get the first byte
int n = 3;
stuff[n] = 0; // set the nth byte to 0
x = stuff[n]; // nth byte, or in the case of some other type, nth whatever - just make sure it's a safe value, from 0 (inclusive) to the number (nbytes here) of things you allocated (exclusive)
However, a couple of things to note:
malloc will not initialise the memory, but calloc will (as mentioned by Prasoon Saurav)
You should always check to see if the memory allocation failed (see below for an example)
int nbytes = 23; // or however many you want
byte *stuff = malloc(nbytes * sizeof stuff[0]);
if (NULL == stuff) // memory allocation failed!
{
//handle it here, e.g. by exiting the program and displaying an appropriate error message
}
stuff[0] = 0; // set the first byte to 0
byte x = stuff[0]; // get the first byte
int n = 3;
stuff[n] = 0; // set the nth byte to 0
x = stuff[n]; // nth byte, or in the case of some other type, nth whatever
malloc() returns a pointer to the allocated memory:
typedef unsigned char byte;
byte * mem = malloc( 5000 );
byte val = mem[1000]; /* gets the 1000th byte */
After using malloc() to initialize 5000 bytes of memory, how would I
reference the bytes in this memory space? For example, if I need to
point to a starting location of data within the memory, how would I go
about that?
Does it matter what I use to point to it? I mean I am seeing people
use bytes/int/char? Is it relevant?
as you have seen malloc allocates a block of memory counted in bytes, you can assign a pointer to that block and depending on the pointer type the compiler knows how to reference individual elements:
unsigned char *memblob = malloc( 1024 );
short* pshort = (short*)memblob;
now if you reference the second short value i.e. *(pshort + 1) or pshort[1] the compiler knows that it needs to add 2 bytes (sizeof(short)) in order get the next element.
float* pfloat = (float*)memblob;
now if you reference the second float value i.e. *(pfloat + 1) or pfloat[1] the compiler knows that it needs to add 4 bytes (sizeof(float)) in order get the next element.
same with own defined data types:
typedef struct s
{
short a;
long b;
} mystruct_t;
mystruct_t* pstruct = (mystruct_t*)memblob;
pstruct + 1 accesses the struct at offset sizeof(mystruct_t)
so it is really up to you how you want to use the allocated memory