小编pol*_*nux的帖子

c sendfile第二次不起作用

这是LIST ftp的cmd的代码段:

count = file_list("./", &files);
if((fp_list = fopen("listfiles.txt", "w")) == NULL){
  perror("Impossibile aprire il file per la scrittura LIST");
  onexit(newsockd, sockd, 0, 2);
}
for(i=0; i < count; i++){
  if(strcmp(files[i], "DIR ..") == 0 || strcmp(files[i], "DIR .") == 0) continue;
  else{
    fprintf(fp_list, "%s\n", files[i]);
  }
}
fclose(fp_list);
if((fpl = open("listfiles.txt", O_RDONLY)) < 0){
  perror("open file with open");
  onexit(newsockd, sockd, 0, 2);
  exit(1);
}
if(fstat(fpl, &fileStat) < 0){
  perror("Errore fstat");
  onexit(newsockd, sockd, fpl, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, …
Run Code Online (Sandbox Code Playgroud)

c sockets sendfile

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

如何复制内存

说我有:

unsigned char *varA, *varB, *varC;
varA=malloc(64);
varB=malloc(32);
varC=malloc(32);
Run Code Online (Sandbox Code Playgroud)

如何将varA 的 32个字节放入varB,将varA的最后 32个字节放入varC?

c pointers

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

在C中的字符串反转期间释放指针时的段错误

int main(int argc, char **argv){
    char *str = argv[1];
    size_t len = strlen(str);
    char *dst = calloc(len+1, sizeof(char));
    int i;
    for(i=len-1; i>=0; i--){
        memcpy(dst, str+i, 1);
        dst++;
    }
    *(++dst) = '\0';
    printf("%s\n", dst);
    free(dst);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

省略错误检查以获得更多可读性
我不明白为什么我得到了一条带有此消息的段错误:"glibc free():无效指针:"
我正在尝试编写一个使用指针反转字符串的小程序但是:
1) last printf什么都不打印;
2)我遇到了段错误

c string segmentation-fault

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

c基于fstat的动态数组大小

我使用fstat来获取文件大小.我想使用此大小声明一个数组,然后用另一个fstat更改大小并重新使用相同的数组.例:

fstat(file1, &fileStat);
fsize = filestat.st_size;
char filebuffer[size-of-file1];
/* do something */
fstat(file2, &fileStat);
fsize = filestat.st_size;
char filebuffer[size-of-file2];
/* do something */
Run Code Online (Sandbox Code Playgroud)

显然我不能重新声明filebuffer数组,我必须声明一个新的.但是,如果我想重新使用不同大小的相同数组,我该怎么办?
谢谢!!

编辑:

filebuffer = malloc(fsize);
if(filebuffer == NULL){
    perror("malloc");
    onexit(sockd, 0, fd, 4);
}
Run Code Online (Sandbox Code Playgroud)

tmpfilebuf = realloc(filebuffer, fsize);
if(tmpfilebuf){
    filebuffer = tmpfilebuf;
}
else{
    perror("realloc");
    free(filebuffer);
    onexit(sockd, 0, fd, 4);
}
Run Code Online (Sandbox Code Playgroud)

但现在我得到了一个段错误:(

c arrays

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

c使用malloc分配内存

我有这些变种:

uint32_t fsize;
char *filebuffer = NULL;
fsize = fileStat.st_size;
Run Code Online (Sandbox Code Playgroud)

如果我想要malloc正确的内存我必须编写的代码是这样的:

malloc(fsize);
Run Code Online (Sandbox Code Playgroud)

或这个:

malloc(fsize * sizeof(char *));
Run Code Online (Sandbox Code Playgroud)

谢谢!

c malloc

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

为什么char指针会被自动清除?

这是我的代码:

tmpip = get_public_ip();
pubip = strtok(tmpip, "\n");
sprintf(buffer, "220 FTPUtils Server [%s]", pubip);
len_string = strlen(buffer)+1;
if(send(newsockd, &len_string, sizeof(len_string), 0) < 0){
    perror("Errore invio len buffer");
    onexit(newsockd, sockd, 0, 2);
}
if(send(newsockd, buffer, len_string, 0) < 0){
    perror("Errore durante l'invio");
    onexit(newsockd, sockd, 0, 2);
}
pubip = NULL;
free(tmpip);
memset(buffer, 0, sizeof(buffer));


if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
    perror("Errore ricezione len user buffer");
    onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
    perror("Errore ricezione del nome utente"); …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

c返回int值不起作用

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

int num_mezzo_1(int num_orig);
int num_mezzo_2(int num_orig);

int main(int argc, char *argv[]){
    int num,contatore,tmp=0,tmp_1,tmp_2;
    num=atoi(argv[1]);
    if(num <= 3){
        printf("%d è primo\n", num);
        exit(0);
    }
    else{
        num_mezzo_1(num);
        num_mezzo_2(num);
        tmp=tmp_1+tmp_2;
            //using printf to debug
        printf("t1 %d, t2 %d\n", tmp_1,tmp_2);
        if(tmp>2){
            printf("%d NON è primo\n", num);
        }
        else{
            printf("%d è primo\n", num);
        }
    }
    exit(0);
}

int num_mezzo_1(int num_orig){
    int tmp_1=0,cont_1;
    for(cont_1=1; cont_1<=(num_orig/2); cont_1++){
        if((num_orig % cont_1) == 0){
            tmp_1++;
        }
    }
    //using printf to …
Run Code Online (Sandbox Code Playgroud)

c function return-value

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

如何在C中从/向不同的arch运行代码?

我开发了一个小型(非常小)的ftp客户端和服务器.
这两个程序只有在使用相同的arch时才能编译和运行.

  1. 如果客户端是Ubuntu amd64而服务器是Ubuntu i386我得到了一个段错误(我认为这是一个指针问题)
  2. 如果客户端是Ubuntu amd64而服务器是Ubuntu amd64都可以
  3. 如果客户端是Ubuntu i386而服务器是Ubuntu i386都可以

我怎样才能使这两个程序在不同的拱门上工作?
这是一个代码问题?如果是这样我必须做什么?
提前致谢!

c architecture

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