memset

The necessity to memset with '\0', in a toy example

旧时模样 提交于 2019-12-07 13:30:41
问题 I encountered the following example of using memset in tutorialspoint: (I am not familiar with C.) #include <stdio.h> #include <string.h> int main(){ char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); } I don't get why the memset line is used, as the compile and result are the same when that line is commented. I would like to ask is that line necessary? or is it a

C-基础:memcpy、memset、memmove、memcmp、memchr

流过昼夜 提交于 2019-12-07 09:19:27
一,原型 void * memcpy ( void * destination, const void * source, size_t num ); 功能: 将以source作为起始地址的数据复制num个字节到以destination为起始地址的数据中,不支持destination和source重叠的情况。函数返回destination指针。 void * memcpy ( void * destination,constvoid* source,size_t num ) {    char * pdes =( char * )destination;    char * psrc =( char * )source;   assert(destination !=NULL && source !=NULL && num> 0 );    while (num-- )      *pdes++=*psrc++ ;    return destination; } void * memmove ( void * destination, const void * source, size_t num ); 功能: 将以source作为起始地址的数据的num个字节移动到以destination为起始地址的数据中,支持destination和source重叠的情况

标准仿C语言 memset、memcpy、memmove函数(含C#版)

ε祈祈猫儿з 提交于 2019-12-07 09:16:36
标准仿C语言 memset、memcpy、memmove函数 前段时间,移植了C的网络加解密算法到C#,其中遇到很多问题,核心问题大多都是字节拷贝问题,今天有时间整理了一下相关的API,废话不多说直接贴源码: (值得注意的是memmove函数,支持数据重叠,详情参照 http://blog.csdn.net/yujun_wu/article/details/4999565 ) 一> C++版 /* * @brief: 将dst中当前位置后面的 count 个字节用 val 替换并返回 dst * @param s - 数据内存的起始地址 * @param c - 替换后的值 * @param n - 要替换的长度 * @return 返回原数据首地址 */ void* memset(void* s, int c, size_t n) { assert(s); unsigned char* p = (unsigned char*)s; while (n > 0) { *p++ = (unsigned char)c; --n; } return s; } /* * @brief: 用于从src拷贝count个字节到dest,如果目标区域和源区域有重叠的话 ,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中。 但复制后src内容会被更改

牛客 题库 memcpy memmove memset strcpy

谁说我不能喝 提交于 2019-12-07 09:14:52
1. 以下哪个函数可以在源地址和目的地址的位置任意的情况下,在源地址和目的地址的空间大小任意的情况下实 现二进制代码块的复制? memcpy、 memmove、 memset 、strcpy 解答: memmove 1) memcpy 函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以dest指向地址为起始地址的空间内。 头文件 #include<string.h> 返回值    函数返回一个指向dest的指针。 说明    1. source和dest所指内存区域不能重叠 ,函数返回指向destin的指针。    2.与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节。 memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度; 例: char a[100], b[50]; memcpy(b, a,sizeof(b)); // 注意如用sizeof(a),会造成b的内存地址溢出。 strcpy 就只能拷贝字符串了,它遇到'\0'就结束拷贝 ;例: char a[100], b[50]; strcpy(a,b);   3.如果目标数组destin本身已有数据,执行memcpy()后,将覆盖原有数据

Using memset on structures in C++

杀马特。学长 韩版系。学妹 提交于 2019-12-07 01:58:40
问题 Hey guys. I am working on fixing older code for my job. It is currently written in C++. They converted static allocation to dynamic but didn't edit the memsets/memcmp/memcpy. This is my first programming internship so bare with my newbe-like question. The following code is in C, but I want to have it in C++ ( I read that malloc isn't good practice in C++). I have two scenarios: First, we have f created. Then you use &f in order to fill with zero. The second is a pointer *pf. I'm not sure how

create my own memset function in c

社会主义新天地 提交于 2019-12-06 23:09:29
here is the prototype: void *memset(void *s, int c, size_t n) first im not sure if I have to return something because when I use the memset i do for example memset(str, 'a', 5); instead of str = memset(str, 'a', 5); here is where I am with my code: void *my_memset(void *b, int c, int len) { int i; i = 0; while(b && len > 0) { b = c; b++; len--; } return(b); } int main() { char *str; str = strdup("hello"); my_memset(str, 'a', 5); printf("%s\n", str); } I dont want to use array in this function, to better understand pointer and memory, so I dont get 2 things: - how to copy the int c into a

Is it well-defined to use memset on a dynamic bool array?

半腔热情 提交于 2019-12-06 20:59:38
问题 Is this code well-defined behavior, in terms of strict aliasing? _Bool* array = malloc(n); memset(array, 0xFF, n); _Bool x = array[0]; The rule of effective type has special cases for memcpy and memmove (C17 6.5 §6) but not for memset . My take is that the effective type becomes unsigned char . Because the second parameter of memset is required to be converted to unsigned char (C17 7.24.6.1) and because of the rule of effective type, (C17 6.5 §6): ...or is copied as an array of character type

C语言各种变量的初始化

佐手、 提交于 2019-12-06 17:49:07
数值类变量初始化 整型、浮点型的变量可以在定义的同时进行初始化,一般都初始化为 0 。 int inum = 0 ; float fnum = 0.00f ; double dnum = 0.00 ; 字符型变量初始化 字符型变量也可在定义的同时进行初始化,一般初始化为 '\0' 。 char ch = '\0' ; 字符串初始化 字符串初始化的方法比较多,我这里简单介绍三种,因为字符串本质上是由一个个字符组成的字符数组,所以其初始化的最终目的, 就是将字符数组里面的一个个字符都初始化为 '\0' 。 方法一 :使用空的字符串 "" 。 char str [ 10 ] = "" ; 方法二 :使用 memset 。 char str [ 10 ] ; memset ( str , 0 , sizeof ( str ) ) ; 方法三 :写一个循环。 char str [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { str [ i ] = '\0' ; } 这里比较推荐的是第二种初始化方法。也即使用 memset 进行初始化。 很多人对 memset 这个函数一知半解,只知道它可以初始化很多数据类型的变量,却不知道其原理是什么样的,这里做一下简要的说明: memset 是按照字节进行填充的。 先看下面的一段代码: int num;

Can memset() be called with a null pointer if the size is 0?

試著忘記壹切 提交于 2019-12-06 17:36:43
问题 For one reason or another, I want to hand-roll a zeroing version of malloc() . To minimize algorithmic complexity, I want to write: void * my_calloc(size_t size) { return memset(malloc(size), 0, size); } Is this well-defined when size == 0 ? It is fine to call malloc() with a zero size, but that allows it to return a null pointer. Will the subsequent invocation of memset be OK, or is this undefined behaviour and I need to add a conditional if (size) ? I would very much want to avoid redundant

tuxedo服务器代码

牧云@^-^@ 提交于 2019-12-06 14:43:17
/* #ident "@(#) samples/atmi/simpapp/simpserv.c $Revision: 1.7 $" */ #include <stdio.h> #include <ctype.h> #include <atmi.h> /* TUXEDO Header File */ #include <userlog.h> /* TUXEDO Header File */ #include </home/zhangenhao/OraHome_1/tuxedo12.1.3.0.0/simpapp/clsinterface.h> #include <fml.h> #include "strPort.h" #include <string.h> #include <stdlib.h> #include "cJSON.h" #define ROW 4422 #define STRLEN 120 #define MOSTDOMAIN 200 #define DATALEN 2048 int lastIndexOf(char *dst, char *src); int indexOf(char *dst, char *src); void ltrim(char *str); void rtrim(char *str); void trim(char *str); void