cstring

How does std::string::c_str() return a c-string that does not cause a memory leak or undefined c-string contents?

蓝咒 提交于 2019-12-10 13:34:49
问题 I'm writing a string class that is similar to std::string for a homework assignment, but I cannot figure out how to return a c-string that does not cause a memory leak and is guaranteed to stay the same until it is no longer in use. I currently have: const char* string::c_str() { char c[_size+1]; strncpy(c,_data,_size); c[_size]='\0'; return c; } but the contents are overridden shortly after it is called. If I do dynamic allocation, I'll have either a memory leak or only one c-string can

2018, XI Samara Regional Intercollegiate Programming Contest 解题报告

£可爱£侵袭症+ 提交于 2019-12-10 06:41:34
2018, XI Samara Regional Intercollegiate Programming Contest 传送门 A. Restoring Numbers 题意: 给出两个数 a a a 和 b b b 的和 s s s 和最大公因数 g g g ,要你求出任意一组 a a a 和 b b b 的解。 题解: 我们可以知道 g c d ( a , b ) = g gcd(a,b)=g g c d ( a , b ) = g ,则我们假设 a = p ∗ g a=p*g a = p ∗ g , b = q ∗ g b=q*g b = q ∗ g ,那么 s = ( p + q ) ∗ g s=(p+q)*g s = ( p + q ) ∗ g ,由此可得有解的 第一个 条件是 s % g = 0 s\%g=0 s % g = 0 。 至于为什么是第一个,是因为题目描述 a a a 和 b b b 是正数,所以上设的 p p p 和 q q q 要大于 0 0 0 ,所以有解的 第二个 条件是 s / g > = 2 s/g>=2 s / g > = 2 ,此时我们令 p = 1 p=1 p = 1 , q = s / g − 1 q=s/g-1 q = s / g − 1 ,就得到了 p p p 和 q q q 的一组解,同时就得到了 a a a 和 b b b

how to convert CString to Bytes

断了今生、忘了曾经 提交于 2019-12-09 07:08:24
i am actually tryin to convert a csharp code to c... below is the C# code.. CString data = "world is beautiful"; Byte[] quote = ASCIIEncoding.UTF8.GetBytes(data); in the above code... it converts the string into bytes..similarily is ther a way that i can convert it using C.. Can any body tell what wud be the quivalent code in C? Please help me guys Well CString is a C++ class so doing it in C is a little unlikely. But if you wish to get it as a standard multi-byte encoded string then you can do the following CString data = "world is beautiful"; CStringA mbStr = data; char* bytes = mbStr

C++ CString equivalent in C#

不打扰是莪最后的温柔 提交于 2019-12-08 17:43:50
问题 What is the C# equivalent for MFC's CString ? 回答1: Probably System.String . But in an effort to provide more useful information: System.String instances are immutable. Concatenation/substring/etc actually create new string objects, so using a string instance as a buffer for building up output is a really bad idea, in case you were going to do that. Think of System.String as a const CString . System.Text.StringBuilder is used to build up and manipulate string content. It has a .ToString()

Need to delete CString after use to free memory?

喜你入骨 提交于 2019-12-08 03:10:07
问题 If I am using a CString like this: void myFunc(char *str) { CString s(str); // Manipulate other data with CString // ... // Finished // Should I somehow delete 's' here to avoid a memory leak? } Is the string erased once the function goes out of scope? Also, I know that the new keyword allocates memory, if I construct an object without the new keyword, is memory still allocated? My intuition tells me yes, but I would like to verify. e.g. CString *asdf = new CString("ASDF"); // same as?

Error: Conversion to non-scalar type

≡放荡痞女 提交于 2019-12-08 02:49:27
问题 I am making a set of derived classes for an assignment. I am instructed to use char arrays (c-strings). When I compile I keep getting the error: Homework11.cpp: In function âint main()â: Homework11.cpp:72: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested Homework11.cpp:73: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested Homework11.cpp:74: error: conversion from âchar [10]â to non-scalar type âAccountâ requested Homework11.cpp:75: error:

Writing into c-string

倾然丶 夕夏残阳落幕 提交于 2019-12-07 16:31:22
问题 my code segfaults and I don't know why. 1 #include <stdio.h> 2 3 void overwrite(char str[], char x) { 4 int i; 5 for (i = 0; str[i] != '\0'; i++) 6 str[i] = x; 7 } 8 9 int main(void) { 10 char *s = "abcde"; 11 char x = 'X'; 12 overwrite(s, x); 13 printf("%s\n", s); 14 return 0; 15 } The gdb debugger tells me, that problem is on the line 6, where I want to store a char, into c-string (if I use lvalue pointer dereferencing, it's the same problem.) This is what he says: (gdb) run Starting

CMyListCtrl

谁说胖子不能爱 提交于 2019-12-07 15:17:35
1. MyListCtrl.h #pragma once #include <vector> #include "resource.h" // CMyListCtrl #ifdef DLL_IMPLEMENT #define DLL_API __declspec(dllexport) #else #define DLL_API __declspec(dllimport) #endif typedef unsigned int UINT; #define ID_LIST_SET_FONT 1001 #define ID_LIST_SAVE_ALL 1002 class DLL_API CMyListCtrl : public CListCtrl { struct SColumn { UINT index, order, width; TCHAR name[128]; }; std::vector<SColumn> m_vecColumn; COLORREF m_clrFore, m_clrBack; CFont m_font; CString m_strCfgFileName; int m_nSortCol;//排序列的索引,-1表示无排序列 bool m_bAscent;//true:升序 false:降序 CImageList m_imgList;//列表头的排序图标

Shall we treat BSTR type in COM as value or reference?

家住魔仙堡 提交于 2019-12-07 05:26:54
问题 From book ATL Internals , I knew BSTR is different from OLECHAR*, and there are CComBSTR and CString for BSTR. According MSDN Allocating and Releasing Memory for a BSTR, I knew memory management responsibility for caller/callee. Take this line from MSDN, HRESULT CMyWebBrowser::put_StatusText(BSTR bstr) I still do not know how to handle bstr properly in my implementation. Since I still have a basic question for BSTR -- should we treat bstr as a value (like int) or as a reference (like int*),

Sqlite时间段查询中遇到的问题

天大地大妈咪最大 提交于 2019-12-07 02:54:30
问题: 我要查询DateTime时间其中的一段符合时间要求的数据,比如我要查询‘2019-06-06 16:50:00’至‘2019-06-06 16:59:00’这一段的数据 开始用这段代码 strSql= ("select * from CollectTableRtd where datetime(DateTime)>=datetime('2019-06-06 16:50:00') and datetime(DateTime)<=datetime('2019-06-06 16:59:00')"); 是可以查询出来的,然后换下面代码: strSql= ("select * from CollectTableRtd where datetime(DateTime)>=datetime('datetime_strf1') and datetime(DateTime)<=datetime('datetime_strf2')"); 其中datetime_strf1、datetime_strf2是从控件读取的选择时间,然而并没有正确读取,下面也一样: strSql= ("select * from CollectTableRtd where datetime(DateTime)>=datetime(datetime_strf1) and datetime(DateTime)<