Malloc question

E

Eyegor

I have a question about malloc function.

I have a function AEI_malloc() which basically returns a pointer to an
array that it malloced.
I have the following code:
A=AEI_malloc();
B=AEI_malloc();
C=AEI_malloc();

where A, B and C are properly defined pointers. This code compiles and
runs on my machine. But if I try to compile it and run it on a
different machine, it still compiles ok but i get a memory error
running it. I solved the problem by doing this:

A=AEI_malloc();
/*Do some stuff like cycle through A and assign it all 0s. A is 1024
element array*/
B=AEI_malloc();
/*Do some stuff like cycle through B and assign it all 0s. B is 1024
element array*/
C=AEI_malloc();

So basically I add a "pause" between malloc function calls.

Is there a reason why this would happen? I do check for malloc failure
inside of AEI_malloc() function, so in 1st case AEI_malloc for B fails
on B, its not that it failed on A and then B...

AEI_malloc() basically looks up some predefined constants and sets up
some default values for the allocated space. A, B, and C are pointers
to structures with array and 2 integers, so AEI_malloc() sets up
values of the integer portions of the structure which was allocated
and pointer to which gets returned by AEI_malloc()

Thanks ahead.
 
I

Ian Collins

I have a question about malloc function.

I have a function AEI_malloc() which basically returns a pointer to an
array that it malloced.
I have the following code:
A=AEI_malloc();
B=AEI_malloc();
C=AEI_malloc();

where A, B and C are properly defined pointers. This code compiles and
runs on my machine. But if I try to compile it and run it on a
different machine, it still compiles ok but i get a memory error
running it. I solved the problem by doing this:

A=AEI_malloc();
/*Do some stuff like cycle through A and assign it all 0s. A is 1024
element array*/
B=AEI_malloc();
/*Do some stuff like cycle through B and assign it all 0s. B is 1024
element array*/
C=AEI_malloc();

So basically I add a "pause" between malloc function calls.

You undoubtedly have a bug in the code you haven't posted.
 
M

Malcolm McLean

So basically I add a "pause" between malloc function calls.

Is there a reason why this would happen?
Undefined behaviour. If the bug evokes undefined behaviour, anything
can happen. Different results on adding diagnostic code or delays is a
very common consequence.
 
T

Tom St Denis

Undefined behaviour. If the bug evokes undefined behaviour, anything
can happen. Different results on adding diagnostic code or delays is a
very common consequence.

Usually as a result of stack corruption [in my experience].

Tom
 
D

David Resnick

I have a question about malloc function.

I have a function AEI_malloc() which basically returns a pointer to an
array that it malloced.
I have the following code:
A=AEI_malloc();
B=AEI_malloc();
C=AEI_malloc();

where A, B and C are properly defined pointers. This code compiles and
runs on my machine. But if I try to compile it and run it on a
different machine, it still compiles ok but i get a memory error
running it. I solved the problem by doing this:

A=AEI_malloc();
/*Do some stuff like cycle through A and assign it all 0s. A is 1024
element array*/
B=AEI_malloc();
/*Do some stuff like cycle through B and assign it all 0s. B is 1024
element array*/
C=AEI_malloc();

So basically I add a "pause" between malloc function calls.

Is there a reason why this would happen? I do check for malloc failure
inside of AEI_malloc() function, so in 1st case AEI_malloc for B fails
on B, its not that it failed on A and then B...

AEI_malloc() basically looks up some predefined constants and sets up
some default values for the allocated space. A, B, and C are pointers
to structures with array and 2 integers, so AEI_malloc() sets up
values of the integer portions of the structure which was allocated
and pointer to which gets returned by AEI_malloc()

Thanks ahead.

I'm guessing something wrong with your structures and you are
scribbling over some memory. If you are on a system with memory
checking tools of any sort (e.g. linux with valgrind) available I'd
suggest using those. If not, you could put a sentinel at the end of
each struct, fill it in during allocation, and see if it is
overwritten...

-David
 
B

BartC

Eyegor said:
I have a question about malloc function.

I have a function AEI_malloc() which basically returns a pointer to an
array that it malloced.
I have the following code:
A=AEI_malloc();
B=AEI_malloc();
C=AEI_malloc();

where A, B and C are properly defined pointers. This code compiles and
runs on my machine. But if I try to compile it and run it on a
different machine, it still compiles ok but i get a memory error
running it. I solved the problem by doing this:

A=AEI_malloc();
/*Do some stuff like cycle through A and assign it all 0s. A is 1024
element array*/
B=AEI_malloc();
/*Do some stuff like cycle through B and assign it all 0s. B is 1024
element array*/
C=AEI_malloc();

So basically I add a "pause" between malloc function calls.

Is there a reason why this would happen? I do check for malloc failure
inside of AEI_malloc() function, so in 1st case AEI_malloc for B fails
on B, its not that it failed on A and then B...

AEI_malloc() basically looks up some predefined constants and sets up
some default values for the allocated space. A, B, and C are pointers
to structures with array and 2 integers, so AEI_malloc() sets up
values of the integer portions of the structure which was allocated
and pointer to which gets returned by AEI_malloc()

You say above that A is a 1024-element array.

Here you say that A is a pointer to structure (or structures?) with an
embedded array, and 2 integers. And that the integers (and array?) are
initialised with data.

The code above also seems to clear the data, including the 2 integers (?)
and the array initialisation data? And the program still works. Or do the
1024 elements fall short of the 2 integers? It's not very clear.

Perhaps post details of the struct definition and exactly which parts of it
are initialised. (If the array is not initialised, then perhaps some bug
elsewhere could be making use of the uninitialised data, with results that
will very, ie sometimes it works, sometimes it doesn't...)
 
E

Eyegor

Here are 3 files for my AEI library:
http://magicmrv.com/codes/AEI/main.c
http://magicmrv.com/codes/AEI/aei.c
http://magicmrv.com/codes/AEI/aei.h

I declare A, B and C as pointers to AEI. AEI is a structure defined in
header file. I allocate space to A by calling AEI_malloc().
If I call AEI_malloc() again right after to allocate space for B the
executable crashes, but it only crashes on a laptop, it does not crash
on my desktop. At laptop has windows XP, and desktop has winVista.

Quick explanation of code:
AEI is a structure:
typedef struct AEI { /*Array encoded integer typedef*/
AEI_unit val[AEI_size]; /*Vector of digits*/
AEI_sd_counter_type sd; /*Number of significant digits*/
char sign; } AEI; /*Sign 0 = positive 1 = negative*/

AEI_malloc() allocates memory to store AEI variable, zeros out AEI-
val[] and zeros out AEI->sd a well as AEI->sign. This way
AEI_malloc() returns a pointer to a positive zero stored as an Array
Encoded Integer.

In the header file #define AEI_unit_max 255 is used to control
overflow for each element in array, it can be up to 2^32-1 since I use
32-bit elements for array, i keep it at 255 for now for debugging
purposes.

What bugs me is that code works on desktop with 8gb of ram, and it
works on laptop with 2gb of ram, but it only works on laptop if i add
an effective "pause" between malloc statements in main.c

Thanks
 
E

Eyegor

The reason I mention RAM amounts on desktop and laptop is to show that
there is plenty of free ram. I realize that this code does not take up
over a meg or so... probably even less.
 
B

BartC

Eyegor said:

(It must be a bad bug: my IE8 crashes just trying to open that link!)
http://magicmrv.com/codes/AEI/aei.h

I declare A, B and C as pointers to AEI. AEI is a structure defined in
header file. I allocate space to A by calling AEI_malloc().
If I call AEI_malloc() again right after to allocate space for B the
executable crashes, but it only crashes on a laptop, it does not crash
on my desktop. At laptop has windows XP, and desktop has winVista.

Quick explanation of code:
AEI is a structure:
typedef struct AEI { /*Array encoded integer typedef*/
AEI_unit val[AEI_size]; /*Vector of digits*/
AEI_sd_counter_type sd; /*Number of significant digits*/
char sign; } AEI; /*Sign 0 = positive 1 = negative*/

AEI_malloc() allocates memory to store AEI variable, zeros out AEI-
val[] and zeros out AEI->sd a well as AEI->sign. This way
AEI_malloc() returns a pointer to a positive zero stored as an Array
Encoded Integer.

You appear to have some mixups in your allocations.

Your calls to malloc() allocate 8197 bytes. But the size of a single AEI
struct is 8208 bytes.

That is, AEI consists of 2048 x 4-byte array (8192) bytes, plus your counter
type (8 bytes) plus a char, total 8201 bytes, but, on my lccwin at least, is
rounded up to 8208 bytes.

Your calculation for malloc is 2048x4 bytes (check), 4 bytes for unsigned
long int (wrong), and 1 byte for the char (wrong). Just use sizeof (AEI).
 
E

Eyegor


(It must be a bad bug: my IE8 crashes just trying to open that link!)


I declare A, B and C as pointers to AEI. AEI is a structure defined in
header file. I allocate space to A by calling AEI_malloc().
If I call AEI_malloc() again right after to allocate space for B the
executable crashes, but it only crashes on a laptop, it does not crash
on my desktop. At laptop has windows XP, and desktop has winVista.
Quick explanation of code:
AEI is a structure:
typedef struct AEI {             /*Array encoded integer typedef*/
AEI_unit val[AEI_size];      /*Vector of digits*/
AEI_sd_counter_type sd;     /*Number of significant digits*/
   char sign; } AEI;            /*Sign 0 = positive 1 = negative*/
AEI_malloc() allocates memory to store AEI variable, zeros out AEI-
val[] and zeros out AEI->sd a well as AEI->sign. This way
AEI_malloc() returns a pointer to a positive zero stored as an Array
Encoded Integer.

You appear to have some mixups in your allocations.

Your calls to malloc() allocate 8197 bytes. But the size of a single AEI
struct is 8208 bytes.

That is, AEI consists of 2048 x 4-byte array (8192) bytes, plus your counter
type (8 bytes) plus a char, total 8201 bytes, but, on my lccwin at least, is
rounded up to 8208 bytes.

Your calculation for malloc is 2048x4 bytes (check), 4 bytes for unsigned
long int (wrong), and 1 byte for the char (wrong). Just use sizeof (AEI).

You are infinitely right and I am an idiot. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top