Re: Allocation of a multidimensional array of structures
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 23 Jun 2007 21:24:03 -0700
On Sat, 23 Jun 2007 19:29:34 -0700, Michel Rouzic
<Michel0528@xxxxxxxx> wrote:
CBFalconer wrote:
Michel Rouzic wrote:
I've recently met issues with my program which can only be
explained by heap corruption, so I've tried debugging my program
with Valgrind, and here's what I get with the following
multidimensional array allocation code :
typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;
lut_entry ***lut; // <-global pointer
void lut_alloc()
{
Apart from the other comments, why are you using K&R C rather than
properly prototyped C functions?
In case you're refering to writing void lut_alloc(); and then void
lut_alloc(){...} I have yet to find out how it is better to do things
this way. But since there was a why in your question I guess it's
because it's simpler, it avoids a redundancy that I deem useless
(maybe out of ignorance?) and it avoid making mistakes by changing a
line and not changing the other one accordingly.
Or to turn the question around, why should I use that prototyping
thing?
If the call to lut_alloc is before the definition of the function, you
should have a prototype in scope to allow the compiler to check that
you are calling it correctly. In C99 it is mandatory; in C89 it is
just good practice and helps avoid undefined behavior.
After fixing that, consider
passing parameters rather than using globals.
Well, while I'm aware that it's better to avoid using globals, it's
really more convenient to make the LUT global here, considered it's
more convenient to write code this way and that it'd make one more
argument to pass from function to function, so I don't see why I
shouldn't use a global pointer here.
Convenience is good. Unfortunately that convenience comes at a price
which frequently makes it non-cost-effective. Global variables make
debugging much harder. Given the omissions in the code you have shown
us, this may (admittedly unlikely) be part of the problem you are
having.
Remove del for email
.
- Follow-Ups:
- Re: Allocation of a multidimensional array of structures
- From: Harald van Dijk
- Re: Allocation of a multidimensional array of structures
- References:
- Allocation of a multidimensional array of structures
- From: Michel Rouzic
- Re: Allocation of a multidimensional array of structures
- From: CBFalconer
- Re: Allocation of a multidimensional array of structures
- From: Michel Rouzic
- Allocation of a multidimensional array of structures
- Prev by Date: Re: I don't see where I'm clobbering the memory
- Next by Date: Re: about structures memory allocation
- Previous by thread: Re: Allocation of a multidimensional array of structures
- Next by thread: Re: Allocation of a multidimensional array of structures
- Index(es):
Relevant Pages
|