小编sys*_*ult的帖子

C中的数组索引作为长整数给出了分段错误

以下C代码给出了分段错误:

#include <stdio.h>
#include <stdint.h>

int main(){
        uint32_t *a;
        uint32_t idx=1233245613;
        a[idx]=1233;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何在C中使用uint32_t作为数组的索引?或者我如何使用类似数组的结构,它可以将uint32_t和12位数作为索引?

我很感激任何帮助.

c pointers segmentation-fault

4
推荐指数
1
解决办法
6712
查看次数

Mysql 列的行以另一行的值开头

我有一张如下表:

 MyTable
 --------
  A
  B
  C
Run Code Online (Sandbox Code Playgroud)

A,B,C 是列,MyTable 是表名,我想运行一个 mysql 查询,如:

SELECT MT1.A, MT2.A, MT2.B FROM MyTable MT1, MyTable MT2
       WHERE MT1.B<>MT2.B and MT2.B like "MT1.B%" and MT2.status=0;
Run Code Online (Sandbox Code Playgroud)

正如您从上面的查询中看到的,我有一个表,我想找到以另一个行值开头并与条件匹配的列。但是上面的查询显然失败了,因为 mysql 将“MT1.B%”作为字符串,我如何用 Mysql 实现这一点?

mysql recursive-query

4
推荐指数
1
解决办法
2166
查看次数

在结构体内定义宏的逻辑是什么?

正如标题中所显示的,我正在质疑在结构中定义宏的原因.我经常在网络编程中看到这种方法,例如以下片段:

struct sniff_tcp {
    u_short th_sport;               /* source port */
    u_short th_dport;               /* destination port */
    tcp_seq th_seq;                 /* sequence number */
    tcp_seq th_ack;                 /* acknowledgement number */
    u_char  th_offx2;               /* data offset, rsvd */
#define TH_OFF(th)      (((th)->th_offx2 & 0xf0) >> 4)
    u_char  th_flags;
    #define TH_FIN  0x01
    #define TH_SYN  0x02
    #define TH_RST  0x04
    #define TH_PUSH 0x08
    #define TH_ACK  0x10
    #define TH_URG  0x20
    #define TH_ECE  0x40
    #define TH_CWR  0x80
    #define TH_FLAGS        (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
    u_short th_win;                 /* window */
    u_short th_sum;                 /* checksum …
Run Code Online (Sandbox Code Playgroud)

c macros struct

4
推荐指数
1
解决办法
3198
查看次数

是否可以使用STL复制功能与地图

我想知道是否有任何技巧可以使用地图复制将地图内容复制到数组中.因为STL映射是通过键值和映射值的组合,所以映射的元素形成键值对.这阻止我们使用标准算法,如std :: copy.例如,以下代码给出错误:

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

int
main()
{
  std::map <int, double> test(4);
  test[0] = 11;
  test[2] = 1.23;
  test[3] = 23.29;
  test[1] = 12.12;
  double *test_arr = (double *) malloc(4 * sizeof(double));
  std::copy(test.begin(), test.end(), test_arr);
  std::cout << test_arr[3] << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

stl_copy_tests.cpp: In function ‘int main()’:
stl_copy_tests.cpp:9:32: error: no matching function for call to ‘std::map<int, double>::map(int)’
/usr/include/c++/4.5/bits/stl_map.h:170:7: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = …
Run Code Online (Sandbox Code Playgroud)

c++ stl copy map

4
推荐指数
1
解决办法
2233
查看次数

GLib哈希表循环问题

我将在C程序中使用GLib的Hash表实现,现在我只是在尝试它.我编写了以下代码用于测试:

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

 int main(){
 // Some codes and declerations here
 GHashTable *g_hash_table;
 uint32_t *a;
 a=(uint32_t *)malloc(sizeof(uint32_t));
 if(a==NULL){
    printf("Not Enough Mem For a\n");
    return 1;
 }
 *a=1123231;

 uint32_t* key;
 key=(uint32_t *)malloc(sizeof(uint32_t));
 if(key==NULL){
     printf("Not Enough Mem For key\n");
     return 1;
 }
 *key=122312312;
 int i;
 g_hash_table=g_hash_table_new(g_int_hash, g_int_equal);
 for(i=0;i<TABLE_SIZE;i++){
     *key+=1;
     *a+=1;
     g_hash_table_insert(g_hash_table,(gpointer)key,(gpointer)a);
     uint32_t *x=(uint32_t *)g_hash_table_lookup(g_hash_table,key);
     printf("Counter:%d,  %u\n",i,*x);
 }

GHashTableIter iter;
g_hash_table_iter_init(&iter,g_hash_table);
int size=g_hash_table_size(g_hash_table);
printf("First size: %d\n",size);
uint32_t *val;
uint32_t *key_;
int counter=0;

// …
Run Code Online (Sandbox Code Playgroud)

c hashtable glib

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

指向嵌套结构中的struct的指针

我正在尝试运行以下代码(在fedora 11 i586上的gcc 4.3中):

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

struct s_smallstruct{
  int smallstruct;
};                      

struct s_test2{
        char * test2;
        struct s_smallstruct* smallstruct;
};

struct s_test3{
        char * test3;
        struct s_smallstruct * smallstruct;
};

struct s_test1{
        char * test1;
        struct s_test2 * test2;
        struct s_test3 * test3;
};


int main(){
        struct s_test1 *test1 = (struct s_test1 *) malloc( sizeof test1 );
        test1->test2[0].smallstruct[0].smallstruct = 123;
        int num = test1->test2[0].smallstruct[0].smallstruct;
//      struct s_smallstruct * smallstruct = (struct s_smallstruct *) malloc( sizeof smallstruct …
Run Code Online (Sandbox Code Playgroud)

c struct pointers

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

矢量矢量的初始化非常慢

我有一个向量的向量,我试图初始化它如下:

vector<vector<float> > matrix(numberOfRows, vector<float> (numberOfCols));
Run Code Online (Sandbox Code Playgroud)

但这行约为0.89,其中numberOfRows和numberOfCols为:

const uint32_t numRows = 10000;
const uint32_t numCols = 20000;
Run Code Online (Sandbox Code Playgroud)

而使用以下代码初始化动态数组需要0.04秒来执行:

float **matrix = new float*[numberOfRows];
for (size_t i = 0; i < numberOfRows; ++i)
    matrix[i] = new float[numberOfCols];
Run Code Online (Sandbox Code Playgroud)

我做错了有没有更快的方法来初始化该向量?

编辑:

至于问题:

我使用g ++ - 4.5来测试使用默认标准的O3优化级别.

c++ performance initialization vector

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

检查网站是否在Perl或C中运行的最快方法是什么?

我正在尝试检查网站是否已启动并正在运行.我目前正在使用Perl中的UserAgent库执行此操作,超时1.然而,对我来说它仍然太慢.

我每隔五分钟从cron打电话给脚本.有许多链接需要检查,脚本需要五分钟以上才能完成执行.所以,我需要一种更有效的方法来做到这一点.它甚至可以是C中的解决方案.

c unix perl

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

如何在我的代码中使用Git的malloc包装器?

我想在代码中使用git的malloc和realloc包装器来处理OOM(内存不足)的情况.这是它的代码:

void *xmalloc(size_t size)
{
      void *ret = malloc(size);
      if (!ret && !size)
              ret = malloc(1);
      if (!ret) {
              release_pack_memory(size, -1);
              ret = malloc(size);
              if (!ret && !size)
                      ret = malloc(1);
              if (!ret)
                      die("Out of memory, malloc failed");
      }
#ifdef XMALLOC_POISON
      memset(ret, 0xA5, size);
#endif
      return ret;
}
Run Code Online (Sandbox Code Playgroud)

但是release_pack_memory函数在sha1_file.c头文件中,这个函数引用了Git代码中其他头文件中的函数,我不想花费太多精力从Git的代码库中隔离这个函数.目前我正在为release_pack_memory函数寻找替代函数,或者你能推荐我另一种选择.我会感激任何帮助

c git malloc

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