小编sim*_*onc的帖子

转换语法练习

我有一个代码如下:

int main(void)
{
    char mychar = 'd';
    int *ptr = malloc(sizeof(*ptr)) ;

    *ptr = (char) 'c' ; // *ptr = (char*) 'c'; Gives the exact same result

    printf("%c\n",*ptr);
    memset(ptr,mychar,sizeof(*ptr));
    printf("%c\n",*ptr);
    free(ptr);
    printf("%c\n",*ptr);
    return 0 ;
}
Run Code Online (Sandbox Code Playgroud)

代码为指针样式转换和数据类型转换提供相同的结果.
应该使用哪两种风格中的哪一种或被认为是一种好的做法?为什么?

*ptr = (char) 'c' ; 
*ptr = (char*) 'c';
Run Code Online (Sandbox Code Playgroud)

c syntax pointers

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

C程序不断崩溃

我的程序一直在崩溃.这些代码似乎合法且正确.不知道有什么问题.

#include <stdio.h>

void queue(int length,int turns){
    int permutations,totalTurns;
    turns++;
    if (length>0){
        queue(length-1,turns);
        if (length>1){
            queue(length-2,turns);
        }
    } 
    else{
        permutations++;
        totalTurns+=turns;
    }
}

int main() 
{
    while(true){
        int length;
        float average;
        int permutations=0;
        int totalTurns=0;

        printf("Queue length: ");
        scanf("%d", &length);
        queue(length,-1);
        average=totalTurns/permutations;
        printf("The average turns are %f", average);
    }
}
Run Code Online (Sandbox Code Playgroud)

c crash

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

释放全局变量

假设我有一个包含大型结构的全局变量:

typedef struct {
    char Big[1024]
} LARGE;

static LARGE x;

void main()
{
     free(x);
}
Run Code Online (Sandbox Code Playgroud)

当我不再需要它时,我可以安全地从主电话免费拨打(x)吗?

c malloc c99

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

在其定义中创建一个实例

如何在其定义中创建类的实例?

class Test{
  Test _test;
};
Run Code Online (Sandbox Code Playgroud)

这引发了

error: field _test has incomplete type
Run Code Online (Sandbox Code Playgroud)

使用指针有效,但我想尽可能避免使用指针.

c++

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

calloc(),malloc()vs new-ing一个内部有unordered_map的结构

我正在创建一个结构,其中的字段是堆上的unordered_map.当我使用new时,我可以毫无问题地添加它.但是使用calloc,我得到一个插入错误,因为桶大小为0.我调用reserve后工作正常.

那么,当在结构上调用calloc时,unordered_map构造函数不会运行吗?我很困惑为什么如果它在一个新版本的结构中,它似乎具有非零桶大小.除了召唤预备队之外,还有更好的方法吗?(在这种情况下我不能使用删除,所以我需要坚持使用calloc调用)

c++ malloc memory-management calloc

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

为什么sendto函数需要第三个参数,在socket编程中

我是套接字程序的新手.学习sendto函数时,作为原型:

ssize_t sendto(int socket, const void *message, size_t length,
                int flags, const struct sockaddr *dest_addr,
                socklen_t dest_len);
Run Code Online (Sandbox Code Playgroud)

我知道"消息"包含目标IP,dest_addr参数还指定了目标IP地址.这个dest_addr论点还有其他用法吗?

c sockets sendto

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

memcpy调用中的内存故障

我打电话给以下时面对"内存故障":

extern void *memcpy (void *__restrict __dest,
         __const void *__restrict __src, size_t __n)
 __THROW __nonnull ((1, 2));
Run Code Online (Sandbox Code Playgroud)

这是一段代码:

    fprintf(stderr, "sysconfig line 440 \n");
fprintf(stderr, "Value size: %d ; Pointer mymsg: %p ; Pointer value: %p ; mymsg->mtext: %s ; value: %s ; size: %d ;   \n", strlen(value), mymsg, value, mymsg->mtext, value, size);
memcpy(mymsg->mtext, value, size);
fprintf(stderr, "sysconfig line 442 \n");
Run Code Online (Sandbox Code Playgroud)

mymsg是一个指向struct的指针:

MSG_T *mymsg;
Run Code Online (Sandbox Code Playgroud)

MSG_T:

typedef struct msgInfo {
int cmd;
int arg1;
int arg2;
char    mtext[MAX_SEND_SIZE];
} MSG_T;
Run Code Online (Sandbox Code Playgroud)

MAX_SEND_SIZE = …

c linux gcc uclibc

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

如何保存指针c ++的类型

可以使用任何类型T调用Function1,它将被转换为(void*)以便能够添加到列表中但是我丢失了原始指针类型(我需要在一个链表中存储tham,因为我不能为每个创建一个可能的类型).所以我需要保存指针的类型.我知道无法使用c ++完成.有谁能建议替代解决方案?

class MyClass
{

    template<class T>
    void function1(T* arg1)
    {
        myList.add((void*)arg);
    }

    void function2()
    {
        for(int i = 0; i < myList.size(); i++)
        {
            myList.get(i);
            //restore the original pointer type
        }
    }

    STLinkedlist<void*> myList;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

C :为什么只有一些文字字符串会导致我的程序崩溃?

我尝试了以下程序:

#include <stdio.h>

char s1[] = "Global String";
char *s2 = "Another Global String";

main()
{
    char s3[] = "Local String" ;
    char *s4 = "Another Local String" ;

    strcpy( s1, "New str" );
    strcpy( s2, "New str" ); // causes seg fault
    strcpy( s3, "New str" );
    strcpy( s4, "New str" ); // causes seg fault
}
Run Code Online (Sandbox Code Playgroud)

s2s4导致分段错误,大概是因为它们存储在只读数据段中。怎么会在文字字符串的指向s1s3不崩溃?这是在 Ubuntu 上。

奇怪的是,s1, s2,s3s4都可以修改,并且在 cygwin 上使用 gcc …

c string

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

mk的4k*4K浮点数导致分段错误

我正在从文件中读取浮点数矩阵.矩阵的尺寸为4k*4K.使用下面的程序,它只会导致now()函数奇怪地重置.如果我将矩阵大小减小到1k*1K,它不会重置.虽然它正确地读取浮点数,但最后几个值是垃圾.我不知道这些垃圾价值来自哪里.我采用BUFFSIZE 6的大小,因为浮动数字中的数字将大约为5-6.不确定它是否正确.

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>


#define ROWS 4000
#define COLS 4000
#define BUFFSIZE 6

//#define USE_FREAD
#define USE_MMAP

double now()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + tv.tv_usec / 1000000.;
}

int main()
{

    double end_time;
    double total_time;
    int i, x, y, k;
    for (k = 0; k < 1; k++)
    {
    double start_time = now();

    FILE* in = fopen("resistence_file", "rb"); …
Run Code Online (Sandbox Code Playgroud)

c++ linux mmap

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

标签 统计

c ×6

c++ ×4

linux ×2

malloc ×2

pointers ×2

c99 ×1

calloc ×1

crash ×1

gcc ×1

memory-management ×1

mmap ×1

sendto ×1

sockets ×1

string ×1

syntax ×1

uclibc ×1