Need help with dynamic memory allocation (malloc) in C 
Author Message
 Need help with dynamic memory allocation (malloc) in C

Hello there,

I thought I understood the way dynamic memory allocation and pointers worked
in C, but obviously not as my program keeps crashing.

Here is the scenario... In my program, I need to manipulate two arrays of
floats at the same time. The values in the second array are altered based on
the data in the first. After I have processed the arrays, I want to get rid of
the first array data, make the first array point to what was the second array,
then load in fresh values for the second array. Now I do the processing step
again. This is repeated until all the data is processed.

Here is the way I am trying to do this (just the relevant lines are shown):

float *line1ptr, *line2ptr;

line1ptr = (float *) malloc(ArrayWidth * sizeof(float));

line2ptr = (float *) malloc(ArrayWidth * sizeof(float));

/*I now load and process the data in the arrays */

PROCESSING STEP

free(line1ptr);         /* free the unwanted data */

line1ptr = line2ptr;            /* make 1st point to what was 2nd */

line2ptr = (float *) malloc(ArrayWidth * sizeof(float));

/*I now load data into the 2nd array and process the arrays */

PROCESSING STEP

/* now attempt to loop, repeating the whole process */

free(line1ptr);         /*porgram dies here */

line1ptr = line2ptr;

line2ptr = (float *) malloc(PixmapWidth * sizeof(float));

Does anyone know how to make this scenario work properly?

Any help would be greatly appreciated,

Thanks,

Bill M.



Sat, 17 Nov 2001 03:00:00 GMT  
 Need help with dynamic memory allocation (malloc) in C

   float *line1ptr, *line2ptr;

   line1ptr = (float *) malloc(ArrayWidth * sizeof(float));

   line2ptr = (float *) malloc(ArrayWidth * sizeof(float));

Don't cast to float *.  If the compiler complains without the cast,
then either you're using C++, not C, or you forgot to #include
<stdlib.h> to get the declaration of malloc().  (Or you've got a
really weird compiler I suppose.)

   /*I now load and process the data in the arrays */

   PROCESSING STEP

   free(line1ptr);         /* free the unwanted data */

   line1ptr = line2ptr;            /* make 1st point to what was 2nd */

   line2ptr = (float *) malloc(ArrayWidth * sizeof(float));

Why are you allocating new space here?  What's wrong with just
swapping the two pointers around, like so:

{
  float *t = line1ptr;
  line1ptr = line2ptr;
  line2ptr = t;

Quote:
}

   /*I now load data into the 2nd array and process the arrays */

   PROCESSING STEP

   /* now attempt to loop, repeating the whole process */

   free(line1ptr);         /*porgram dies here */

   line1ptr = line2ptr;

   line2ptr = (float *) malloc(PixmapWidth * sizeof(float));

I don't see anything inherently fatal in what you're doing.  It's most
likely you wrote before the beginning or past the end of one of the
arrays.

--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz



Sat, 17 Nov 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Help needed with Dynamic Memory Allocation

2. dynamic memory allocation performance problems with malloc/free

3. Help of Malloc--problem of memory allocation

4. Help Please!(linked lists/dynamic memory allocation etc.)

5. Two dimensional dynamic allocation using malloc

6. Problem with malloc, heap space, unix and scientific C with dynamic allocation

7. Help needed with Turbo C++ 1.0 memory allocation

8. Need help with memory allocation

9. Help: Need Memory Allocation Code to Use with MSC

10. I need help in fast memory allocation for ATL grid control

11. Help needed with memory allocation in C

12. Tree memory allocation, pool memory allocation

 

 
Powered by phpBB® Forum Software