memcpy

Does “&s[0]” point to contiguous characters in a std::string?

依然范特西╮ 提交于 2020-01-18 03:24:04
问题 I'm doing some maintenance work and ran across something like the following: std::string s; s.resize( strLength ); // strLength is a size_t with the length of a C string in it. memcpy( &s[0], str, strLength ); I know using &s[0] would be safe if it was a std::vector, but is this a safe use of std::string? 回答1: A std::string's allocation is not guaranteed to be contiguous under the C++98/03 standard, but C++11 forces it to be. In practice, neither I nor Herb Sutter know of an implementation

Avoiding memset for a multi-type structure

孤街醉人 提交于 2020-01-17 16:26:27
问题 I would like to avoid using memset() on a structure like this: typedef struct { int size; float param1; } StructA; typedef struct { StructA a; unsigned int state; float param2; } Object; Can I do something like this (pseudo code, I can't check right now)? Object obj; int *adr = (int*)&obj; for (int i; i < sizeof(obj); i++) { *adr++ = 0; } Will it set every member of obj to zero? EDIT : to answer questions on comments. I have been working on some cases (with uni-type structures), where memset

Avoiding memset for a multi-type structure

可紊 提交于 2020-01-17 16:25:23
问题 I would like to avoid using memset() on a structure like this: typedef struct { int size; float param1; } StructA; typedef struct { StructA a; unsigned int state; float param2; } Object; Can I do something like this (pseudo code, I can't check right now)? Object obj; int *adr = (int*)&obj; for (int i; i < sizeof(obj); i++) { *adr++ = 0; } Will it set every member of obj to zero? EDIT : to answer questions on comments. I have been working on some cases (with uni-type structures), where memset

memcpy() from smaller array to larger one

孤者浪人 提交于 2020-01-16 06:59:28
问题 I couldn't find any reference to this question. I have an array of structs which I need to resize into a larger array. both structs are completely initialized (each cell has a value other than NULL) say typedef struct Square { ... ... }Square; Square s1[1024]; Square s2[2048]; If I copy using memcpy() s1 into s2, how would s2 would look? I know it copies byte data. will the first 1024 cells would be the same as s1 and the remaining 1024 would be as they initialized? or does it affect them too

[非原创] 获取CPUID;

喜欢而已 提交于 2020-01-15 22:28:50
    【注】本文是参考百度百科对CPUID指令的介绍而简要写的一个总结;但并不一定所有CPU都能支持获取CPUID;关于CPU序列号的表示可能不同人有不同写法。还有关于CPU缓存的信息的获取,需要根据返回的信息去查一个对照表,看起来不够简洁明了,所以关于CPU缓存信息获取的代码我就不写进去了。     相关代码如下: code_getCpuId 1 // GetCPUID.cpp : Defines the entry point for the console application. 2 // 3 4 #include " stdafx.h " 5 #include < string .h > 6 7 typedef unsigned long DWORD; 8 9 #define CI_VENDOR 0x01 10 #define CI_BRAND 0x02 11 #define CI_SERIAL 0x04 12 #define CI_ALL (CI_VENDOR|CI_BRAND|CI_SERIAL) 13 14 typedef struct _CPUINFO 15 { 16 unsigned int nMask; 17 char szVendor[ 16 ]; 18 char szBrand[ 52 ]; 19 char szSerial[ 32 ]; 20

Does memmove actually “move” a chunk of memory and leave behind zeros at the source? [duplicate]

流过昼夜 提交于 2020-01-15 03:26:49
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: memcpy vs memmove Does memmove actually "move" a chunk of memory? If so, does it leave the memory with zeros? Or, is it just like memcpy? I am looking at the man page, and I do not believe my assumption is correct. If I want to move a chunk of memory using memmove, would I have to manually zero out the chunk of memory where I did the move? 回答1: memmove has nothing to do with clearing the old memory, and in fact

Can I memcpy() any type which has a trivial destructor?

孤街浪徒 提交于 2020-01-12 14:11:31
问题 I do realize is_pod is a sufficient condition for a type to be memcpy -able, but is has_trivial_destructor also sufficient for this purpose? If not, why? 回答1: No. The requirement is that the type be trivially copyable (§3.9/2) which has a few more requirements, like the lack of a non-trivial copy constructor (§9/6). A trivially copyable class is a class that: — has no non-trivial copy constructors (12.8), — has no non-trivial move constructors (12.8), — has no non-trivial copy assignment

Can I memcpy() any type which has a trivial destructor?

自古美人都是妖i 提交于 2020-01-12 14:08:08
问题 I do realize is_pod is a sufficient condition for a type to be memcpy -able, but is has_trivial_destructor also sufficient for this purpose? If not, why? 回答1: No. The requirement is that the type be trivially copyable (§3.9/2) which has a few more requirements, like the lack of a non-trivial copy constructor (§9/6). A trivially copyable class is a class that: — has no non-trivial copy constructors (12.8), — has no non-trivial move constructors (12.8), — has no non-trivial copy assignment

Linux字符串函数集

戏子无情 提交于 2020-01-11 22:31:51
//Linux字符串函数集: 头文件:string.h   函数名: strstr   函数原型: extern char *strstr( char *str1, char *str2);   功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。   返回值:返回该位置的指针,如找不到,返回空指针。 包含文件:string.h   函数名: strstr   函数原型:extern char *strstr(char *str1, char *str2);   功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。   返回值:返回该位置的指针,如找不到,返回空指针。 原型: extern char *strchr( const char *s, char c);    const char *strchr( const char* _Str, int _Val)    char *strchr( char* _Str, int _Ch)   头文件:#include <string.h>   功能:查找字符串s中首次出现字符c的位置   说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。   返回值:Returns the address of the first occurrence of

Dereferencing function pointers in C to access CODE memory

谁都会走 提交于 2020-01-11 02:16:33
问题 We are dealing with C here. I'm just had this idea, wondering if it is possible to access the point in memory where a function is stored, say foo and copying the contents of the function to another point in memory. Specifically, I'm trying to get the following to work: #include <stdlib.h> #include <stdio.h> #include <string.h> void foo(){ printf("Hello World"); } int main(){ void (*bar)(void) = malloc(sizeof foo); memcpy(&bar, &foo, sizeof foo); bar(); return 0; } But running it gives a bus