小编Dhw*_*nit的帖子

包含字符串值的struct在使用动态内存分配创建后,在分配时会导致分段错误

编译器在以下代码上抛出运行时段错误:

#include <iostream>
#include <string>
using namespace std;

struct Node{
  int data;
  void *next;   
  string nodeType;
};

Node* initNode(int data){
  Node *n = (Node*)malloc(sizeof(Node));
  n->data = data;
  n->next = NULL;
  n->nodeType = "Node";   //if this line is commented it works else segfault
  return n;
}

int main() {
  Node *n1 = initNode(10);
  cout << n1->data << endl;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么字符串赋值在动态分配的结构中不起作用,在静态分配的情况下,为什么它有效?

它的工作方式如下:

Node initNode(string data){
  Node n;
  n.data = data;  //This works for node creation statically
  n.next = NULL;
  n.nodeType = "Node"; …
Run Code Online (Sandbox Code Playgroud)

c++ string struct memory-management dynamic-memory-allocation

7
推荐指数
1
解决办法
2572
查看次数

构建失败 - Apache Parquet-MR源(mvn安装失败)

尝试执行"mvn clean install"来构建来自https://github.com/apache/parquet-mr的 parquet-mr源时出现以下错误

[INFO] Storing buildScmBranch: UNKNOWN
[INFO] 
[INFO] --- maven-remote-resources-plugin:1.5:process (default) @ parquet-generator ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Apache Parquet MR ................................. SUCCESS [1.494s]
[INFO] Apache Parquet Generator .......................... FAILURE [0.064s]
[INFO] Apache Parquet Common ............................. SKIPPED
[INFO] Apache Parquet Encodings .......................... SKIPPED
[INFO] Apache Parquet Column ............................. SKIPPED
[INFO] Apache Parquet Jackson ............................ SKIPPED
[INFO] Apache Parquet Hadoop ............................. SKIPPED
[INFO] Apache Parquet Avro ............................... SKIPPED
[INFO] Apache Parquet Benchmarks ......................... SKIPPED
[INFO] …
Run Code Online (Sandbox Code Playgroud)

apache velocity maven-3 maven parquet

7
推荐指数
2
解决办法
1723
查看次数

在Eclipse中启动gdb调试之前如何运行linux脚本

如何在Eclipse中启动gdb调试之前给命令运行?

实际上,我想执行一些脚本来设置环境变量(导出变量)并执行其他程序,然后再从Eclipse启动gdb进程来调试程序。

我尝试在调试器选项卡选项中执行以下操作:

<command> && <path-to-gdb-executable> 
Run Code Online (Sandbox Code Playgroud)

但是我得到了eclipse无法执行上述声明中给出的gdb的错误。请帮忙-我实际上想在gdb启动调试之前执行一个名为“ before-launch-commands.sh”的脚本。我正在尝试在eclipse kepler下执行cpp程序。

谢谢。

c++ eclipse linux debugging gdb

3
推荐指数
1
解决办法
3743
查看次数

SVG-如何填充侧面由贝塞尔曲线形成的多边形?

我想使用纯SVG创建以下内容,但不使用 D3和弦或类似功能。

在此处输入图片说明

我可以使用标准路径元素在SVG中使用贝塞尔曲线创建多边形。但是不知道如何用自定义颜色填充该多边形。如何定义此多边形的边界并设置fill属性?

有什么想法可以帮助分组吗?

html javascript svg d3.js

3
推荐指数
1
解决办法
1621
查看次数

POST 文件(作为二进制流)使用 java.net.HttpURLConnection 作为参数 file=&lt;file binary stream&gt;

我正在尝试使用 java.net.HttpURLConnection 将文件上传(POST)到端点,但我不断收到 http 代码 400(错误请求)。

在 android API 23 中提到了使用 HttpURLConnection 将文件和参数发送到服务器

但问题是我需要将此文件作为请求正文参数(文件=)发送。

注意:这些文件只有很小的尺寸(4-5mb),所以我完全在内存中读取它。

对应的 curl 请求是:

curl -X POST "API" -H "Content-Type: multipart/form-data" -F "file="


我正在使用的代码摘录:

    Proxy webproxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(" 
                                <proxy host>", <proxy_port>));
    HttpURLConnection http_conn = (HttpURLConnection) 
                                     url.openConnection(webproxy);
    String authorization = getAuthorization(access_token);
    http_conn.setRequestMethod("POST");
    http_conn.setRequestProperty("Accept-Charset", "UTF-8");        
    http_conn.setRequestProperty("Authorization", authorization);
    http_conn.setRequestProperty("Connection", "Keep-Alive");
    http_conn.setRequestProperty("Content-Type", "multipart/form-data);  
    http_conn.setDoOutput(true);
    http_conn.setDoInput(true);
    DataOutputStream outputStream;
    outputStream = new DataOutputStream(http_conn.getOutputStream());
    File file_obj = new File(this.file);                                
    byte[] allBytes = new byte[(int) file_obj.length()];
    FileInputStream fileInputStream …
Run Code Online (Sandbox Code Playgroud)

java multipartform-data httpurlconnection sophoslabs-intelix

3
推荐指数
1
解决办法
2019
查看次数