小编pos*_*444的帖子

结构初始化和新运算符

我在C#中有两个相似的结构,每个结构都包含一个整数,但后者有实现的get/set访问器.

为什么在分配字段之前必须Y使用new运算符初始化struct a?是y仍然当我初始化它的值类型new

public struct X
{
    public int a;
}

public struct Y
{
    public int a { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        X x;
        x.a = 1;

        Y y;
        y.a = 2; // << compile error "unused local variable" here

        Y y2 = new Y();
        y2.a = 3;
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# struct

21
推荐指数
3
解决办法
939
查看次数

OS X 上的 vsprintf:EXC_BAD_ACCESS

我在 OSX 上发现了 vsprintf 的奇怪行为。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#if defined(WIN32)
#include <windows.h>
#define VSNPRINTF _vsnprintf
#elif defined(LINUX) || defined (DARWIN)
#define VSNPRINTF vsnprintf
#include <sys/types.h>
#include <unistd.h>
#endif

char *f(const char *fmt, ...)
{
 char *out = NULL;
 const int step = 32;
 int n = -1, lout = step;
 va_list arg;

 if(fmt!=NULL)
 {
  va_start(arg, fmt);
  do
  {
   if(!out)
   {
    free(out);
    out = NULL;
   }
   out = (char*)malloc(lout + 1);
   if(!out) break;
   memset(out, 0, …
Run Code Online (Sandbox Code Playgroud)

c macos segmentation-fault

2
推荐指数
1
解决办法
741
查看次数

标签 统计

.net ×1

c ×1

c# ×1

macos ×1

segmentation-fault ×1

struct ×1