511
questions
3
votes
1
answer
133
views
Why did GCC turn a for loop to clear an array into a memset?
For a project to learn C, I decided for fun to put the code in Compiler Explorer and compare the assembly output with the C code. Here's a minimal example of some code
unsigned char count[256][256];
...
0
votes
0
answers
45
views
ZynqMP memset() raises unhandled alignment fault error
I'm having trouble using memset() in C; let me explain.
I'm working with embedded Linux on a zynq ultrascale+ board running Linux kernel v4.14. I'm performing some DMA transfers from the logic side of ...
0
votes
0
answers
149
views
ARM64. Calling memset() on the address returned by mmap() PCIe BAR causes a BUS error
I wrote a very simple test program in the application layer. First, I open the PCIe device, then mmap() the starting space of the PCIe BAR. Finally, I use the address returned by mmap(), apply an ...
0
votes
1
answer
94
views
memset bus error after mmap when init shared memory
The scenario is one process writes the program metric stats data to shared memory file.And another process will read the file and send the data to metric server.
The bus error and core dump occurs in ...
-1
votes
2
answers
114
views
Why does memset not store all values similarly in 2D array?
I have a 2D array set as
long long arr[500][500]
Running this main function, I get different values for m=-1 and m=-3:
int main(){
memset(arr,m,sizeof(arr));
for(int i=0;i<10;i++)
...
1
vote
2
answers
123
views
How correctly memset array of bool
I have bool array and I want initialize all elements to false. What is the correct way using memset. I see these examples on stackoverflow:
bool arr[10];
memset(arr,0,sizeof(arr));
and
bool arr[10];
...
5
votes
1
answer
219
views
Is it legal to zero empty C++ classes in the constructor and inherit from them?
It is not very rare to find a library that zeros its objects in the constructors using memset. And sometimes it happens to empty classes as well. Is it safe, especially when one inherits such classes?
...
2
votes
1
answer
467
views
Efficient way to reset array of structs which contain a std::atomic member?
Let's say I have a struct Foo s. t.
struct alignas(64) Foo {
std::atomic<int> value;
Foo* other;
};
Then, if I have an array Foo array[2048]; of Foo's: I already have initialized the ...
0
votes
0
answers
118
views
Memset warning treated as error, based on its thrd argument [duplicate]
When built without AM_CXXFLAGS += -Wno-memset-elt-size on the AutoMake Makefile, i get this error:
error: ‘memset’ used with length equal to number of elements without multiplication by element size [-...
0
votes
2
answers
78
views
Memset memory allocation
given the following code in C:
char str[] = "";
size_t size = strlen(str) + 1;
char *actual = memset(malloc(size), '@', size);
After I run this, actual = "@" .
If malloc ...
0
votes
3
answers
662
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,
...
0
votes
0
answers
91
views
when use memset() to clear .bss area, PC pointer suddenly points to a strange address
In the initialization code of ARMv8, I intend to use the memset function to initialize the BSS segment. However, when reaching a certain line of the memset function, the PC pointer suddenly points to ...
-1
votes
1
answer
143
views
Unexpected behavior of memset in C++
I have noticed unexpected behavior of memset() when used with dynamic arrays.
// This works well, sets value of each index = 0
int dp[5];
memset(dp, 0, sizeof(dp));
// This one doesn't work as ...
0
votes
1
answer
99
views
Understanding memset c
I'm trying to understand how memset works
Here is my implementation :
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
*((char *)(s + i)) = c;
...
2
votes
1
answer
555
views
What are the correct types for memset function?
On trying to recreate "memset" function I have error if I try to dereference a void pointer, or have a different type for the second argument than the first. To make the function work I had ...
0
votes
3
answers
349
views
warning: 'memset' will always overflow [-Wfortify-source]
Trying to manipulate IPv6 addresses in struct sin6_addr, e.g. cleaning it:
struct sockaddr a;
memset(&(((struct sockaddr_in6 *)&a)->sin6_addr.s6_addr), 0, 16);
printf("SIZEOF: %lu\n&...
3
votes
1
answer
808
views
Why does gcc choose the most basic memset() implementation?
My bare-metal program manually calls memset() to zero entire/aligned 4k pages like this (I'm not using uint64_t, but another 8-byte thing):
uint64_t something[512] __attribute__((aligned(4096)));
...
2
votes
0
answers
109
views
Why does memset receive the second argument as an integer? [duplicate]
The man page of memset function in C, says
The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
And the signature of memset is as below:
void *...
2
votes
2
answers
732
views
Memset memory that will not be optimized away
This is related to this question: is there is a way to memset a buffer after finished using it (for security for example) without it being optimized out?
Trying to cast the pointer to volatile pointer ...
6
votes
3
answers
590
views
Assignment: create my own memcpy. Why cast the destination and source pointers to unsigned char* instead of char*? [duplicate]
I'm trying to create my own versions of C functions and when I got to memcpy and memset I assumed that I should cast the destination and sources pointers to char *. However, I've seen many examples ...
1
vote
0
answers
203
views
Is there a memset alternative for more than one byte? [duplicate]
Is there a memset or memcpy operation to set a single int16, or int32 to an entire array?
Basically I have an integer array and I want to broadcast a single value to all of its elements. memset only ...
0
votes
1
answer
109
views
Xcode malloc with memset did not cause memory increase on Release
I want to manually create a oom by code below:
[NSTimer scheduledTimerWithTimeInterval:0.1 repeats:true block:^(NSTimer * _Nonnull timer) {
void *bytes = malloc(1024*1024*50);
memset(bytes, 1, ...
2
votes
2
answers
91
views
Value assignment using calloc
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
int main()
{
int size = 5 ;
int* c = (int*)calloc(size,sizeof(int));
memset(c,0,size*sizeof(int)); // when I ...
0
votes
2
answers
1k
views
Understanding Buffering using memset() [closed]
There are a few questions on here about how to use memset() in C or what a buffer is. But I have yet to find a good explanation for why it is necessary to use and what it actually does for your ...
1
vote
1
answer
975
views
Pointers, structs and memset in C [duplicate]
I have been learning C for a few days now without any other programming experience, so I might not be clear when asking my question. It is mostly about pointers. For convenience purposes I named the ...
0
votes
3
answers
355
views
Why does casting the pointer change the value at the address?
So I was doing an exercise to see if I was using memset correctly.
Here's the original code I wrote which was supposed to memset some addressese to have value 50:
int main(){
int *block1 = malloc(...
0
votes
2
answers
356
views
What is ((unsigned char *)b)[i] = c; mean in c?
void *ft_memset(void *b, int c, size_t len)
{
size_t i;
i = 0;
while (i < len)
{
((unsigned char *)b)[i] = c;
i++;
}
return (b);
}
2
votes
2
answers
580
views
gdb - how to call memset for the array of pointers
I debug an example program which defines the array of pointers:
int a = 1, b = 2, c = 3;
int* t[] = {&a, &b, &c};
I would like to set all pointers in the array to NULL during debugging. ...
1
vote
0
answers
854
views
Configure dev environment correctly to use memset_s function (C++)
I've to perform a call to the memset_s method but I've the error that is not defined.
I've declared STDC_WANT_LIB_EXT1 and set to 1 before including string.h file
#define __STDC_WANT_LIB_EXT1__ 1
#...
0
votes
0
answers
126
views
Why does initializing a string member of a zeroed struct with "" cause a crash but not if the string has length of 1 or greater? [duplicate]
I am using g++ with the c++ 17 standard
In the accepted answer for question memset() or value initialization to zero out a struct? it states that
struct TestStruct
{
int a;
std::string b;
};
...
5
votes
1
answer
395
views
What governs that std::memset may be used if the destination object is trivially copyable and is not a potentially-overlapping subobject?
Cppreference's page on std::memset states:
std::memset
// Defined in header <cstring>
void* memset( void* dest, int ch, std::size_t count );
[...] If the object is a potentially-overlapping ...
0
votes
2
answers
115
views
memset with math class macros
I am trying to use memset with INFINITY or NAN present in C header file <math.h>
My code is:
double *dist;
dist = (double *)malloc(7*sizeof(double));
memset(dist, INFINITY, 7*sizeof(dist[0]));
...
0
votes
1
answer
250
views
Fastest way to fill complex matrix with incoming data. Armadillo Library
I have situation where each 1ms is coming new buffer of 2*200 samples of int16_t. (multiplied by 2 is because complex data)
Those data should be added into Armadillo Complex Double Matrix, in the ...
0
votes
0
answers
146
views
memset() doesn't initialize a dynamic array correctly
I have a matrix class and there is a constructor like this:
// m is the 2d array "T **m", r = no. of rows and c = no. of coumns
matrix<T>(int r, int c, T v = T()) : r(r), c(c) {
m =...
2
votes
3
answers
2k
views
How to properly solve memset() function MISRA errors in C?
I have written a simple function to initialise the structure values using memset(). These are the code I have written in C language.
myfile.h
typedef struct{
bool flag;
bool check;
int val_1;
int ...
0
votes
1
answer
667
views
C: check if an array element is empty after zero-out using memset
I am trying to zero out an array element and check its status so it won't be used again, however, it seems doesn't work.
int main(int argc, char const *argv[]){
char pid[10][20] = {"abc", &...
1
vote
0
answers
144
views
Does Dead Store Elimination work on class member variables in the Destructor?
I'm aware that DSE is a problem for crypto developers who primarily use C, particularly with the reliance on memset. However, would the same be true if a class zeroes out its member variables within ...
1
vote
2
answers
771
views
Properly initialise a struct in C++, specifically addrinfo
I am wondering how I can properly initialise a struct in a "C++-way".
I am filling an addrinfo-structure with the relevant information for my hints (to use in getaddrinfo()).
Depending on ...
0
votes
1
answer
71
views
Why does memset print different value when it's set to non zero? [duplicate]
I am trying to understand memset and pointer to an array. Below is my program and when I set the array contents 0 the value of *(p+6) prints 0.
But when I set the value to 5 *(p+6) it prints 84215045
...
0
votes
0
answers
121
views
Is it possible to securly remove a variable and its contents out of memory in PHP?
I am looking for a way to remove a variable and its contents out of ram securly within PHP. I know its possible in C/C++ and of course in underlying assembler. In C/C++/Asm so long as you don't copy ...
1
vote
1
answer
35
views
3rd argument is ignored when I use memcpy in C
I'm a beginner in C and try to use memcpy to send byte in order to transfer data to server. It seems like no matter what I choose for 3rd argument. It's always the same.
#include <stdio.h>
#...
0
votes
0
answers
42
views
memset and static array declaration vs malloc [duplicate]
Malloc is used for dynamic memory allocation. However, this code doesn't use malloc, but is dynamic. In what case would I use malloc? Or is this code doing something really bad?
Sometimes, I see this ...
0
votes
1
answer
413
views
what is `__GI_memset`? why does it cost so much CPU resource?
I'm new to perf, and I'm trying to use it to analyse my programme.
and I got this when running perf top:
PerfTop: 296 irqs/sec kernel:62.8% exact: 0.0% [1000Hz cycles:ppp], (all, 6 CPUs)
-----...
1
vote
1
answer
452
views
How to improve code around flawfinder memset warning?
In my code, all calls to memset appear as warnings with the flawfinder tool.
In the simplest case it could boil down to the equivalent to
float f1;
float f2;
void* p1 = &f1;
void* ...
26
votes
3
answers
2k
views
When is memset to 0 nonportable? [duplicate]
From this comment in GCC bug #53119:
In C, {0} is the universal zero initializer equivalent to C++'s {} (the latter being invalid in C). It is necessary to use whenever you want a zero-initialized ...
2
votes
2
answers
440
views
Not being printed out when using fgets
I'm learning about file input/output in C. But there is something I don't understand.
I was about to print the simple sentence in the [words.txt] using fgets, but it doesn't work.
[words.txt] as below:...
0
votes
1
answer
1k
views
Check if a struct is empty [duplicate]
I have an old piece of code with a big struct that looks like this:
typedef struct {
long test1;
char test2[10]
…
} teststruct;
This struct gets initialized like this:
memset(teststruct, 0, sizeof(...
1
vote
2
answers
2k
views
bare metal embedded c++: undefined reference to memcpy and memset while writing flash to ram
As the tittle suggest, I'm getting an error related to not defining memcpy and memset, even though I'm not directly using it (although I guess it is used for the linker and compiling process given the ...
0
votes
1
answer
1k
views
stack corruption detected when use memset in c++ from JNI Android
I am developing an Android application using C++ native code.
I have C++ code (XTTEA Algorithm in C++ native) which perfectly runs online with C++ compiler and I can get the output, but when I try to ...
2
votes
1
answer
652
views
Strange characters in String C
I was writing a simple compiler just for fun and I wanted each line to have proper indentation.
So I wrote a function that returns an array of characters with some white-spaces depending on the number....