473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Realloc error

Hello,
I have a code which does malloc and does realloc,but whenever i free i get the fllowing error:
*** glibc detected *** free(): invalid next size (fast): 0x0804a008 ***

the code is as below:

short* p;
short i;
p = (short*)calloc(10, 2);
for(i = 0; i < 10; i++){
p[i] = i;
}
p = (short*)realloc(p, 20);
for(i = 10; i < 20; i++){
p[i] = i;
}
for(i = 0; i < 20; i++){
printf("%d\n", p[i]);
}
free(p);
return 0;

whats wrong in my code???
Aug 13 '08 #1
1 1746
arnaudk
424 256MB
As a tip, use calloc(10,sizeof(short) ) instead of calloc(10,2) because a short might not have the same size on every system, and it makes your code more readable.

The problem is that your reallocated block of memory is too small, use realloc(p, 20*sizeof(short) ); only then does it make sense to assign to p[10] and upwards.

Please use [code]...[/code] tags around your code.
Aug 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
16
by: Eric A. Johnson | last post by:
Hi, I'm going through a book I got from the library. Admittedly, the book is old -- it hails from 1994, and much has changed since then. However, I would like to learn as much as possible from...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
4
by: alex323 | last post by:
Hey. I must have an array that can be resized dynamically. I have coded an implementation of it using malloc/realloc, but I am getting a runtime error as seen below in GDB: *** glibc detected...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
7
by: Jonathan Shan | last post by:
Hello all, I am trying to run a program which has dynamic array of type struct. The program works until the line which uses realloc function to allocate more memory. I have tried to reproduce...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
10
by: Igal | last post by:
hay, i'm doing this program. having problem wiht realloc in the function that reads data structures into array (pointer - bp2), this happens after reading the second record. when call to realloc....
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.