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

BST with strings

I am trying to modify a BST to work with strings from the model in my book that works with integers. I am having a hard time with the modifying because of mallocing spacing for the string (i believe). I tested the code and it goes the proper…
0
votes
4 answers

Modify string address inside of function

I have string in main() char *string = NULL; Then I have a function foo(char *s){ s = realloc( ... ); .. } what I want to do is reallocate memory for string if its not long enough, so when I access this function in main() do i have to put…
lllook
  • 729
  • 2
  • 7
  • 20
0
votes
5 answers

Significance of double pointer and triple pointer in this code snippet

#include #include void add(char **p); void print(char **p); int cnt=0; main() { int option; char **p=NULL; while(1) { printf("------MENU-----\n"); printf("1>input\n 2>print\n3>exit\n"); printf("enter ur choice\n"); …
Jhansi Rani
  • 449
  • 1
  • 5
  • 11
0
votes
2 answers

Reallocating memory and adding a string at the reallocated memory space in C

I am having trouble adding "records" at the end of a dynamically allocated string array. Before reallocating more memory for the records to be added, everything works fine, and then I basically replicate what I initially did but now with realloc.…
Kenshin
  • 177
  • 1
  • 9
0
votes
2 answers

Subscripted value not array nor pointer

I have a program that reads a 2d array from a file, and makes it a jagged array (where each row is sized perfectly to fit all non-zero elements). Then it prints the array out. But I have a couple issues I can't figure out. Specifically 26: I get a…
cclloyd
  • 8,171
  • 16
  • 57
  • 104
0
votes
2 answers

free()ing multiple times if malloc() and realloc() was called multiple times in a loop

Let's say I have a fragment of code that contains this s = strtok_r(buffer, " \t\n", &saveptr); do { inStr = (char*)malloc(strlen(s)+1); (void) strncpy(inStr, s, strlen(s)+1); …
maregor
  • 777
  • 9
  • 32
0
votes
2 answers

realloc returns NULL after some time while allocating small (<500Kb) data block; there is enoug memory

Hi! The short question is: what can be the problem? The overall memory usage of my program (shown by task manager) is almost the same all the time (near 40 minutes) it's running, and I have near 2G more free memory. Running on win2003r2. Memory…
zxcat
  • 2,054
  • 3
  • 26
  • 40
0
votes
1 answer

Dynamic memory allocation with char

I'm trying to allocate memory only if i need it for the next while. char *str = malloc(sizeof(char)); int i = 0; while(something == true){ str[i] = fgetc(fp); str = realloc(str, strlen(str)+1); i++; } free(str); But for some reason…
Christopher
  • 3,379
  • 7
  • 25
  • 36
0
votes
0 answers

Reallocating memory - can't understand the output

I am trying to create a dynamic array, bu the reallocation fails on the third try. Below you can see the code and the output of it. As seen, the third attempt puts strange integers in the array and then the reallocation fails. Where am I…
Michael Sazonov
  • 1,531
  • 11
  • 21
0
votes
5 answers

Calloc & realloc: Error in `./a.out': free(): invalid next size (normal)

I have a little piece of code (in C) where I'am allocating an array and scaning numbers in it. If the array is too small I'm reallocating memory for my array. Sometimes it works fine but sometimes it returns "Error in `./a.out': free(): invalid next…
Nash
  • 493
  • 1
  • 4
  • 7
0
votes
2 answers

Realloc 2D and functions

I want to realloc a 2D matrix in a different function from main but unfortunately, I get an invalid pointer error. I have read other answers but I can't see where is my error. My code is: void example(double* w, size_t s){ …
Learning from masters
  • 2,032
  • 3
  • 29
  • 42
0
votes
2 answers

How to resize array of integers?

I've been looking at other answers of similar nature here but still run into problems. I am a beginner in C and need some advice. Relevant parts of code: int readNumbers(int **array, char* fname, int hexFlag) { int numberRead = 0; FILE*…
Ansdai
  • 47
  • 9
0
votes
2 answers

C realloc() char ** "invalid next size" error

int parseString(char* input, char*** words, int* size) { char *word; *words = malloc(sizeof(char)); word = strtok(input, " \n\t"); while (word != NULL) { (*words)[*size] = malloc(sizeof(word) + 1); …
bogconst
  • 33
  • 1
  • 7
0
votes
2 answers

realloc() seems to affect already allocated memory

I am experiencing an issue where the invocation of realloc seems to modify the contents of another string, keyfile. It's supposed to run through a null-terminated char* (keyfile), which contains just above 500 characters. The problem, however, is…
krystah
  • 3,587
  • 2
  • 25
  • 43
0
votes
1 answer

Dynamically Readjustable Arrays and OpenMP

I have a function that uses realloc to dynamically adjust the memory of a 1D array as the initial size of the array cannot be predetermined .I want to parallelize this code by dividing the task across multiple threads whereby each thread would work…