我在程序中创建了几个函数。在我的类(class)中“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
.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);
}
测试文件
#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;
}
最佳答案
*destination = (char*) realloc(*destination, sizeof(char) * (strlen(truncatedLength) + 1)); // Omallokerar för en sträng
首先,不要强制转换 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 返回一个指向内存位置的指针,该内存位置包含与原始指针相同的数据(如有必要,复制数据并释放前一个指针),但具有指定的大小。
关于c - 分配内存(C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37238348/