标签: realloc

在C/C++中使用realloc时出错

char *t = malloc(2);
t = "as";

t = realloc(t,sizeof(char)*6);
Run Code Online (Sandbox Code Playgroud)

我收到错误"无效指针:0x080488d4 * "..

我在使用内存分配函数时遇到了奇怪的错误.是否有任何好的tuts /指南可以解释我的内存分配功能.我正在使用linux ..

请帮忙..

c realloc

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

Realloc和strcpy

我的代码给了我段错误:我不明白,调试器说错误来自于从stored_打印值

char *stored_ = NULL;
char testMessage[15];

//strcpy(stored_, testMessage);

for (int a = 0;a < 10; a++)
{
    sprintf(testMessage,"Message::%i\n",a);
    printf("string is:%s;length is %i\n",testMessage,strlen(testMessage));

    stored_ = (char*) realloc (stored_, sizeof(char) * (strlen(testMessage) * (a+1) ));

    strcpy(&stored_[a], testMessage);
} 

for (int b = 0;b < 10; b++)
{
    printf("inside:|%s|\n",stored_[b]);
}
Run Code Online (Sandbox Code Playgroud)

c realloc segmentation-fault

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

linux和osx中不同的realloc行为

我有这个简单的C程序:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char **argv) {
    int i = 0;
    int j = 0;
    size_t size = 4194304; /* 4 MiB */
    char *buffer = malloc(size);
    char *buffers[10] = {NULL};
    void *tmp_pointer = NULL;
    fprintf(stderr, "initial size == %zu\n", size);
    fprintf(stderr, "initial buffer == %p\n\n", buffer);
    srand(time(NULL));
    /* let's see when it fails ... */
    for (; i < 10; ++i) {
        /* some random writes */
        for (j = …
Run Code Online (Sandbox Code Playgroud)

c linux macos errno realloc

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

为什么realloc会在重复调用时失败,因为使用malloc分配大块有效?

我试图从stdin读入(从文件传递值).我正在从字符串中读取每个字符并将其存储到动态分配的字符串指针中.需要时我会重新分配内存.我想尽可能多地填充字符.虽然我可以将其限制为100,000个字符.但是在一些迭代之后realloc失败了.但是如果我在malloc的第一次初始化期间指定一个大块的大块,比如1048567,我就能完全读取该字符串.为什么是这样?

以下是我的计划:

#include <stdio.h>
#include <stdlib.h>

int display_mem_alloc_error();

enum {
    CHUNK_SIZE = 31    //31 fails. But 1048567 passes.
};

int display_mem_alloc_error() {
    fprintf(stderr, "\nError allocating memory");
    exit(1);
}

int main(int argc, char **argv) {
    int numStr;                  //number of input strings
    int curSize = CHUNK_SIZE;    //currently allocated chunk size
    int i = 0;                   //counter
    int len = 0;                 //length of the current string
    int c;                       //will contain a character
    char *str = NULL;            //will contain the input string
    char *str_cp = …
Run Code Online (Sandbox Code Playgroud)

c string malloc realloc

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

将C realloc转换为Delphi

int StackRealloc(stack* S){

  if ((S->array = realloc(S->array,sizeof(int)*(S->size)*2)) != NULL){
    S->size = (S->size)*2;
    return 1;
  }
  else return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何将此代码(c)转换为Delphi(Delphi7)?

c memory delphi realloc

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

我在下面的代码中得到Aborted(core dumped)

嗨,在我的下面的代码测试realloc()我收到错误Aborted (Core dumped).

代码的输出如下

$ ./realloc
My Name // <<-- This works
Aborted (core dumped) // <<-- why this error occur?
Run Code Online (Sandbox Code Playgroud)

copyThis()第一次调用函数给出正确的结果并且不产生错误.以同样的方式,如果我copyThis()第二次打电话,它会出错.我不明白为什么会这样.任何人都可以指出问题出在哪里以及我应该做些什么调整?

代码如下

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *copyThis(char *str1, const char *str2, size_t size);

int main(void)
{
    char *ptr1 = "My Name ";
    char *ptr2 = "is Alice";
    char *ptr3;
    char *ptr4;

    ptr3 = copyThis(ptr3, ptr1, strlen(ptr1) + 1); // This works
    printf("%s\n", ptr3);

    ptr4 = copyThis(ptr4, ptr2, strlen(ptr2) +1); …
Run Code Online (Sandbox Code Playgroud)

c memory-management realloc

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

realloc()会使所有指针无效吗?

注意,这个问题不是询问realloc()原始块中的指针是否无效,而是它是否使所有其他指针无效.

realloc()对它的性质有点困惑,特别是如果它移动任何其他记忆.

例如:

void* ptr1 = malloc(2);
void* ptr2 = malloc(2);
....
ptr1 = realloc(ptr1, 3);
Run Code Online (Sandbox Code Playgroud)

在此之后,我可以保证ptr2realloc()呼叫之前指向相同的数据吗?

c pointers realloc

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

重新分配内存时"无效的下一个大小"异常

PS:我几乎所有的问题都是"无效的下一个大小",但他们没有帮助我,因为我没有另外一段代码可以使用malloc或realloc,所以这是排除在外的.另外,我没有超出内存空间的限制,所以我也很好.

我有以下代码给我 - *** Error in./server_issue':realloc():下一个大小无效:0x08249170***`.

我无法识别问题,我一次只分配64个字节,所以我没有内存地址空间,还有其他内存分配可能会损坏我的堆内存.

错误说,下一个大小无效但是因为我的上一个日志告诉Reallocating 64 bytes, Aborted (core dumped)所以这意味着我仍然在尝试分配64个字节.那么,为什么错误呢?

对于HTML文件,任何超过256个字节的文件.

代码:(重现问题的最小代码)

#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef char BYTE;
bool load(FILE*, BYTE**, size_t*);

int main(void)
{
    FILE* file = fopen("/home/university/psets/pset6/pset6_working/public/hello.html", "r");
    BYTE* content;
    size_t length;
    load(file, &content, &length);
}

bool load(FILE* file, BYTE** content, size_t* length)
{
    int totalLength = 0;
    int readBytes = 0;
    *content = NULL;

    BYTE* fileContentTemp[64]; // working with 222222
    while ((readBytes = fread(fileContentTemp, 1, 64, …
Run Code Online (Sandbox Code Playgroud)

c memory memory-management realloc

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

内存泄漏与malloc和fgetc

我在使用malloc和getchar从用户那里读取类似内容时遇到了一些麻烦.我得到了结果,然而,我使用valgrind得到了内存泄漏.我对此很无能为力,并且问了我的同学和导师,但似乎没有人知道为什么.

char *ReadLineFile(FILE *infile){
   int i=0;
   char c;
   char *newStringLine;
   newStringLine = (char *) malloc(sizeof(char));
   while( (c = fgetc(infile)) != '\n' ){
        newStringLine[i++] = c;
        realloc(newStringLine, (sizeof(char) * (i+1)));
   }
   newStringLine[i] = '\0';
   return newStringLine;
}
Run Code Online (Sandbox Code Playgroud)

Valgrind给了我几个错误,包括1的无效写/读和无效的realloc.

c pointers realloc dynamic-memory-allocation

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

使用mallloc每隔三分钟运行我的程序进行分段错误

我几乎每隔三次就会遇到一个分段错误,我正在努力理解为什么.我认为它的原因是使用malloc()free()错误.我必须读取用户标准输入,然后使用malloc将其保存在数组中.这部分工作,直到错误开始发生.

我的代码:

char *Input() {
    char user_input;
    int length;

    char *buffer = malloc(2 * sizeof(char));

    while (((user_input = getchar(stdin)) != EOF) && (user_input != '\n')) {
        buffer[length] = user_input;
        length++;
        char *buffer_new = realloc(buffer, length + 2);
        if (buffer_new != NULL) {
            buffer = buffer_new;
        } else {
            free(buffer);
            printf("Error.\n");
            return 1;
        }
    }

    buffer[length] = '\0'; 

    if (strlen(buffer) > 200) {
        printf("Error.\n");
        return 2;
    }

    return buffer;
}
Run Code Online (Sandbox Code Playgroud)

我不确定我的错误在哪里或错误发生的原因.以下是我如何调用该函数:

int main() {
    char *input = …
Run Code Online (Sandbox Code Playgroud)

c pointers realloc dynamic-memory-allocation

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