473,480 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Repeated realloc causes crash in glibc

2 New Member
Here is the code snippet, realloc succeeds once but fails the next time. I use gcc 4.1.2, glibc 2.5.

Expand|Select|Wrap|Line Numbers
  1. typedef struct array
  2. {
  3.   double *x, *y;
  4.   size_t nelem, size;
  5. } array_t;
  6.  
  7. #define INIT_A_SIZE 1
  8. int init_a(array_t *a)
  9. {
  10.   if (a) {
  11.     a->x = (double *)malloc(INIT_A_SIZE*sizeof(double));
  12.     a->y = (double *)malloc(INIT_A_SIZE*sizeof(double));
  13.     if (!a->x || !a->y) {
  14.       /* technically I should free x here is y fails
  15.          but that's not the issue */
  16.       return -1;
  17.     }
  18.     a->nelem = 0;
  19.     a->size = INIT_A_SIZE;
  20.   }
  21.   return 0;
  22. }
  23.  
  24. int add_a(array_t *a, double x, double y)
  25. {
  26.   if (a->nelem == a->size) {
  27.     size_t newsize;
  28.     double *newx, *newy;
  29.     newsize = 2*a->size;
  30.     newx = realloc(a->x, newsize);
  31.     if (!newx) {
  32.       return -2;
  33.     }
  34.     newy = realloc(a->y, newsize);
  35.     if (!newy) {
  36.       a->x = realloc(a->x, a->size);
  37.       return -3;
  38.     }
  39.     a->size = newsize;
  40.     a->x = newx;
  41.     a->y = newy;
  42.   }
  43.   a->x[a->nelem] = x;
  44.   a->y[a->nelem] = y;  
  45.   a->nelem++;
  46.   return 0;
  47. }


This has come up before, and I went through the threads, but (as far as I can tell) my situation is different than any previous posters, because unlike their code, this code:
1. assigns reallocated pointer to original (a->x = newx)
2. does not free anything
3. checks if newx or newy are NULL
Jul 16 '10 #1
3 2752
Bold Gambit
2 New Member
@Bold Gambit
Mistake in cut-and-paste, but not in original code: Move a->nelem++ down after a->y[a->nelem] = y;
Sorry about that.
Jul 16 '10 #2
Banfa
9,065 Recognized Expert Moderator Expert
OK to start will you have made this more complex than required by not coding the relationship between the data into the program.

That is there is a direct correlation between x and y in you array structure, they always have the same number of elements. Therefore they should not be separate arrays, create a type to embody this relationship and then create an array of that type like so

Expand|Select|Wrap|Line Numbers
  1. typedef struct data
  2. {
  3.   double x;
  4.   double y;
  5. } data_t;
  6.  
  7. typedef struct array
  8. {
  9.   data_t *data;
  10.   size_t nelem, size;
  11. } array_t;
  12.  
You halve the number of mallocs/reallocs and you do not have to handle allocation of y failing when x has already been allocated. Generally the code becomes less complex and that means more maintainable.

I can't see anything particularly wrong with the but I am just trying it.
Jul 16 '10 #3
Banfa
9,065 Recognized Expert Moderator Expert
Line 36 is wrong, byt this time a->x is not valid you need to reallocate from newx

Line 30 and 34 you are allocating the number of elements but not factoring in the size of the data type (double).
Jul 16 '10 #4

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

Similar topics

0
1548
by: Kevin Nelson | last post by:
Has anyone experienced this before. I have a web project/application. One of the files in this application is a SQL Script file. When I use Ctrl+A to select all of the text of the file VS Net...
1
1280
by: Norvin Laudon | last post by:
Hi, I've developed a C# app for internal use on our LAN. I've deployed the app with a setup project. The problem is, when I make updates to the exe, I don't want to reinstall the app on...
10
3470
by: Alex Vinokur | last post by:
GNU g++ 3.3.3, Cygwin // Stuff static char* mbuffer = NULL; // Stuff void doit()
1
1613
by: John C | last post by:
Importing DBase files I get to the screen titled "Select Source Table and Views" And none appear in the list. (I can get the list displayed about once in ten tries) From that screen anything causes...
1
2120
by: Tim Marshall | last post by:
Given a bound continuous subform in an unbound main form, a button on the subform sets allowadditions = true and dataentry = true. This works no problem. But a second button on the subform...
2
3191
by: Composer | last post by:
One form in my Access 2000 mdb causes a crash whenever I close it. The crash is of the type where I get a "Please tell Microsoft about this problem". Easy, you think! Well, here are the things...
1
3288
by: Kurt Smith | last post by:
We are in the process of creating a managed C++ library to wrap some existing unmanaged code. The unmanaged code makes use of the STL. We are seeing NULL pointer exceptions whenever we construct...
1
2113
by: s_schutte | last post by:
Here's my situation. I have an unmanaged C++ project that I am using to interface with the Lotus Notes C API. I then have a managed C++ project in the same solution that functions as a "C++...
1
1253
by: keith.halligan | last post by:
Hi, I've tried converting the current project I'm working on into Release Mode. It keeps crashing on the vftable() throwing an memory exception. The function stack looks like:...
3
2361
by: Typehigh | last post by:
I am front ending a SQL Server database with MS-Access 2000. The UI has many buttons that launch VB code. One of the buttons launches a lengthy process that takes 20-30 seconds to complete. If the...
0
7048
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
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6743
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
5344
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,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.