Questions tagged [memset]
memset is a C standard library function that sets the first N bytes of the block of memory to the specified value (interpreted as an unsigned char)
505
questions
2
votes
1
answer
208
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
84
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
67
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
241
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
53
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
118
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
49
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
201
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
182
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
371
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
74
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 *...
1
vote
2
answers
296
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
369
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
159
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
84
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
64
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
704
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
616
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
194
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
318
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
347
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
417
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
119
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
324
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
98
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
187
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
137
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
1k
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
474
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
112
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
484
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
68
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
93
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
27
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
298
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
332
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* ...
25
votes
3
answers
1k
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
384
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
606
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
448
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....
4
votes
0
answers
338
views
nlohmann json: securely erase the keys (C++)
I'm using nlohmann/json library to represent sensitive information. Once the needed processing has been completed, I'm interested in securely erasing the keys of the json type.
Example:
json test;
...
0
votes
1
answer
510
views
Is this the correct way to use memset() in C? [closed]
So to start out I am a noob with C and am learning memory manipulation. I have a buffer that is called void* buffer. I then want to put an integer into the first 4 bytes of that buffer (since an int ...
0
votes
2
answers
507
views
Memset function and bool arrays
I was trying to learn about memset function and I read an article that we cannot initialise int array to 1 using memset. But I have declared a bool array and initialised it to true using memset ...
2
votes
1
answer
1k
views
Measure memory write bandwidth using C
I'm trying to measure the write bandwidth of my memory, I created an 8G char array, and call memset on it with 128 threads. Below is the code snippet.
#include <stdio.h>
#include <stdlib.h>...
0
votes
3
answers
490
views
Fill 2D std::array of uint16_t in C++
I have a matrix in C++ defined as a std::array of std::array that I would like to set uniformly to a given value. I can't find something as simple as C-style memset for C-Style array (int a[10][10] ...
0
votes
1
answer
493
views
Memset too slow on large data. Any alternatives?
I have a large cv::Mat with dimensions (100,32768). I update it for every frame in a video stream. Before updating, I need to set everything back to zero so I execute
memset(myMat.data,0,100*32768*...
5
votes
2
answers
522
views
C++ placement new after memset
Suppose there's a struct whose constructor does not initialize all member variables:
struct Foo {
int x;
Foo() {}
}
If I memset some buffer to 0, use placement new on that buffer to create an ...