我正在尝试为一个结构数组(实际上是一个数组,每个包含2个结构,但为了简单起见,这里包括1个)来动态重新分配内存,这些结构正在从文件中读取或由用户输入.
typedef Struct
{
char surname[21];
char firstname[21];
char username[21];
...
} User;
Run Code Online (Sandbox Code Playgroud)
...在main()中:
int size = 0; /* stores no. of structs */
User* user_array = (User *) calloc(1, sizeof(User));
if(user_array == NULL)
{
printf("Cannot allocate initial memory for data\n");
exit(1);
}
else
size++;
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用函数调用在需要时增加数组:
int growArray(User user_array*, int size)
{
User *temp;
size++;
temp = (User *) realloc(user_array, (size * sizeof(User));
if(temp == NULL)
{
printf("Cannot allocate more memory.\n");
exit(1);
}
else
user_array = temp;
return size;
} …Run Code Online (Sandbox Code Playgroud) 看了一些开源项目的C代码之后,我不确定我是不是做得对.
当我创建字符串(char*)时,我通常会这样做:
#define DEF_LEN 10
char *mystring;
mystring = malloc(DEF_LEN*sizeof(char));
Run Code Online (Sandbox Code Playgroud)
当我更改我的字符串时(通常在函数内完成):
mystring = realloc(mystring, strlen(newstring)*sizeof(char)+1);
strcpy(mystring,newstring);
Run Code Online (Sandbox Code Playgroud)
在许多开源项目中,我看到许多开发人员只是这样做:
char another_string[1024];
Run Code Online (Sandbox Code Playgroud)
问题:
realloc好吗?realloc性能杀手(在我的代码/经常使用)?我正在尝试创建一个将两个向量附加在一起的函数,但是当我尝试将更多内存重新分配给第一个向量时,我最终得到了一个段错误,因此第二个向量适合.这是代码
void vectorAppend(double** v1, size_t* s1, double const* v2, size_t s2){
assert(v1 && v2 && *s1 > 0 && s2 > 0);
(*v1) = realloc((*v1), (*s1 + s2)*sizeof(double));
for (size_t i = 0; i < s2; i++){
*v1[*s1+i]=v2[i];
}
*s1+=s2;
}
Run Code Online (Sandbox Code Playgroud)
这就是我从main中调用它的方式
double *v1 = vectorConstruct(3, 2);
double *v2 = vectorConstruct(3, 0);
unsigned int s = 3;
vectorAppend(&v1, &s, v2, 3);
Run Code Online (Sandbox Code Playgroud)
vectorConstruct返回一个指向初始化为第二个参数的向量的指针.
double* vectorConstruct(size_t s, double val){
assert(s>0);
double *ret = malloc(s*sizeof(double));
for(size_t i = 0; i < s;i++){
ret[i]=val; …Run Code Online (Sandbox Code Playgroud) 我编写了一个将字符串转换为字符串数组的函数.测试程序正常工作,直到我替换int main()为int main(int argc, char** argv)
安慰:
*** glibc detected *** ./a.out: realloc(): invalid pointer: 0xbfdc6370 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb764eee2]
/lib/i386-linux-gnu/libc.so.6(realloc+0x25d)[0xb765355d]
/lib/i386-linux-gnu/libc.so.6(realloc+0x273)[0xb7653573]
./a.out[0x80485d6]
./a.out[0x80486bf]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75f24d3]
./a.out[0x8048461]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:05 2889457 /home/tg/a.out
08049000-0804a000 r--p 00000000 08:05 2889457 /home/tg/a.out
0804a000-0804b000 rw-p 00001000 08:05 2889457 /home/tg/a.out
085cc000-085ed000 rw-p 00000000 00:00 0 [heap]
b75a5000-b75c1000 r-xp 00000000 08:05 5637044 /lib/i386-linux-gnu/libgcc_s.so.1
b75c1000-b75c2000 r--p 0001b000 08:05 5637044 /lib/i386-linux-gnu/libgcc_s.so.1
b75c2000-b75c3000 rw-p 0001c000 08:05 5637044 /lib/i386-linux-gnu/libgcc_s.so.1
b75d8000-b75d9000 rw-p 00000000 00:00 …Run Code Online (Sandbox Code Playgroud) 我有一个程序可以计算文本文件中的单词出现次数并将它们存储在一个数组中.到目前为止,我正在使用固定数组,一切正常但现在我想将其更改为动态数组,因此永远不会浪费/需要任何内存.我知道必须使用malloc和realloc来实现这一点,但我真的不明白如何去做.
我的第一个想法是简单地计算文本文件中的单词然后malloc足够的空间为所有这些,但这将留下浪费的空间,因为重复的单词将有一个计数器增加,但不会再次添加到数组.
这种方法听起来有道理并且是实现它的最佳方式吗?如果我首先malloc一个小数组就足以找到一个单词及其计数器.然后每当我找到一个需要添加到数组的新单词时,只需重新分配足以适合另一个单词和计数器.如果它是重复的,则不需要realloc,因为现有的计数器只会递增.
我有一个有num行的文件:每行包含一个数字.我想将每个数字保存到矢量中*vet.为什么这段代码不起作用?
Segmentation fault (core dumped)
我认为错误是sscanf在save_numbers功能,但我不知道为什么.
#include <stdio.h>
#include <stdlib.h>
/* This function allocate memory
and save numbers into a vector */
int save_numbers (int **vet, int *num)
{
FILE *fin;
int i = 0;
char buff[10];
if ( !(fin = fopen("numbers.dat", "r")) )
return 1;
while ( fgets(buff, sizeof(buff), fin) )
{
*vet = (int *) realloc (*vet, (i+1) * sizeof(int) );
sscanf (buff, "%d", vet[i]);
i++;
}
*num = i; …Run Code Online (Sandbox Code Playgroud) 如果我重新分配以前分配的内存区域的特定内存块会发生什么?
#include <stdlib.h>
int main(void)
{
char *area = malloc(15 + 1);
strcpy(area, "Stack / Overflow");
realloc(area + 5, strlen(area) + 5);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
area在这个例子中,字符串是否会扩展为5个字节?
Idx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Chr: S t a c k \0 \0 \0 \0 \0 / O v e r f l o w \0
Run Code Online (Sandbox Code Playgroud) 考虑代码:
char *word = NULL; // Pointer at buffered string.
int size = 0; // Size of buffered string.
int index = 0; // Write index.
char c; // Next character read from file.
FILE *file = fopen(fileDir, "r");
if (file)
{
while ((c = getc(file)) != EOF)
{
printf("Current index: %d, size: %d, Word: %s\n", index, size, word);
if (isValidChar(c))
{
appendChar(c, &word, &size, &index);
}
else if (word) // Any non-valid char is end of word. If (pointer) …Run Code Online (Sandbox Code Playgroud) 我在使用malloc/realloc命令和数组时遇到了一些麻烦.我已经创建了一个包含一些整数的小数组,并尝试通过使用realloc扩展大小并添加值来为其添加一个值,但是当我这样做时,0索引的值不会被保留并被视为垃圾.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
printf("Enter size of array\n");
scanf("%d",&n);
int *A = malloc(n*sizeof(int));
for(int i = 0; i < n; i++){
A[i] = i + 1;
}
*A = realloc(A, sizeof(A)+ sizeof(int));
A[n] = 1234;
for(int i = 0; i < n + 1; i++){
printf("%d\n",A[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,会发生这种情况:
Enter size of array
5
14643216
2
3
4
5
1234
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么数组的0索引得到这个值而不是1?
我不明白为什么valgrind(版本3.14)没有在此程序中检测到可能的内存泄漏:
#include <stdlib.h>
int main() {
int *p = malloc(sizeof(int));
p = realloc(p, 2 * sizeof(int));
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C99标准(ISO / IEC 9899:1999,第314页)表示以下内容realloc:
如果无法分配用于新对象的内存,则不会释放旧对象,并且其值不变。[...] realloc函数返回指向新对象的指针(该值可能与指向旧对象的指针的值相同),如果无法分配新对象,则返回null指针。
因此,它可能发生的p是NULL,但与以前分配的存储单元malloc仍然存在,不应该这是一个可能的内存泄漏?
如果我使用编译程序gcc -std=c99并执行valgrind,--tool=memcheck --leak-check=full --track-origins=yes则会显示以下消息:
#include <stdlib.h>
int main() {
int *p = malloc(sizeof(int));
p = realloc(p, 2 * sizeof(int));
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)