Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.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/1/vb.net/16.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
分配内存(C)_C - Fatal编程技术网

分配内存(C)

分配内存(C),c,C,我在程序中创建了几个函数。在我的类“void dstring_truncate(dstring*destination,unsigned int truncatedLength);”中,我分配内存的方式错误。我应该如何正确分配内存?还有一个关于realloc和malloc的问题。他们之间有什么不同 头文件 #ifndef DSTRING_H #define DSTRING_H #include <stdio.h> typedef char* DString; /* Returns

我在程序中创建了几个函数。在我的类“void dstring_truncate(dstring*destination,unsigned int truncatedLength);”中,我分配内存的方式错误。我应该如何正确分配内存?还有一个关于realloc和malloc的问题。他们之间有什么不同

头文件

#ifndef DSTRING_H
#define DSTRING_H
#include <stdio.h>

typedef char* DString;

/* Returns a string that contains the same text as 'str'. The returned string is dynamicilly allocated */
DString dstring_initialize(const char* str);

/* Puts the original string with source */
int dstring_concatenate(DString* destination, DString source);

/* shortening *destination so it contains only truncatedLength number of sign. If 'truncatedLenght' is longer than string length nothing happens */
void dstring_truncate(DString* destination, unsigned int truncatedLength);

/* Writes a string to a textfile. 
  Textfile is supposedly open before and continiues to be opened after */
void dstring_print(DString stringToPrint, FILE* textfile);

/* frees the memory for a dynamic string and set the string(*stringToDelete) to NULL */
void dstring_delete(DString* stringToDelete);

#endif
#ifndef DSTRING#H
#定义DSTRING_H
#包括
typedef char*DString;
/*返回与“str”包含相同文本的字符串。返回的字符串是动态分配的*/
DString DString_初始化(const char*str);
/*将原始字符串与源字符串放在一起*/
int-dstring_串联(dstring*目的地,dstring源);
/*缩短*目的地,使其仅包含符号的截断长度编号。如果“TruncatedLength”长于字符串长度,则不会发生任何情况*/
void dstring_truncate(dstring*目的地,unsigned int truncatedLength);
/*将字符串写入文本文件。
文本文件应该在之前打开,之后继续打开*/
无效dstring_打印(dstring StringTopPrint,文件*textfile);
/*释放动态字符串的内存,并将字符串(*stringToDelete)设置为NULL*/
无效数据串删除(数据串*stringToDelete);
#恩迪夫
.C文件

#include "dstring.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>


DString dstring_initialize(const char* str)
{
    assert(str != NULL); // Precondition

    DString sameStr;

    sameStr = (char*) malloc(sizeof(char) * (strlen(str)+1)); // Allokerar en dynamisk sträng



    sameStr = strcpy(sameStr, str); // Kopierar innehållet i "str" till "sameStr"

    assert(*sameStr == *str); // Kollar om strängarna har samma innehåll

    return sameStr;
}

int dstring_concatenate(DString* destination, DString source)
{
    assert(*destination != NULL); // Avreferar och man kommer åt innehållet
    assert(destination != NULL); // Kollar så den inte är tom
    assert(source != NULL); // kollar så att den innehåller en sträng

    DString oneSent;

    oneSent = (char*) realloc(*destination, sizeof(char)*(strlen(*destination)+1) + (strlen(source)+1)); // Omallokerar för två strängar

    oneSent = strcat(*destination, source); // Sätter ihop *destination och source

    assert(oneSent == *destination && source); // Kollar om oneSent har samma innehåll som *destination och source

    return 1;
}

void dstring_truncate(DString* destination, unsigned int truncatedLength)
{
    assert(destination != NULL);
    assert(*destination != NULL);

    *destination = (char*) realloc(*destination, sizeof(char) * (strlen(truncatedLength) + 1)); // Omallokerar för en sträng
    *destination = "Department";

    assert(strlen(*destination) == truncatedLength); //kollar om längden är 10
}

void dstring_print(DString str, FILE* textfile)
{
    assert(textfile != NULL);

    fprintf(textfile, "%s", str); // textfile är en stdout som printar ut str

}

void dstring_delete(DString* stringToDelete)
{
    assert(stringToDelete != NULL); // Kollar om det finns något att frigöra

    *stringToDelete = NULL; // Tömmer innehållet i strängen

    free(*stringToDelete); // Frigör minnet

    assert(*stringToDelete == NULL);
}
#包括“dstring.h”
#包括
#包括
#包括
DString DString_初始化(常量字符*str)
{
断言(str!=NULL);//前提条件
DString sameStr;
sameStr=(char*)malloc(sizeof(char)*(strlen(str)+1));//动态分配
sameStr=strcpy(sameStr,str);//Kopierar innehållet i“str”到“sameStr”
assert(*sameStr==*str);//Kollar om strängarna har samma innehåll
返回sameStr;
}
int dstring_串联(dstring*目标,dstring源)
{
assert(*destination!=NULL);//Avreferar och man kommeråt innehållet
assert(destination!=NULL);//Kollar såden inteär tom
assert(source!=NULL);//kollar såatt den innehåller en sträng
DString-oneSent;
oneSent=(char*)realloc(*destination,sizeof(char)*(strlen(*destination)+1)+(strlen(source)+1));//Omallokerar för tvåsträngar
oneSent=strcat(*目的地,来源);//Sätter ihop*目的地och来源
assert(oneSent==*目的地和来源);//Kollar om oneSent har samma innehåll som*目的地och来源
返回1;
}
void dstring_truncate(dstring*目标,unsigned int truncatedLength)
{
断言(目标!=NULL);
断言(*目的地!=NULL);
*destination=(char*)realloc(*destination,sizeof(char)*(strlen(truncatedLength)+1));//Omallokerar för en sträng
*目的地=“部门”;
断言(strlen(*destination)=truncatedLength);//kollar om längdenär 10
}
无效数据字符串_打印(数据字符串str,文件*textfile)
{
断言(textfile!=NULL);
fprintf(textfile,“%s”,str);//textfileär en stdout som printar ut str
}
无效数据字符串\u删除(数据字符串*stringToDelete)
{
assert(stringToDelete!=NULL);//Kollar om det finns något att frigöra
*stringToDelete=NULL;//Tömmer innehållet i strängen
自由(*stringtodelite);//弗里格·米内特
断言(*stringToDelete==NULL);
}
测试文件

#include <assert.h>
#include <string.h>

#include "dstring.h"


int main(void)
{
    DString str1, str2;
    str1 = dstring_initialize("Department of ");
    str2 = dstring_initialize("Redundancy ");
    dstring_concatenate(&str1, str2); 



    assert(str1 != NULL);
    assert(str2 != NULL);
    assert(strlen(str2) == 11); 
    assert(strlen(str1) == 25); 



    dstring_print(str1, stdout);    
    dstring_truncate(&str1, 10);    
    dstring_print(str1, stdout);    


    dstring_delete(&str1);
    dstring_delete(&str2);


    assert(str1 == NULL);
    assert(str2 == NULL);
    return 0;
}
#包括
#包括
#包括“dstring.h”
内部主(空)
{
DString str1,str2;
str1=dstring_初始化(“部门”);
str2=dstring_初始化(“冗余”);
dstring_串联(&str1,str2);
断言(str1!=NULL);
断言(str2!=NULL);
断言(strlen(str2)==11);
断言(strlen(str1)==25);
dstring_打印(str1、stdout);
dstring_截断(&str1,10);
dstring_打印(str1、stdout);
dstring_delete(&str1);
dstring_delete(&str2);
断言(str1==NULL);
断言(str2==NULL);
返回0;
}
首先,不要强制转换malloc或realloc的结果

*destination = realloc(*destination, sizeof(char) * (strlen(truncatedLength) + 1)); // Omallokerar för en sträng
而且,
sizeof(char)
总是
1
,因此它是冗余的

这里的问题是您正在调用
strlen(truncatedLength)
,但这是一个
int
。你可能是想打电话的

*destination = realloc(*destination, sizeof(char) * (truncatedLength + 1)); // Omallokerar för en sträng
但是,您会立即用

*destination = "Department";
因此,
realloc
没有任何作用

如果要覆盖内容,需要使用
strcpy
或类似功能

但是,我假设您不打算在截断时替换内容。在这种情况下,需要向字符串添加终止符:

 (*destination)[truncatedLength] = '\0';
导致:

void dstring_truncate(DString* destination, unsigned int truncatedLength)
{
    assert(destination != NULL);
    assert(*destination != NULL);

    *destination = realloc(*destination, truncatedLength + 1); // Omallokerar för en sträng
    (*destination)[truncatedLength] = '\0';

    assert(strlen(*destination) == truncatedLength); //kollar om längden är 10
}

malloc
realloc
之间的区别在于
realloc
接受指针和大小,而
malloc
只接受大小
realloc
返回一个指向内存位置的指针,该内存位置包含与原始指针相同的数据(复制数据并在必要时释放上一个指针),但具有指定的大小。

主题外,但
dstring_truncate
是一个函数,不是一个类
malloc
realloc
之间的区别似乎在手册页中描述得很清楚。你需要参考一本好的C语言书,IMHO。哦,亲爱的<代码>realloc错误@SouravGhosh我期待着这个评论(和这个链接)出现。。。
void dstring_truncate(DString* destination, unsigned int truncatedLength)
{
    assert(destination != NULL);
    assert(*destination != NULL);

    *destination = realloc(*destination, truncatedLength + 1); // Omallokerar för en sträng
    (*destination)[truncatedLength] = '\0';

    assert(strlen(*destination) == truncatedLength); //kollar om längden är 10
}