Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
0
votes
2 answers

C realloc not changing apparent size of character array

When I run the below code, I get the given output. #include /* printf */ #include /* malloc, realloc */ int main() { char* characters = (char *) malloc(10 * sizeof(char)); printf("Before: characters at %p, size=%lu\n",…
sredmond
  • 460
  • 3
  • 15
0
votes
1 answer

Reallocating memory in C

I Think I am having a problem with reallocing the token character pointer, please help. i googged how to realloc but I am not sure if this is the right way to do it because I am getting MEMORY CORRUPTION error when I run my program. char* token =…
user3100209
  • 357
  • 1
  • 4
  • 17
0
votes
1 answer

Realloc is messing up the tree created by tsearch

I'm using the tsearch/tfind/twalk functions from the search.h library in a program that basically sorts and counts unique words in a document. Since I don't know how many words are in a document, I'm using malloc to assign a certain amount of memory…
HamHamJ
  • 435
  • 2
  • 10
0
votes
2 answers

Unable to extend array using realloc: "invalid next size"

I seem to have trouble in extending an array using realloc...please help me out here's the code : main() { int resultarray[] = {1}, i = 0 ,ch ; int *number = malloc(sizeof(int)); printf("Enter number : "); while ( ch = getchar()…
0
votes
1 answer

why is realloc() crashing after the 2nd loop?

this is just a test code i've been using to figure out how the code works before impelmenting it into my program. basically the same issue happens in both programs, im able to realloc 2 times then it crashes. the final project is a 2d pointer,…
0
votes
1 answer

Some malloc() sequentially and then a realloc for one of these???

I'm writing a short program where I need to first malloc an array of structure, and if asked by user to malloc others, the allocation give them consequentially addresses (i've prooved this), BUT when i realloc() one of them the reallocation invades…
0
votes
1 answer

Reallocating same array seems to not work

This is the code snippet, it belongs to a bigger project. Basicly it has to store and print how many times a character appears in a block of text. What i dont really seem to understand is how to reallocate the same array.... void…
sorin.va
  • 41
  • 5
0
votes
2 answers

Realloc array of Strings in C? segmentation error

#include #include #include void sortString(const char* input, char* output); int cmpstr(void const *a,void const *b); int readAllWords(FILE* f, char*** res, int * num_read); int main (int argc, char ** argv) { char…
user3213348
  • 255
  • 1
  • 5
  • 14
0
votes
2 answers

realloc causes glibc error for array of pointers

I have some C code that contains an array of text that I'm trying to manipulate in the following manner :- Allocate an array of pointers dictionary of size dictionary_size initialized to 50 Replace all spaces and \n's with '\0' Store the address of…
user277465
0
votes
1 answer

C - memory realloc error

I'm new to programming and here's a program that can find out all the prime numbers within a limit. However, there is an error that says: Prime 2(1682,0x7fff76316310) malloc: *** error for object 0x1002000a0: incorrect checksum for freed object -…
user3181273
0
votes
2 answers

Why this realloc inside a function fails to execute with Intel compiler?

Shown below is a piece of code written in C with an intention of reallocating memory inside a function. I would like to know why this crashes during execution and also an efficient way to do it. int main() { int *kn_row, *kn_col,…
Stoka
  • 39
  • 7
0
votes
2 answers

Malloc Realloc Free

Hello I for an exercise at University i need to malloc an array with this way. The array at stars has 1 slot. If the inputs are more than one then the array is doubled. If the inputs are more than 2 then it is doubled again etc. After this I have to…
JmRag
  • 1,443
  • 7
  • 19
  • 56
0
votes
3 answers

Realloc allocation in C

Hello I try to understand how realloc works so here is my question: Let's say that first we call malloc in order to allocate enough memory for 1 int. int *p=malloc(sizeof(int)) then we call realloc like this: p=realloc(p,sizeof(int)*2); The…
JmRag
  • 1,443
  • 7
  • 19
  • 56
0
votes
6 answers

C: realloc() not functioning as expected

If I have a variable, str that I would like to allocate memory to on the heap, I would use malloc() like: char* str = (char*)malloc(sizeof("Hello")); malloc() is returning a void* pointer, which is the memory location where my memory is. So now, I…
user1998032
0
votes
2 answers

Using malloc() and realloc() to store strings in an Array

getWordsArray() gets a pointer to a char array - the input. Im trying to split that input and store each word in a char array. And eventually return that char array. char *getWordsArray(char *input) { char *token; char *search…
Yevgeni
  • 1,533
  • 17
  • 32