Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么RealCube()在编译C++时神秘地表现不同?_C++_C_Realloc - Fatal编程技术网

为什么RealCube()在编译C++时神秘地表现不同?

为什么RealCube()在编译C++时神秘地表现不同?,c++,c,realloc,C++,C,Realloc,我以前在C程序中多次使用过以下函数: /** Splits a given string into an array of strings using given delimiters. @param input_string The string to be split. @param delimiters The characters that will be used as delimiters. @return

我以前在C程序中多次使用过以下函数:

/**
    Splits a given string into an array of strings using given delimiters.
    @param input_string
        The string to be split.
    @param delimiters
        The characters that will be used as delimiters.
    @return
        The components of the split string followed by @c NULL , or only
        @c NULL if sufficient memory fails to allocate for the returned array.
 */
char **split(char *input_string, const char *delimiters) {
    char **components = NULL;
    int components_count = 0;

    char *component = strtok(input_string, delimiters);
    while (component) {
        ++components_count;

        // |components| is reallocated to accomodate |component|. If
        // |realloc()| fails, |NULL| is returned.
        components = realloc(components, sizeof(char *) * components_count);
        if (!components) return NULL;
        components[components_count - 1] = component;

        component = strtok(NULL, delimiters);
    }

    // |components| is reallocated once more to accomodate an additional
    // |NULL|. Only |NULL| is returned in |realloc()| fails.
    components = realloc(components, sizeof(char *) * (components_count + 1));
    if (!components) {
        return NULL;
    }
    components[components_count] = NULL;

    return components;
}
<>我最近刚刚把函数添加到C++项目中,以便在需要处理C字符串的情况下使用。编译时,我现在遇到以下错误:

error: assigning to 'char **' from incompatible type 'void *'
        components = realloc(components, sizeof(char *) * components_count);

error: assigning to 'char **' from incompatible type 'void *'
    components = realloc(components, sizeof(char *) * (components_count + 1));
我完全不知道如何处理这些错误。就我而言,我所做的应该是C++中的合法的,因为它在C中总是很好用的。 如果有帮助的话,我将在OS X上使用clang++作为编译器,但这段代码也将在Ubuntu上使用g++进行编译。

C++不允许将void*指针隐式转换为其他指针。C和C++是完全不同的语言,所以C语言的所有工作都不依赖于C++,因为它是一种不同的语言。 但是,显式转换realloc结果应该可以工作。

C++不允许将void*指针隐式转换为其他指针。C和C++是完全不同的语言,所以C语言的所有工作都不依赖于C++,因为它是一种不同的语言。

明确地铸造ReAloc结果应该是有效的。

< P>不是所有的C和C++都是相同的;malloc和realloc就是一个常见的例子

您不必在C中显式地强制转换void指针,它将自动完成,如您的示例所示。 在MaloC和RealLoc函数中,您必须明确地在C++中显式地投示该指针。 这两种语言之间有很大的差异,不要想当然

<> P>在此,说明C与C++之间基本的一些区别;也许值得一读

或者通过评论建议:


不是所有的东西都在C和C++上是相同的;malloc和realloc就是一个常见的例子

您不必在C中显式地强制转换void指针,它将自动完成,如您的示例所示。 在MaloC和RealLoc函数中,您必须明确地在C++中显式地投示该指针。 这两种语言之间有很大的差异,不要想当然

<> P>在此,说明C与C++之间基本的一些区别;也许值得一读

或者通过评论建议:


<>将C代码与C++代码隔离。他们是不同的语言。处理C好像它可以运行在C++中,和处理传统的汉语一样有意义,就像它可以用简体中文一样。如果你正在编写一个用C++编译器编译的程序,那么它可能看起来和你编写的用C编译器编译的程序非常不同。 但是,有时你可能想链接一些C代码或汇编代码,或者任何C++代码。如果您使用的编译器具有兼容的ABI,那么每种语言的过程可能都是类似的。我将使用的示例是gcc和g++

使用C编译器(如gcc)编译C代码,但不使用链接过程(如使用-C标志)。例如:gcc-csplit.c


<>使用C++编译器编译C++代码,并将C编译器生成的目标代码链接到其中。例如:g++Me.cpp.Op/P>< P>将C代码与C++代码隔离。他们是不同的语言。处理C好像它可以运行在C++中,和处理传统的汉语一样有意义,就像它可以用简体中文一样。如果你正在编写一个用C++编译器编译的程序,那么它可能看起来和你编写的用C编译器编译的程序非常不同。 但是,有时你可能想链接一些C代码或汇编代码,或者任何C++代码。如果您使用的编译器具有兼容的ABI,那么每种语言的过程可能都是类似的。我将使用的示例是gcc和g++

使用C编译器(如gcc)编译C代码,但不使用链接过程(如使用-C标志)。例如:gcc-csplit.c


<>使用C++编译器编译C++代码,并将C编译器生成的目标代码链接到其中。例如:g++main.cpp split.o

很高兴有这么简单的解决方案。在每次realloc调用之前添加char**,一切正常。谢谢你的有用链接。那个网站一点也不准确!最后一点是完全错误的。C++的所有三个C标准都包含了一些东西:确实,C和C++是完全不同的语言,尽管它们通常具有兼容的ABIs,如汇编、D等。如果你正在寻找一个有效的链接来支持该语句,那么它也值得一读。很高兴这有一个简单的解决方案。在每次realloc调用之前添加char**,一切正常。谢谢你的有用链接。那个网站一点也不准确!最后一点是完全错误的。所有三个C标准都包含了与这句话大致相同的内容:确实如此
C和C++是完全不同的语言,尽管它们通常具有兼容的ABIs,如汇编、D等。如果你正在寻找一个有效的链接来支持该语句,那么它也值得阅读……评论不用于扩展讨论;此对话已结束。评论不用于扩展讨论;这段对话已经结束。