小编Col*_*Two的帖子

使用memcpy时出现分段错误

我有一个关于memcpy()的基本问题:

我有一个结构,其中有几个数组作为其成员.
我想将结构中的数据复制到缓冲区内存中(通过malloc()分配)

我看到了这个分段错误.我在这个实现中做错了吗?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 10

struct temp {
    int en;
    int one[MAX];
    int two[MAX];
 };

int main()
{
    struct temp *cpy;
    int *buffer, i;

    for(i=0; i<MAX; i++) {
            cpy->one[i] = i;
            cpy->two[i] = i * i;
     }

   buffer = malloc(3 * MAX * sizeof(int));
   memcpy(buffer, cpy, sizeof(struct temp));
}
Run Code Online (Sandbox Code Playgroud)

如何将完整数据复制到缓冲区?

c malloc memcpy

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

Lua中的print(0x1e1)输出是什么?为什么?

print(0x1e1)将在Lua打印481,但我不知道为什么.有人可以帮我理解吗?

lua

0
推荐指数
1
解决办法
78
查看次数

为什么通过cin的int8_t和用户输入显示奇怪的结果

一小段代码让我发疯,但希望你可以阻止我跳出窗外.看这里:

#include <iostream>
#include <cstdint>

int main()
{
    int8_t i = 65;
    int8_t j;

    std::cout << "i = " << i << std::endl; // the 'A' is ok, same as uchar

    std::cout << "Now type in a value for j (use 65 again): " << std::endl;
    std::cin >> j;
    std::cout << "j = " << j << std::endl;

    if (i != j) 
        std::cout << "What is going on here?????" << std::endl;
    else 
        std::cout << "Everything ok." << std::endl;

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ int uint8t

-1
推荐指数
1
解决办法
1389
查看次数

if(x是A){//使用x作为A

在C#中,如果对象属于某种类型,我有时必须做某事.

例如,

if (x is A)
{
    // do stuff but have to cast using (x as A)
}
Run Code Online (Sandbox Code Playgroud)

如果在if街区内,我们可以x像使用它一样真的很好A,因为它不可能是其他任何东西!

例如,

if (x is A)
{
    (x as A).foo(); // redundant and verbose
    x.foo();   // A contains a method called foo
}
Run Code Online (Sandbox Code Playgroud)

编译器是不是很聪明,不知道这个或是否有任何可能的技巧来获得类似的行为

Dlang能否有效地做类似的事情?

顺便说一句,我不是在寻找动态.只是尝试编写更简洁的代码.显然我可以做var y = x as A;y不是用X.

c# d

-1
推荐指数
1
解决办法
151
查看次数

多线程中的C++ Singleton

我是多线程编程的新手,但我正在研究其他人的大项目.在代码中,他有一个单例类,它有一些公共成员变量和一个成员互斥.他在不同的线程中使用这个单例,如:

singleton::instance()->mutex.lock();
singleton::instance()->value = getval();
singleton::instance()->mutex.release();
Run Code Online (Sandbox Code Playgroud)

这是安全的方法吗?如果不是在单例中读取/写入值的正确方法是什么?

c++ qt multithreading

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

这段代码出错了吗?

所以,我没有看到我的逻辑在这个问题上有什么缺陷.

它从左到右读取表达式,操作数是浮点数.

但是,我的程序陷入了循环.它读取并分配最终的数字,例如它将10.5分配给num1,但它永远不会退出循环.

int main(void)
{
    float num1, num2;
    char oper = 0;

    printf("Enter an expression: ");
    scanf("%f", &num1);

    while (oper != ('\n' || EOF))
    {
        oper = getchar();
        scanf("%f", &num2);

        switch (oper)
        {
            case '+':
            num1 += num2;
            break;

            case '-':
            num1 -= num2;
            break;

            case '*':
            num1 *= num2;
            break;

            case '/':
            num1 /= num2;
            break;
        }

    }

    printf("Value of Expression: %.2f", num1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

Enter an expression: 1+2.5*3

Value of expression: 10.5
Run Code Online (Sandbox Code Playgroud)

c

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

实例化一个类是什么意思?

这是标准:

  • nsLookup类使用一个字符串进行实例化,该字符串定义要查询的主机.
  • 构造函数使用此字符串实例化InetAddress对象.
  • 一种方法旨在解决查询查询.如果存在,查询可以返回多个IP地址.这些应作为String对象数组返回到GUI以供显示.

这是代码:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class NsLookup {

  private InetAddress inet = null;

  public void resolve(String host) {
    try {
      inet = InetAddress.getByName(host);

      System.out.println("Host name : " + inet.getHostName());
      System.out.println("IP Address: " + inet.getHostAddress());
    }
    catch (UnknownHostException e) { 
      e.printStackTrace(); 
    }
  }

  public static void main(String[] args) {
    NsLookup lookup = new NsLookup();
    lookup.resolve(args[0]);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果可能,请帮我举例?

java

-12
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×2

c++ ×2

c# ×1

d ×1

int ×1

java ×1

lua ×1

malloc ×1

memcpy ×1

multithreading ×1

qt ×1

uint8t ×1