标签: realloc

在我自己的getline版本上出现故障

我正在尝试制作一个简单版本的getline.它应该从stdin读取一行,根据需要重新分配缓冲区的大小.它还应该返回读取的字符数.它需要一个char**,以便以后可以释放重新分配的缓冲区.为什么我会遇到段错误?

继承人我的版本:

int get_input_line(char **buff, int start_size) {
char c;
int stop = 0, length = 0, k = start_size;
while(!stop) {
    if(length > k) {
        k += 50;
        buff = (char *)(realloc(buff, start_size + 1));
    }
    c = getchar();
    if(c == '\n'){
        stop = 1;
    }
    buff[length] = c; 
    length++;
}
return length;
}
Run Code Online (Sandbox Code Playgroud)

这是电话:

char *buff = (char *)(malloc(50 + 1));
get_input_line(&buff, 50);
printf("%s", buff);
Run Code Online (Sandbox Code Playgroud)

c memory-management getline realloc segmentation-fault

0
推荐指数
2
解决办法
137
查看次数

call中的realloc()打印垃圾值

我已经上传了所有的代码..这是正在进行的工作..请检查realloc()因为如果我没有达到realloc()的条件一切正常...谢谢evry1 ..

  // contactinfo.h--  header file 
  #ifndef _ELEMENT_H

  #define _ELEMENT_H

  typedef struct ContactInfo ContactInfo;

  struct ContactInfo{
    char Name[30];

    char email_id[50];

    int phon_num;

  };


typedef ContactInfo **ContactList;

 #endif 

 //contactops.h

  #include "contactInfo.h"

ContactList createCL(int size);

void addContact(ContactList clist1, ContactList clist2, ContactInfo ci, int size);

ContactInfo *findByName(ContactList cl, char *name);

ContactInfo *findByNumber(ContactList cl, int num);

void deleteContactByName(ContactList cl, ContactList c2, char *name);

void deleteContactByNumber(ContactList cl, ContactList c2, int num);

void printContacts(ContactList cl);

void Merge_Sort(int hi, int mid, int lo, ContactList c);

void Merge(int …
Run Code Online (Sandbox Code Playgroud)

c realloc

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

这个realloc使用有什么问题

我正在尝试使用realloc从用户那里获得无限的输入.这是我到目前为止所做的:

int getCharactersFromUser(char* arr,char terminator)
{
char c = getch();
int length =0;
while(c!=terminator)
    {
    arr = realloc(arr, sizeof (arr)*(++length));
    arr[length-1]=c;
    c = getch();
    }
return length;
}
Run Code Online (Sandbox Code Playgroud)

我用这样的arr调用这个方法:char *unknownArr = calloc(0,sizeof *unknownArr); int length = getCharactersFromUser(&unknownArr,TEMINATOR_FOR_LIST);TEMINATOR_FOR_LISTeof

c realloc

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

realloc()无法重新分配内存

我正在写一个文件复制程序,其中我遇到了realloc()的困难.请看下面的代码片段(我写的是为了解realloc()的工作情况): -

int main(){
    char *p =(char *) malloc ( 10 ),*t;
    p = "this is";
    if (strlen (p)==7)
    {
        t = realloc ( p,14);
        if ( t==NULL)
        {
            printf ("no no\n");
        }
    }
    printf("%p\n%p\n%d",p,t,strlen(p));
    free (p);
    free (t);
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

产量

no no      //because realloc () is unable to reallocate  the memory
00450357   //address of p
00000000   //address of t
Run Code Online (Sandbox Code Playgroud)

那么为什么realloc()无法重新分配内存并将其(它的地址)分配给t

编辑 我在Windows中使用代码块.

c realloc

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

C中的变量参数在Valgrind中创建错误

我试图运行一个使用函数的程序concat_str.它可以将多个参数作为字符串,参数的结尾表示为"quit".我的功能代码如下:

char *concat_str(char *str1, ...)
{
    va_list pstr;
    char *minion = NULL, *temp = NULL;
    minion = (char*) malloc (sizeof(str1));
    strcpy (minion,str1);
    va_start (pstr, str1);
    if ( strcmp ("quit",str1) == 0)
    {
        va_end (pstr);
        return minion;
    }
    while (1)
    {
        temp = va_arg (pstr, char *);
        if ( strcmp ("quit", temp) == 0)
        {
            break;
        }
        minion = (char*) realloc (minion, sizeof(temp));
        strncat (minion,temp,sizeof(temp));
    }
    va_end (pstr);
    return minion;
}
Run Code Online (Sandbox Code Playgroud)

对此的调用声明将是:

char *result;
result = concat_str("hello", …
Run Code Online (Sandbox Code Playgroud)

c variables valgrind arguments realloc

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

realloc指针数组无所事事

我有一个字符串数组,当它不再有NULL指针(意味着数组已满)时,我想要它.我已经尝试了realloc并没有成功,我想我并没有正确地指向思考.

这是我的代码:

int storage; //global, outside of main
int i, key;
char **people;
char **phones;

printf("Please enter a storage cacpity:\n");
scanf("%d",&storage);
printf("\n");

people=malloc(storage*sizeof(char *));
phones=malloc(storage*sizeof(char *));

for (i=0; i<storage; i++) {
    people[i] = NULL;
    phones[i] = NULL;
}

void AddNewContact(char * people[], char * phones[]) {
    char name[100];
    char phone[12];
    int i, listfull = 0;

    printf("Enter a contact name:\n");
    scanf("%s",&name);
    printf("Enter a phone number:\n");
    scanf("%s",&phone);

    for (i=0; i<storage; i++) {
        if (people[i]==NULL) {
            people[i] = (char *)malloc(strlen(name));
            phones[i] = (char …
Run Code Online (Sandbox Code Playgroud)

c arrays string pointers realloc

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

奇怪的free()无效指针C.

我修改了很多次这段代码的问题(但总是出现错误):似乎在释放"过滤器"的最后一个索引时出错

char** read_and_filter(int fd) {
  char buf[MAXLENGTH];
  char **bufs=NULL;
  char ch;
  int j = 0, len = 0, t = 0;
  while (!t && read(fd,&ch,1) == 1) {
    switch (ch) {
    case '\n':
      t = 1;
    case ' ':
      bufs = realloc(bufs, (j+1)*sizeof(char*));
      bufs[j++] = strndup(buf,len);
      memset(buf,0,len);
      len = 0;
      break;
    default:
      buf[len++] = ch;
    }
  }
  bufs[j] = 0;
  return bufs;
}

int main(int argc, char **argv) {
  char **filter;
  int i,fd = open("input.txt",O_RDONLY);
  filter = read_and_filter(fd);
  for(i = …
Run Code Online (Sandbox Code Playgroud)

c malloc free pointers realloc

0
推荐指数
2
解决办法
819
查看次数

c中的线程重新分配

我很难重新分配一些内存来存储更大的字符串.这是在一个线程上分配内存然后将其传递给另一个线程.然后另一个线程进行一些处理并更改字符串.问题是它无法更新字符串并失败.我没有太多关于C的经验,并且肯定认为我错过了什么或者说错了什么.

这是一些示例代码.

//Struct to store options for passing across threads
typedef struct options{
    char *host;
//other options here
} options;

int main(int argc, char *argv[])
{
    //set initial options
    options *opt;
    opt->host = malloc(strlen(argv[0])*sizeof(char));
    strcpy(opt->host,argv[0]);

    //set variables for thread
    void *pData;
    DWORD dwThreadId;
    HANDLE hThread = NULL;

    //allocate memory for thread
    pData = malloc(sizeof(options));
    memcpy(pData, opt, sizeof(options));

    //create thread
    hThread = CreateThread(NULL,0,WorkerThread,pData,0,&dwThreadId);
    if (hThread ==NULL) //thread not started
         exit(1);

    //wait for thread
    WaitForSingleObject(hThread,   //Address of thread
                    INFINITE); //Timeout value

    //close …
Run Code Online (Sandbox Code Playgroud)

c multithreading realloc

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

Realloc分配的内存多于请求的内存

我有以下功能的问题.

当我尝试realloc()记忆时,我得到的比实际要求的要多!

在这种情况下我会再做连接两个字符串,一个谁是14个字符长,一个谁是长11个字符,但最终的结果是,memTemp长38个字符,即使memNewSize表明,它实际上是在25,没有人知道该怎么办?

int dstring_concatenate(DString* destination, DString source)
{
    assert(destination != NULL); // Precondition: destination ar ej NULL
    assert(*destination != NULL); // Precondition: *destination ar ej NULL
    assert(source != NULL); // Precondition: source ar ej NULL
    //Dstring looks like this = "typedef char* Dstring;"

    int memNewSize = strlen(*destination) + strlen(source);
    char *memTemp;
    memTemp = (char*)realloc(memTemp, sizeof(char) * memNewSize);

    printf("%d\n", memNewSize);
    printf("%d\n", strlen(memTemp));

    if(memTemp == NULL)
    {
        printf("Could not allocate new memory.\n");
        return 0;
    } …
Run Code Online (Sandbox Code Playgroud)

c pointers realloc dynamic-memory-allocation

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

C.双数组和char数组中的realloc之间的区别

我必须增加双数组的长度。我知道,如何用char数组来做,所以我尝试了这个:

int main() {
    char * tmp = NULL;
    for (int i = 1; i <= 4; i++) {
        tmp = realloc(tmp, i * sizeof(char));
        tmp[i] = 'i';
    }
    puts("char OK");
    double * tmp1 = NULL;
    for (int i = 1; i <= 4; i++) {
        tmp1 = realloc(tmp1, i * sizeof(double));
        tmp1[i] = 0;
    }
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

第一个数组工作正常。但是第二个消息粉碎了realloc(): invalid next size

因此,有两个问题:
1.为什么这种方法不能在双数组中工作?
2.如何动态增加双精度数组的大小?

UPD:删除了错字

c memory arrays memory-management realloc

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