1,618 questions
3
votes
2
answers
182
views
Force the C compiler to read the object's memory
I duplicated the topic because (possibly) I made a mistake with the section of the site and initially posted in the wrong place: Ways to reliably read an object in C
I have a regular byte buffer ...
Best practices
1
vote
5
replies
139
views
Ways to reliably read an object in C
I have a regular byte buffer declared in file scope:
static unsigned char buffer[32];
At some point, the function to start a DMA read operation into this buffer is called:
void func(void) {
...
3
votes
0
answers
92
views
Can I modify host data after cudaMemcpyAsync
Can I modify host data in host_data_ptr after the following ?
cudaMemcpyAsync(device_data_ptr,
host_data_ptr,
size,
cudaMemcpyHostToDevice,
...
3
votes
1
answer
160
views
Copying heap allocated memory in multiple threads... Is a mutex required or is it safe?
Environment is Ubuntu 24.04, QT Creator 6, g++ 13.3, pthreads.
Given the following code (error handling has been removed for readability):
#include <iostream>
#include <thread>
#include &...
3
votes
4
answers
231
views
Direct assignment crashes but memcpy() works?
It's a problem I ran into in my work. Our embedded platform is a customized Android OS running on quad-core Cortex-A7.
Background: I'm receiving a set of parameters from outside my process, and it's ...
8
votes
1
answer
339
views
Does memcpy require pointer to array rather than first element?
In the C++ standard the meaning of memcpy is taken from C (I will quote C++23 here but I don't believe the wording has changed from earlier versions):
The contents and meaning of the header are the ...
1
vote
1
answer
177
views
Is there a legit way of getting a "copy" of a non-copyable (nor-movable, fwiw) object?
This is not a XY question. I'm curious about what I'm asking. That's it. I'm not going to use std::memcpy() or other things to make copies of non-copyable objects anyway.
I know what copyable means, ...
0
votes
0
answers
61
views
Why doesn't memcpy update the MAC address pointer in my Contiki-NG Black Hole attack implementation?
I'm working on simulating a Black Hole attack in a TSCH network using Contiki-NG and Cooja (Z1 Motes ) with simple node which exists in the following path "contiki-ng/examples/6tisch/simple-node/...
-6
votes
1
answer
156
views
C++ memcpy of char arrays
I was exploring memcpy in C++. I have a program that reads 10 bytes from a file called temp.txt. The content of the file is:
abcdefghijklmnopqrstuvwxyz
Here's the code:
// This file is called "...
0
votes
1
answer
67
views
Segfault in LLVM
I have the following short LLVM program that is resulting in a segfault. I'm hoping someone can either tell me why or tell me how I can find out.
This is a much simplified version of code generated ...
1
vote
1
answer
97
views
Stuck on a practice problem involving arrays and modifying array of array elements [closed]
There is a problem on Exercism I'm stuck on. I should get the multiples of a factor that are less than the limit and then add them to an array, I should do this to all factors (get their multiples ...
12
votes
1
answer
412
views
Why does bit cast require both types to be trivially-copyable?
Why does C++ std::bit_cast require both To and From to be trivially-copyable?
For example:
From from{};
To to;
static_assert(sizeof to == sizeof from);
std::memcpy(&to, &from, sizeof to);
...
0
votes
1
answer
186
views
Is there any common PCIe DMA memcpy function in Linux
I meet the same problem with this guy
Memcpy from PCIe memory takes more time than memcpy to PCIe memory
Yes, MMIO read is very slow at x86/x86-64, only create/send the 32 or 64 bits of TLP payload ...
0
votes
4
answers
196
views
Does memcpy in C contain a buffer
P.S I am aware that memcpy should not be used to copy data to an overlapping memory address, and that memmove should be used instead.
Based on my understanding of memcpy, the function basically copies ...
-4
votes
1
answer
150
views
How do I copy a unsigned long long to a byte array in c++?
I have code that sets each byte of a byte array m individually from an unsigned long long named i. I want to do it in ONE operation using memcpy. So copy the value of i to m, so that each byte of m, ...
0
votes
0
answers
94
views
std::memcpy from an object to another one of same type through an intermediate array of bytes
This is a follow up of Bitwise swapping the internal representation of two simple objects, even if they are not of a trivially copyable type.
I merely simplify a bit OP snippet and make the considered ...
3
votes
2
answers
144
views
Question about implementations of a memcpy function
This is the code:
void *ft_memcpy(void *dst, const void *src, size_t n)
{
unsigned char *to;
unsigned char *from;
if (dst == NULL && src == NULL)
return (NULL);
...
2
votes
1
answer
127
views
Why am I unable to copy and execute a function in C?
I tried to write a C program that would copy a function to other memory location, and then execute it as a function pointer.
But I'm facing issues.
This is my code:
#include <stdio.h>
#include &...
4
votes
1
answer
220
views
Does std::bit_cast incur an overhead?
I'm looking for legit use-cases of std::bit_cast in my code bases. Yet current cppreference documentation makes me wonder if it may induce an overhead by creating a (named) temporary object (thus not ...
1
vote
0
answers
47
views
backtrace() falls on memcpy exception [duplicate]
I managed to break backtrace(). I so happend that I can't trace some program crashes like so:
void backtrace_init() {
struct sigaction sigact;
sigact.sa_sigaction = crit_err_hdlr;
...
1
vote
0
answers
185
views
Problem with memcpy implementation in x86_64 assembly language
I have a task to implement memcpy function in x86_64 assembler with this signature:
extern void* my_memcpy(void* dest, const void* src, uint32_t count);
I wrote it:
.intel_syntax noprefix
.text
....
0
votes
0
answers
51
views
Copying 8 bytes of data from union to pointer in Structured text
I am new to structured text and barely getting started . I need to copy 8 bytes of data I receive over a CAN to another variable in structure in Structured text . Here is my code
TYPE msg :
UNION
...
1
vote
1
answer
97
views
Dereferencing a Local Reference to a Global Struct Member vs. Memcpy’ing
I have a callee function invoked by a caller function that modifies the contents of a member of a field in a union. This union is nested within a strut, and the struct is globally scoped. However, the ...
0
votes
1
answer
137
views
Is an 8 byte memcpy() atomic on a 64bit linux machine?
I'm using an 8 byte shared memory segment in PHP, using shmop_* functions. By looking at PHP's source code, I see that internally shmop_write() and shmop_read() use memcpy() to fill/read those 8 bytes....
0
votes
1
answer
113
views
Why is the dst pointer changed when using memcpy function for flexible array in struct of this case?
#include <cstring>
#include <cstdio>
#include <cstdlib>
struct Block {
int32_t size;
u_int8_t data[0];
};
int main(int argc, char **argv) {
Block block;
Block *ptr =...
2
votes
1
answer
126
views
Why does memcpy result in an unexpected value?
#include <iostream>
#include <cstring>
#include <bitset>
using namespace std;
int main()
{
double tx = 0xFFFF0000FFFF0000;
uint64_t tx1 = 0;
static_assert(sizeof(...
1
vote
0
answers
207
views
Why is memcpy performance from heap to shared memory so poor?
Original question
I tested memcpy performance from heap to heap and from heap to shared memory (shm_open). The test codes are as follows:
// shm_msg.hpp
#ifndef _SHM_MSG_HPP_
#define _SHM_MSG_HPP_
#...
1
vote
0
answers
262
views
memcpy crashes in native shared object logic on particular device (VIVO / Samsung) and on 64bit devices only(arm64-v8a)
Recently, we made some optimization attempts in the Native code of a specific app in our company to address performance issues. We released a gray version containing the mentioned modifications to a ...
0
votes
1
answer
120
views
Why is using memcpy with custom structs that have pointers in them causing a heap buffer overflow?
I am making a program which requires multiple types of dynamic arrays in C89. What I am trying to do is create my own implementation of a dynamic array that supports custom struct data.
However, while ...
5
votes
1
answer
230
views
Meaning of "trivial eligible constructor" for implicit lifetime types and memcpy
Say you have a type that is trivially copyable, not an aggregate, and not trivially constructible:
struct Foo
{
Foo() = default;
Foo(int i)
: a(i)
{};
int a = 5;
};
Foo isn't an ...
4
votes
2
answers
133
views
Is assigning a struct variable to itself (via pointer dereferencing) defined behaviour?
If two pointers are pointing to the same struct variable, is it defined behaviour to assign one dereferenced pointer to the other?
struct Struct {
int member;
};
int main() {
struct Struct s, ...
0
votes
1
answer
134
views
using memcpy for copy constructor and assignment operator for 2d array
I have 2 classes.
Pointer to an array of fixed width and Pointer to a Pointer for implementing 2d array of floats.
I wanted to understand, if and how can I use memcpy for my Copy constructor and ...
0
votes
0
answers
341
views
A bus error that occurs when mmap memory is used with memcpy
Recently, when I make a Linux device driver and memcpy memory obtained with mmap, the Linux kernel has a bus error and I ask questions.
Here's the code that caused the problem
typedef struct buffer_t {...
-1
votes
2
answers
121
views
Memcpy copies more data into less size [duplicate]
I have declared 2 strings i.e string1 and string2. string1 is of size 8 and string2 is of size 200. Now, I am trying to copy string2 to string1. string1 has lesser size than string2. And I am copying ...
3
votes
1
answer
256
views
"memcpy must be given valid pointers": where can I find this in the standard?
It is stated on cppref that
If either dest or src is an invalid or null pointer, the behavior is undefined, even if count is zero
Same is stated for memset and memmove.
My question is: where is it ...
1
vote
0
answers
145
views
Do moves in rust always copy the stack allocation?
Lets say I have a large, non-Copy, stack allocated data structure, and then I move it a bunch of times:
struct Foo(u8);
fn main() {
// A big (8MiB) stack allocation
let big_stack_data: [Foo; ...
-2
votes
3
answers
436
views
Replacing reinterpret_cast by memcpy [closed]
I've got some legacy code containing the following line of code:
uint16_t* data = reinterpret_cast<uint16_t*>(array_.data()); // #1
With array_ of type std::vector. So far, I understood that we ...
4
votes
2
answers
133
views
Why does memcpy into a buffer and a pointer to the buffer work the same?
Say I have the following code:
char buff[100] = {0};
char msg[] = "hello!";
memcpy(buff, msg, sizeof(msg));
OR
memcpy(&buff, msg, sizeof(msg));
Why do the two memcpy lines work ...
1
vote
0
answers
65
views
C Memory Allocation For Dynamic char Pointer to chat Pointers Array
The C script uses dirent to fetch directories and stores paths fetched and files:
// Packages
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#...
-1
votes
1
answer
105
views
I want to use memcpy to copy from a certain byte in c
I want to copy some part of data into source_mac
This is my code:
uint8_t *data
uint8_t *source_mac
memcpy(&source_mac, &data[6], 6);
So I want to copy 6 bytes from data, starting with the ...
0
votes
0
answers
167
views
Do optimised calls to memcpy@plt respect restrictions on instruction sets?
In c++ I have the following piece of code, that is optimised in assembly to call memcpy.
void do_something(big_struct& a, const big_struct& b)
{
a = b;
}
lea rdi,[rsi+0x4]
mov edx,...
-3
votes
1
answer
423
views
How to properly memcpy without overflow issues [closed]
In a legacy code I am getting buffer overflow errors in fortify audit.
Let me explain the issue here:
I have a function, say foo(size_t len, unsigned char **buf) ,in this foo I am memcopying a string ...
-1
votes
1
answer
214
views
SYCL q.memcpy() & h.memcpy() & Intel developer cloud problems
I'm using SYCL on Intel Develope Cloud to test Innovative algorithms.
My questions:
SYCL q.memcpy() & h.memcpy() do not work. It seems that Intel
know about it
What is the status of this issue? ...
0
votes
3
answers
1k
views
Practical advantages of std::copy, std::equal, std::fill over memcpy, memset, memcmp
A few posts compare the performance between C++-style functions std::copy, std::equal, std::fill with C-style functions std::memcpy, std::memcmp, std::memset, for example :
memcpy vs std::copy,
...
2
votes
0
answers
175
views
How to recover from SIGBUS
Assuming some insane person designs an FPGA to produce a SIGBUS signal occasionally on valid memcpy accesses to mmapd addresses, as a weird way to communicate up, what is the best way to reset the ...
-1
votes
2
answers
235
views
How to Recover in C++ from memcpy "Bus Error" from FPGA?
Everything I have read suggests that memcpy does not throw an exception so try-catch statements cannot be used to handle such an error. I have been provided memory addresses and ranges by the ...
4
votes
0
answers
213
views
Why using non-temporal store instructions cannot reduce memory bandwidth usage? (Writes seem to be generating extra reads)
I want to use the non-temporary instruction to reduce the read bandwidth generated by write allocate during the memcpy process. The expected read and write bandwidth after optimization should be the ...
0
votes
1
answer
125
views
Memcpy for nested structures containing pointers
The following code gives a crash stating double free. How to fix this with leaving no memory leaks?
Trying to copy contents of tmp to list. Removing free from get_copy gives no crash. But that will ...
0
votes
0
answers
76
views
Spurious bytes added to end of DIY memcpy() implementation
I've been experimenting with memcpy() implementations and optimizations in C; I recently wrote a version which was supposed to use successive powers-of-two to speed up the copy:
first, large 8-byte ...
0
votes
2
answers
100
views
Copying struct into one element of an array
In the code I am working on, I need to do the following:
Create a map where each value is buffer of fixed size
Use memcpy to populate one element of one value of the map (that is, one element of an ...