Questions tagged [realloc]

C library function for reallocating a dynamically allocated memory region.

man page

Wikipedia

References

Related Tags

1717 questions
0
votes
1 answer

Dynamically allocating memory for three dimensional array

I don't get it. I am looking since hours on following function and don't get why I get a memory fault when walking through the array in the end. If I don't walk through it, it runs without error. The function should split the input string first by…
timmornYE
  • 708
  • 2
  • 8
  • 22
0
votes
2 answers

Best way to realloc memory when adding substrings to a string in C

I have: char *data = malloc(file_size+1); I also have strings such as char *string = "string1". I want to be able to realloc memory to data whenever I'm about to add another string to data. What would be the best way to do this in C, I've seen…
Nassim
  • 301
  • 3
  • 11
0
votes
2 answers

finding reoccuring string in I/O stream -C?

I'm quite new to C. I'm trying to write a code that finds a string in a I/O stream, and I don't understand what I'm doing wrong. I know the error is probably in the large while loop (in the code below). I want the function to return the location in…
jilin
  • 3
  • 1
0
votes
1 answer

realloc() fails when passing pointer to char *. Why? (Because it was by-value, not reference!)

EDIT Thanks Joachim. Used the function signature as you pointed out, and passed the address of my strings in. Inside the function, I did the realloc() using *currBuffer, and placed the values into the string using (*currBuffer)[lenBuffer] ....…
gone
  • 1,079
  • 5
  • 13
  • 31
0
votes
2 answers

Realloc overwriting contents of array?

I'm trying to read a file into an array of strings using getline() and realloc(). I have used very similar code in the past for tokenizing strings and everything worked correctly. I'll consider the sample input file as 1 2 3 4 5 Here's the…
wdonahoe
  • 1,043
  • 1
  • 10
  • 22
0
votes
1 answer

C CrtIsValidHeapPointer Error

Im developing a C code to do a GET request and handle variable size response. I use realloc to resize the heap chunk where i am storing the response. The problem is that i am getting an error related with the CrtIsValidHeapPointer function of the…
Alberto
  • 701
  • 4
  • 9
  • 25
0
votes
2 answers

dealing with pointers in realloc?

Please look at the following code char *line = (char *) malloc(100); char *newline,*source = line; int size=100; newline = realloc ( line , size +=size); // assuming that newline has been successfully assigned the demanded memory and line is …
OldSchool
  • 2,123
  • 4
  • 23
  • 45
0
votes
3 answers

realloc() unable to reallocate memory

i am writing a file copy program,in which i faced difficulty regarding realloc(). Please look at the following snippet (which i write to understand the working of realloc()):- int main(){ char *p =(char *) malloc ( 10 ),*t; p = "this is"; …
OldSchool
  • 2,123
  • 4
  • 23
  • 45
0
votes
3 answers

Dynamic allocation of a struct in C

I have a char* within a "news" struct like this: typedef struct news{ char *name; }news; I read from a file some text (in example I have ever the same line:hello). For the main and my function i wrote this code: int insert(news **note, char…
Pascal NoPascensor
  • 171
  • 1
  • 1
  • 14
0
votes
1 answer

Realloc of structures

typedef struct dvdtype{ int dvdcode; char title[50]; int customerID; int daysowned; }dvdtype; typedef struct dvdstruct{ dvdtype *dvd; int numdvds; }dvdstruct; void initDvds(dvdstruct *dvds); int displayMainMenu(); void…
0
votes
2 answers

How to reallocate array of pointers in class?

I have class Citizen { string name,address ... } and then I have class TaxRegister. class TaxRegister { public: bool Add_citizen ( const string& name, const string& addr ); private: static const int m_Size=1000; int…
0
votes
2 answers

Problems with realloc - "invalid next size"

In my program, I receive text from a .txt file. Here is an example of a line of text: 12X15 de3 ds4 dn9 g2,7 m5,9 m3,1 h2,2 I am trying to use strtok() to break up each chunk of text; I will then put each chunk as an element in an array. So an…
Plaidypus
  • 81
  • 9
0
votes
1 answer

glibc detected, realloc(): invalid old size C

Admittedly what I was trying to do was at best educated guess work. I have an array of strings, and I was trying to account for if someone entered a string that was too large, or the array received too much input (it needs to be dynamic for what I'm…
noneabove
  • 89
  • 2
  • 12
0
votes
2 answers

Reading a string of unknown length from user (works once and stops)

Here is my code: char* get_string() { #define MAX_STRING_LENGTH 1000 char *input=NULL; char buffer[MAX_STRING_LENGTH]; fgets(buffer,MAX_STRING_LENGTH,stdin); fflush(stdin); …
user34920
  • 227
  • 3
  • 6
  • 12
0
votes
1 answer

How to allocate memory for strings entered by a user in real time in C?

I need to get an unknown amount of strings from the user (via keyboard) and set an array of string pointers so that it will point to all the strings entered. I defined a variable char tmp_strng[] to hold the string the user enters using the…
user34920
  • 227
  • 3
  • 6
  • 12