FAQ Database Discussion Community


re-alloc Invalid Pointer runtime error

c,pointers,runtime-error,realloc
Code below merges 2 sorted arrays A and B of size n1 and n2 respectively. Merged output needs to be stored in A. (No need to go through entire code) Doubt: While re-allocating A, I am getting a run-time error. Why? int* temp = (int*)realloc(A,sizeof(int)*(n1+n2)); if(temp != NULL) A =...

How to recycle and reuse allocated memory?

c,malloc,realloc
What I'm working on right now is a state-based parser for any input from a stream. My professor tells me this is the best way to avoid special cases. The way I've got it set up is using functions, and I'm having a little bit of trouble trying to reuse...

card game in c, shuffle linked list

c,linked-list,shuffle,realloc
I trying to shuffle a linked list in c. My idea was to move the list into an array of card then to shuffle the array and then to put it all back in the linked list. when I do build everything is ok but when id use the debugger...

Cannot free fileName char * after fclose

c,memory-management,malloc,free,realloc
I am trying to free my file name (char * pointer) but got an error : Heap corruption detected: after normal block (#65) at 0x.... The code: static FILE *initializeIndexFile(char *database, char **indexFileName) { FILE *file1_p; *indexFileName = NULL; int len = strlen(database); *indexFileName = (char *)malloc(len *sizeof(char) + 1);...

Realloc in list implementation sends a SIGABRT signal on third call

c,list,void-pointers,realloc,sigabrt
typedef struct List { void **data; int dataSize; int count; int capacity; } List; List list_create(int dataSize) { List list; list.data = malloc(dataSize); list.dataSize = dataSize; list.count = 0; list.capacity = 1; return list; } void list_add(List *list, void *data) { if(list->count == list->capacity) { printf("INCREASING LIST..."); void *temp =...

Disadvantages of calling realloc in a loop

c,memory-management,out-of-memory,realloc
I'm trying to implement some math algorithms in C on Windows 7, and I need to repeatedly increase size of my array. Sometimes it fails because realloc can't allocate memory. But if I allocate a lot of memory at once in the beginning it works fine. Is it a problem...

Did I use realloc in a wrong way?

c,linux,memory,realloc
This is a part of my program which is related to realloc(). I give the array myEdge an initial size my_edge_num, when this size is not enough, realloc() more space to it. However, even though the new realloc temp_edge is not NULL, it still says EXC_BAD_ACCESS in the next step...

malloc and realloc array giving unexpected output

c,arrays,malloc,realloc
When input is '1 2 3 4 5 6 7 8 9 10 11' Expected output should be the same as input. Yet the output is '1 2 3 4 5 6 7 8 9 10 -1850774484' . Only happens when there are more than 10 integers being inputted. Where...

Using Realloc to resize a char **

c,realloc
typedef struct element element; struct element{ dado_t str; elemento* preview; elemento* next; }; typedef struct lista2 lista2; struct lista2{ elemento* primeiro; elemento* ultimo; elemento* corrente; }; void caller(lista2* l){ char *s = l->corrente->str; char *s2 = l->corrente->next->str; my_func(&s, &s2); } void my_func(char **s, char**s2){ size_t len = strlen(*s); size_t len2...

Invalid Pointer while realloc()

c,realloc
Caller Function: void caller_func(){ char *c = malloc(strlen("Hi") + 1); c = "Hi"; //c is a pointer char* char **s = &oi; //s is a pointer to a pointer: char **; called_func(s, 3, 'A'); } Called Function void called_func(char **s, int p, char c){ char *w = *s; //w =...

How do I create a function in C that allows me to split a string based on a delimiter into an array?

c,arrays,string,pointers,realloc
I want to create a function in C, so that I can pass the function a string, and a delimiter, and it will return to me an array with the parts of the string split up based on the delimiter. Commonly used to separate a sentence into words. e.g.: "hello...

What's the bug with a possible wrapper function of realloc C function?

c++,c,malloc,realloc
I like to know the possible bug in the following possible wrapper function of the realloc C function: void reallocX(void** ptr, size_t size) { void *new_ptr = realloc(*ptr, size); if (new_ptr != NULL) { if (new_ptr != *ptr) { *ptr = new_ptr; } } else { printf( "Help! realloc returned...

C Language Dynamic array realloc causes Heap block error message

c,arrays,visual-studio,realloc
I just started learning C programming and practicing dynamic array and malloc and realloc. I'm using Visual Studio and the code is in file extension .c But I do not know why realloc is causing the error message "HEAP[Assignment1Start.exe]: Heap block at 01217FA8 modified at 01217FE8 past requested size of...

Malloc , Realloc , Memset : Struct pointers , arrays of char, int

c,pointers,char,malloc,realloc
#ifndef vECTOR_h #define vECTOR_h #include<string.h> #include<stdio.h> #include<stdlib.h> /* My iterator is an int but thanks to this typedef I could change it easily */ typedef int iterator; #define iterator_begin 0 #define iterator_end -1 //typedef is for vector_type and in the meanwhile struct vector_struct is defined typedef struct vector_struct { size_t...

Realloc doesn't work in a while loop

c,memory-leaks,realloc
I need help with my C assignment. The task is to write a program, which takes string input of unknown length. Also I need to separate words, that's why I use char**. Program stops taking input, when a special word appears. You can see my code bellow: char **words=NULL; maxWords=1;...

Realloc an array but do not lose the elements in it

c,pointers,malloc,realloc
Code: int * data; data = malloc(sizeof(int)*10); int i; for(i=0;i<10;i++) data[i]=i; int * aux; aux = realloc(data,sizeof(int)*20); if(aux) data=aux; for(i=10;i<20;i++) data[i]=i; A teacher once told me "No, you can't reallocate an array with elements without a backup"; I said, "Oh, OK", but now that make no sense to me. The...

Why realloc fails with temporary pointer

c,pointers,char,malloc,realloc
The following works successfully: char *op, op_temp; op = malloc(len+1); op_temp = op; op = realloc(op, ++len); while the following results in a runtime error: char *op, op_temp; op = malloc(len+1); op_temp = op; op = realloc(op_temp, ++len); Why so even though the same piece of memory is reallocated?...

Does realloc() invalidate all pointers?

c,pointers,dynamic-memory-allocation,behavior,realloc
Note, this question is not asking if realloc() invalidates pointers within the original block, but if it invalidates all the other pointers. I'm new to C, and am a bit confused about the nature of realloc(), specifically if it moves any other memory. For example: void* ptr1 = malloc(2); void*...

pointer being realloced was not allocated

c,malloc,realloc
I am trying to dynamically allocate an array of structs but whenever I run the program I keep getting: a.out(6487,0x7fff7ecb8300) malloc: * error for object 0x7fff6f670000: pointer being realloc'd was not allocated * set a breakpoint in malloc_error_break to debug struct node { char course[25]; char category[20]; char prereq[50]; char...

realloc() in a 64bit iOS device

ios,c,realloc
When I use the C function realloc(p,size) in my project, the code runs well in both the simulator and on an iPhone 5. However, when the code is running on an iPhone 6 plus, some odd things happen. The contents a points to are changed! What's worse, the output is...

Realloc and glocal new/delete operator overriding

c++,c,stdvector,realloc
Stated that there is no C++ equivalent of the C realloc function, I've found in another question that such a thing is automatically managed by std::vector and we should use it instead. I'm fine with it. I guess that, since there is no other way of do reallocation, the std::vector...

How use index in a pointer with Struct and pointer in C

c,pointers,struct,malloc,realloc
I need to make a program that can register some car. Then I need show all the cars registeres. I can't make this work, when I execute the code below the printf show just memory trash, and just the last car appears right! Code (I have a menu function that...

realloc: invalid next size error

c,realloc
I am having this error with realloc that only occurs on my school's lab computer and not on mine. In this program, I am storing line numbers in a File_Node struct. File_Node is part of a linked list and each node contains a file path string and an array line...

Realloc Invalid Pointer. Aborted (core dumped)

c,pointers,realloc
*MyFile.h* typedef char* dado_t; typedef struct elemento elemento; typedef struct lista2 lista2; *MyFile.c* struct elemento{ dado_t str; elemento* ant; elemento* prox; }; struct lista2{ elemento* primeiro; elemento* ultimo; elemento* corrente; }; void_insert(lista2* l, dado_t d){ elemento* novo = malloc(sizeof(elemento*)); novo->str = malloc(strlen(d) * sizeof(char)); novo->str = d; l->corrente =...

Segfault with String array C++

c++,arrays,segmentation-fault,malloc,realloc
Had this program in C and was trying to convert some to C++ as I learn the language. Basically char arrays to strings and some of the input/output. Only issue is I get a segfault when attempting to put the input string into the string array (test2 prints. test3 does...

Create a dynamic array of strings that will scale upon being filled up

c,arrays,string,dynamic-arrays,realloc
Okay, so I'm reading though a txt file and storing each item by new line into an array of strings. The only problem is, I need to resize this array by 5 whenever it is close to filling up. Input looks something like this, except a lot larger CSE 1104...

Why realloc of char** gives Address is 0 bytes after alloc'd

c,segmentation-fault,realloc,dynamic-allocation
I have the following code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char *argv[]) { char* filename = "file_prefix.txt"; FILE* file_prefix = fopen(filename, "r"); char buff[1024]; int i = 0; char** prefix = NULL; char c = fscanf(file_prefix, "%s", buff); while ( EOF != c ) { printf("%d...

Confusion with realloc in C language [duplicate]

c,malloc,realloc
This question already has an answer here: Why can I still access a member of a struct after the pointer to it is freed? 6 answers I have a code like this : void **array = (void**)malloc(sizeof(void*)*4); array[0] = (void *)"Hello"; array[1] = (void *)"World"; array[2] = (void *)"My";...

Initially mallocate 0 elements to later reallocate and measure size

c,arrays,malloc,realloc
I have a function that will add a new position to an array by reallocating new memory every time it is called. The problem is that, for each call I need it to add one position to the array, starting from 1 at first call, but I understand that I...

How to realloc a *char[] inside a while loop?

c,pointers,char,malloc,realloc
Let's consider this array of char pointers : char *lesMots[10]; Which is used inside a while loop : while(fgets(buffer, TAILLE_BUFFER, fichier)!=NULL){ token = strtok(buffer, s); while( token != NULL && token[0]!=13) { token[strlen(token)]='\0'; *(lesMots + ligne)=strdup(token); ligne++; token = strtok(NULL, s); } } I'm trying to reallocate the memory of...

How to realloc based on size of array?

c,arrays,pointers,realloc
Say you malloc enough memory space to hold an array of size 20. The program is running and now I need enough memory for an array of size say 40. I tried to do this using realloc but it doesn't seem to be working. My code is the following(I'm trying...