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)
443
questions
0
votes
1answer
73 views
memset() vulnerability [closed]
I am using memset in an embedded application to delete one data. here is the function:
uint8_t Delete()
{
memset(cure.name, 0x0, 32);
cure.volume = 0;
cure.valid=0;
printf("[*...
1
vote
0answers
75 views
Why memset does not work for vector of bool? [duplicate]
I was doing some problem in which I required to reset values in a vector of bool after some searching I found out that memset() is faster than fill() so I tried to use it like this:
vector<bool>...
0
votes
1answer
48 views
Is there any performance advantage of initialising an integer array with memset instead of default zero initialisation like array[10]={0}?
Given that memset can be only used to initialise an integer array with the values 0 or -1 what would be the reason that you use it for zero initialisation. Does its efficacy lie solely in string ...
0
votes
2answers
41 views
Use memset for 2d array at specific row
recently, I have learnt about C++. Specially, I learned about memset function. But I don't know how to set value for 2d array at specific row
Example:
int dp[10][10];
// I want to set all values for ...
2
votes
2answers
41 views
memset = program crash
Iam struggle with memset.
If i write in my array element my programm crash. If I comment out the memset i have no problems to write..
My type struct:
typedef struct
{
char Frage [maxLEN_F_A];
...
2
votes
1answer
72 views
Implementation of memset to set a whole word instead of byte by byte in C
So I'm trying to implement my personal MemSet that will do the same as memset but also:
Copy word size chunks when possible, instead of byte by byte.
Guarantee dest's alignment
Test for all ...
0
votes
2answers
78 views
error clearing an object of non-trivial type with memset
Well, the thing is simple, im getting
warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘struct FormatHashBuffers(CBlock*, char*, char*, char*)::<unnamed>’; use ...
1
vote
1answer
50 views
fill_n vs memset to set default value of an array
I wonder what is the ideal way if you want to fill an array with a default value n:
#include <cstring> // for memset
#include <algorithm> // for fill_n
static constexpr int N = 100;
int a[...
1
vote
1answer
58 views
How to use memset to fill in max value for an array?
Here is my code, I would need to fill in every element in the 2d array with the maximum size ( INT_MAX), however, my code was unable to work properly?
#include <iostream>
#include <string.h&...
-1
votes
2answers
63 views
dynamically allocated memory with garbage characters
While using the following approach to read contents of a file:
struct stat st;
stat(argv[i], &st);
int size = st.st_size;
//allocate memory to the whole file size
buffer = malloc(size);
while (...
2
votes
1answer
74 views
Which are the use case of punpcklbw (interleave in MMX/SSE/AVX)?
Which classes of algorithms could be using punpcklbw ?
In particular, what is punpcklbw xmm0, xmm0doing ?
And yet, what is maskedPow2_Value useful for ?
maskedValue = 0x101010101010101i64 * *(_QWORD ...
1
vote
0answers
95 views
Trying to understand clang/gcc __builtin_memset on constant size / aligned pointers
Basically I am trying to understand why both gcc/clang use xmm register for their __builtin_memset even when the memory destination and size are both divisible by sizeof ymm (or zmm for that matter) ...
0
votes
3answers
68 views
Memset function in programm that resolves host adresses
First of all, i understand what the code (see at the end of the post) does in general and i dont want an overall explanation.
What i don't understand is this particular line of code:
memset(&hints,...
1
vote
1answer
40 views
Memset() in Python, clearing buffer
I trying convert some C code to Python and have problem with clearing buffer (data which will be send by UDP). Function to convert looks like:
void func(Struct *data_1, Struct *data_2)
{
data_p1 = ...
2
votes
0answers
54 views
For aligned memcpy and memset REP MOVSD and REP STOSD vs temporal and non-temproray ymm / zmm registers
I have seen a few very informative posts here and here discussing the merits of difference approach for copying / setting memory.
All of the posts go into detail about the pros and cons of REP MOVSD ...
0
votes
1answer
219 views
Calloc implementation
I made a ft_calloc and ft_memset, they both work but I don't understand why.
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
unsigned char *p;
i = 0;
p = (...
1
vote
2answers
65 views
Setting part of an array to zero efficiently
I am trying to reset an array of simple objects to zero, as shown below:
using Node = std::array<std::uint16_t, 256>;
using Array = std::array<Node, 4096>;
void reset_1(Array& a) {
...
0
votes
0answers
29 views
What is the purpose of `void*` return in memset? [duplicate]
I'am pretty amateur in C, please forgive me if this question is silly.
This is memset implementation in GCC took from here:
PTR
memset (PTR dest, register int val, register size_t len)
{
register ...
0
votes
4answers
65 views
Memory leak using Malloc/Free in C
I've been reading up on the use of pointers, and allocating memory for embedded projects. I must admit, that i perhaps don't understand it fully, as i can't seem to figure where my problem lies.
My ...
1
vote
0answers
41 views
Why memset works to initialize 2D array in C++ with zero but failed to set when declared like this `array[n][k+1]={0}`? [duplicate]
I have initialized an array like this array[n][k+1]={0} where n and k are user input. But I found that it doesn't initialize all index values to zero. While on the other hand memset(array,0,sizeof(...
0
votes
1answer
94 views
Problems with simple threadpool and TCP listener in C
I've implement a multi threaded TCP listener in c with a simple thread pool logic and I have two main problem after I tested with a for((i=1;i<100000;i++)) do echo "Hi $i" |nc 127.0.0.1 ...
1
vote
2answers
85 views
Prime Number above 6 million
I was solving a question in Hackerrank and the question was to find the prime number count in a range. Since using the normal methodology was facing timeout, I used Sieve of Eratosthenes. Most ...
0
votes
1answer
61 views
How do I set the buffer value to zero?
after I memory copy the value from GPU to CPU, how do I set the value to 0 with the same length? For example, cpubuff here.
cudaMemcpyAsync(
(void*) cpubuff_,
(const void *) src_,
(...
0
votes
1answer
95 views
Initializing std::memset
I'm using std::memset to solve a LeetCode problem copied here. Currently, map_cherries has been initialized as:
map_cherries[70][70][70] = {}
...
std::memset(map_cherries, -1, sizeof(map_cherries));
...
2
votes
2answers
99 views
Why can I use more memory than allocated? [duplicate]
I was messing around with the memset function and did this:
int* p = malloc(sizeof(int);
memset(p, 0, 10000);
I was wondering why this is valid. I've only allocated 5 bytes of memory yet I can take ...
0
votes
1answer
61 views
Does memset change the array length when another array is memset?
program-1->
#include <iostream>
#include <cstring>
int main()
{
char a[4];
memset(a, 'A', sizeof a);
std::cout << a << '!';
return 0;
}
program-1 outputs ...
2
votes
2answers
37 views
How to set a huge head allocate array as specific float number C++
At first, I allocate a huge block memory by using Intel Math Kernel Library API. After that I want to set this array as specific value, for example 1234.5678. I plan to use the void* memset( void* ...
-1
votes
6answers
342 views
How to initialize a 2D array with all values same?
I want to initialize a 2D array with -1 as all the value. I used memset() for this.
#include <bits/stdc++.h>
using namespace std;
int dp[100][100];
memset(dp, -1, sizeof(dp));
int ...
0
votes
1answer
3k views
clearing an object of type 'struct ' with no trivial copy-assignment; use assignment or value-initialization instead
I am working on a module containing C and C++ codes. The problem is that I am getting the following forbidden warning. I provided the code that Is causing that warning.
warning: 'void* memset(void*, ...
0
votes
0answers
128 views
::memset crashes while initialising large array
I am writing a C++ program that is aimed to work on a large volume of data. I was testing a particular function HVE_KeyGen() which takes in one large array along with other parameters. I have written ...
2
votes
2answers
69 views
Why am i getting an exception at dynamic_cast after filling object with zeros using memset
I am getting this weird runtime exception with dynamic_cast, but only if i fill the the object i'm casting with zeros using memset, or just copying some data into it with memcpy . Here's an example ...
1
vote
1answer
170 views
Does cudaMalloc() initialize the array to 0?
Or do I need to perform cudaMemset() if I want to make sure the array contains all 0? I can't find it in the documentation.
Thank you.
0
votes
0answers
127 views
Why doesn't Clang merge these consecutive and adjacent calls to memcpy or memset?
When compiling this with Clang 10.0.0 and current Clang-trunk it seems to be missing some pretty obvious optimization oppertunities:
struct A {
int x[16] {0}; // Everything zero init by default
...
1
vote
1answer
78 views
A faster or slower way to clear truncated pointers?
In a truncate implementation I've read recently, the author uses the following way to clear the truncated items:
var nilItems = make(items, 16)
func (s *items) truncate(index int) {
var ...
0
votes
1answer
31 views
volatile argument in sc_memset
A part of the C program that I am writing includes initialization of an input table using sc_memset() function. The table is defined by a typedef struct as shown below.
typedef struct {
UINT32 ...
0
votes
1answer
114 views
The memset function is giving me an error when using VS C++
I am using memset to clear a buffer:
char header_buffer[100];
memset(header_buffer[100],0,sizeof(header_buffer[100]));
I'm getting a compile error:
Error C2664
'void *memset(void *,int,size_t)'...
0
votes
1answer
56 views
Memset struct variables separately vs memset entire struct, which is faster?
Say I have a structure like this:
struct tmp {
unsigned char arr1[10];
unsigned char arr2[10];
int i1;
int i2;
unsigned char arr3[10];
unsigned char arr4[10];
};
Which of ...
0
votes
0answers
55 views
How would I set the value of '0xA5' as the first 5 bytes of my string “Now the Message is: ”?
Currently my output looks like this:
But I'd like to replace the first 5 bytes of the string "The end is near!" with 'A5' at the end of my x_memset function following the statement "Now the message ...
0
votes
0answers
53 views
Why should I (void) the return of memset()? [duplicate]
When I was looking for some inspiration, I stumbled across the following code:
(void)memset(&ifm, 0, sizeof(ifm));
https://github.com/i3/i3status/blob/master/src/print_eth_info.c#L55
I know (...
-2
votes
1answer
74 views
Calling mmap from inside function in C
I have recently start playing with mmap and memory segments. I want to call mmap from inside function but I have problems to get it working. I can call mmap from main and it works. I know it's about ...
0
votes
1answer
119 views
memset initialization of struct freeze
Some library I use in an embedded (STM32F103) project initializes it's structs with memset() which for some reason causes the program to stop responding/hang.
To test this I used:
typedef struct
{
...
1
vote
1answer
427 views
memset with Swift 5.2
Sometimes I need to clear a part of a byte array to zero. With a for loop this is slow. Much faster is using the memset() function. I have the following code which will clear elements 200 to 299 of ...
0
votes
1answer
51 views
Why memset is needed before strptime
In order for this code to run properly, memset must be used. Why is that?
struct tm temp;
memset(&temp, 0, sizeof(struct tm));
strptime(str.c_str(), "%d-%m-%Y", &temp);
Why this is not ...
0
votes
1answer
122 views
Using memset with pointer to an array
How do I memset an array using a pointer to that array?
int *A, n;
printf("\n Enter the size of array : ");
scanf("%d",&n);
A = (int*) malloc(sizeof(*A) * n);
int i = 0;
memset(*A,5,n * ...
2
votes
1answer
90 views
Result of my implementation of memset only prints the changes, and not the entire result string
This is the same implementation experiment from memset movq giving segfault
I've been printing out memset's result, and it seems to only print out the change, and not the rest of the string as well.
...
1
vote
1answer
116 views
memset movq giving segfault
I am getting a segfault at the movq (%rsi, %rcx) line.
I know you can't do mem->mem mov, so I did it through a temporary register. (%rsi), %rcx, then in the loop %rcx, (%rdi). Here is my code:
...
-7
votes
1answer
175 views
What causes the segmention fault in this memset()?
So I know a seg fault occurs but I can't pinpoint the internal logic behind it.
I believe that it is because there was a failure to initialize a pointer before accessing it.
But what would trigger ...
2
votes
2answers
87 views
unable to initialize array in c | warning: conversion to long unsigned from long
I'm trying to write a program which starts by creating a 2D array that will represent a graph (note that the dimensions of the graph will be given as input).
I want to initialize this array at 0 and ...
0
votes
1answer
89 views
Why I get access writing violation when using memset with a big _Size?
So I have a variable in my C++ code of type _Scalar who can takes a lot of different types (int, double...).
So I initialize my pointer with the new operator in my template class. When I want to ...
7
votes
2answers
425 views
Linux kernel NULL-pointer dereference in memset from kzalloc
Quite by chance stumbled upon some code in kernel jungles and was a bit confused. There are two implementations of kzalloc(): in tools/virtio/linux/kernel.h and the main one in linux/slab.h. Obviously,...