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

Problems with realloc in C

I'm struggling with realloc... strucType mkBggr (structType x, char ch) { x = realloc(x, 100); printf("%d", sizeof(x)); } I'm thinking this should print out the value 100, but it prints 8. It obviously has something to do with pointers,…
user1341676
0
votes
1 answer

Realloc function seems to not work! This program is from a textbook

My code (or the example from the book) seems to not work. I know it's the realloc function causing this, since I will allocate some memory to a set amount at the beginning. If I surpass the original amount I allocated, I will realloc() to increase…
0
votes
1 answer

*** glibc detected *** ./333: realloc(): invalid next size: 0x0804b008 ***

#include #include #define ALFA 4 int simVarDiscr(int* nr, int l, int nrm) { int k = 0, i = 1; double p, q; q = (double)ALFA / (ALFA + l - 1); p = (double)nr[1] / (ALFA + l - 1); double F = p; double u =…
user3623498
  • 69
  • 1
  • 1
  • 8
0
votes
1 answer

Reallocating 2d char array in c

I have a problem with reallocating an array. I want to save inputs to a string array and realloc it with every new entry. Heres my function: char** history=0; int historycounter=0; void saveHistory(char* input){ …
slyr
  • 1
  • 3
0
votes
1 answer

Struct Memory Allocation from File in C, only two variables work

I have to write a program that will read text from a file, break it up into a struct, validate the sections to a certain criteria, then produce two new files; one with the clean data and one with the errors. So far i am up to the stage of breaking…
0
votes
2 answers

Doubling Dynamic Stack Array

I have an array used to represent a generic stack. struct Stack { int size; int type; int capacity; void **data; // An array of generic data. }; Stack *new_stack(int type) { Stack *tmp = malloc(sizeof(Stack)); assert(tmp !=…
Mickey
  • 117
  • 1
  • 10
0
votes
2 answers

Realloc pollfd - conversion to non-scalar

I'm using the poll function. The library has a structure as so: struct pollfd{ int fd; short events; short revents; } Now, in my code I have an array of these events and I need to be able to realloc and give more memory space for more…
jdepypere
  • 3,453
  • 6
  • 54
  • 84
0
votes
2 answers

Using realloc for array of structs

I'm using realloc to increase the amount of memory for an array of structs and it doesn't seem to be increasing at all. I've read all of the posts relating to this and I can't figure out what I'm doing wrong. Here is my code: struct fileInfo…
Ryan McClure
  • 1,183
  • 2
  • 17
  • 34
0
votes
1 answer

Issue with realloc function on dynamic array of linked-lists based graph

currently working on some computer science exercises, when I ran into a weird issue. The idea of the exercise is to create and work on a graph using first dynamic matrices (no issues there) and later an array of linked lists. The exercise asks to…
Eloh666
  • 23
  • 1
  • 6
0
votes
3 answers

realloc() is wasting a lof of space, what did I do wrong?

So, I was doing this exercise: Write a C function void occurrences(char* s, char c, char*** occp, int* n) that, given a string s and a char c, counts the number of occurrences of char c in the string s, returns that number in n and returns in…
Atlas80b
  • 119
  • 1
  • 2
  • 8
0
votes
4 answers

Why isn't realloc shrinking the array?

I have trouble reducing the size of dynamically created array. Here's what my main function looks like: int main(void) { // Intialize big array int * a = (int *)malloc(10*sizeof(int)); assert(a); // Fill it with squares for (int…
vaultah
  • 44,105
  • 12
  • 114
  • 143
0
votes
2 answers

reading an input(int) and storing it into an array with malloc and realloc

I'm trying to read ints from stdin, but i don't know the length. I tried this but I have no idea why it doesn't work #include #include int main() { int *arr = (int *) malloc(sizeof(int)); int sizeCounter = 0; int…
jabk
  • 1,388
  • 4
  • 25
  • 43
0
votes
1 answer

*** glibc detected *** ./all: realloc(): invalid next size: 0x0804b008 ***

I'm trying to copy an array of integers in a dynamically allocated array. At first it has size 1 and with every element I want to increase it's size by one. Here's the code: #include #include int main() { int *nr,i; …
user3623498
  • 69
  • 1
  • 1
  • 8
0
votes
1 answer

How to reallocate memory for a read-only string?

I know this use-case might sound a bit strange, but I need to understand if it is possible to do something similar to this. This is my code, and it causes crash on Aborted (core dumped): char *my_str = "Hello World"; my_str = realloc(my_str,…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
0
votes
2 answers

program crashing when trying to add to a dynamic array

I am trying to write a function that will add a set of data (first name, last name, score) into 3 different dynamic arrays (one 2d char array for the first name, one 2d char array for the the last name, and a float array for the score). Here is my…