Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
4
votes
1 answer

How do I use use std::allocator in place of realloc?

Let's say I'm writing a custom vector using the std::allocator to wrap new and delete. When the number of elements exceed the vector's capacity, I would like to reallocate the buffer to something larger. I can achieve this easily by calling…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
4
votes
2 answers

Can realloc shrink my array on the left side (C only)?

I want to move a large chunk of data i've got in my memory. Unfortunately this data is saved as an array, and i cannot change that. I can't use circular arrays, because the same memory is also used by a couple of fortran methods i do not want to…
Nils_M
  • 1,062
  • 10
  • 24
4
votes
1 answer

Realloc() returning NULL when memory is available

I have a c++ program running on a Windows 7 machine with 12GB memory. The compiler and linker is Visual Studio 2013 Express. the program uses a library OGDF. I compiled the library source codes into a static library with Release X64 configuration…
user2218067
  • 155
  • 2
  • 13
4
votes
2 answers

read() fails with Bad address, valgrind shows Syscall param read(buf) points to unaddressable byte(s)

I have a function to read a file using the read() system call and return a char pointer with the data read from the file. The function reallocates space if necessary. After a specific point the read fails with the error "Bad Address". The minimum…
abhijat
  • 535
  • 6
  • 12
4
votes
6 answers

How to create extensible dynamic array in Java without using pre-made classes?

Yeah, it's a homework question, so givemetehkodezplsthx! :) Anyway, here's what I need to do: I need to have a class which will have among its attributes array of objects of another class. The proper way to do this in my opinion would be to use…
AndrejaKo
  • 1,721
  • 5
  • 25
  • 41
4
votes
2 answers

Reallocating array inside struct

typedef struct { int count; int *items; }set; set* set_alloc(set *src, int num); int set_insert(set *s, int num); int main() { set *A = NULL; A = set_alloc(A, 0); A = set_alloc(A, 1); //this and line below is part of inserting…
Poody
  • 325
  • 3
  • 13
4
votes
2 answers

Also check realloc() if shrinking allocated size of memory?

When you call realloc() you should check whether the function failed before assigning the returned pointer to the pointer passed as a parameter to the function... I've always followed this rule. Now is it necessary to follow this rule when you…
4
votes
2 answers

Is this use of realloc correct?

Original question Can I use realloc() function like the following code: int *ptr, i, num=5; for (i=0; i
synth
  • 65
  • 5
4
votes
3 answers

Realloc into a function

My question is about realloc. The following code works correctly (with no warning): #include #include int main () { int num=10; int *vet; int i; for (i=0; i
ᴜsᴇʀ
  • 1,109
  • 2
  • 9
  • 23
4
votes
1 answer

Converting an array to use malloc

Currently I have set up an array to hold 50 packets created by the program. However I would like to change this array to use malloc instead along with the use of storing a maximum of 50 pieces of information just as the current array does. Here's…
Funkii
  • 77
  • 4
4
votes
4 answers

C realloc() function fails

Why does this code not work? char *x=malloc(100); x++; x=realloc(x, 200); I mean x is a valid string pointer, just incremented by one?
user1598019
4
votes
1 answer

c++ vector inside a structure

I've got a doubt about using std::vector. If I got a structure with a std::vector object with nondefined space in the memory, if it need to be reallocate (the std::vector), is the structure also reallocated? or only the std::vector? For…
Bardo91
  • 585
  • 2
  • 7
  • 17
4
votes
2 answers

In C is it possible to shrink the allocated memory without moving it?

Is there a method/function that frees up memory without the possibility of moving it to a new pointer in c? Thanks!
user2662982
  • 237
  • 1
  • 2
  • 8
4
votes
3 answers

Dynamically expanding array using realloc

I have written following code to get clear with malloc and realloc. I have initialized pointer using malloc and then using realloc, i am increasing the size of the array. But i get following error when i run the…
ajay_t
  • 2,347
  • 7
  • 37
  • 62
4
votes
1 answer

Strings with Realloc in C

I am trying to use realloc function as making the array bigger as the user entered names. It gives me an error when I add 5. element, the error like : * glibc detected ./a.out: realloc(): invalid next size: 0x00000000017d2010 ** And the code…
Guray Yildirim
  • 321
  • 3
  • 7