Dynamic pointers

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Dynamic pointers

1,092 次查看
gdesmedt1
Contributor I

Hey all,

 

I come from a C++ computer programming background and would like to create a dynamic structure to save my data in. It needs to be dynamic as could have several a's in one info structure or only 1 a in several info structures... you get the idea.

 

ok.. here's example...

 

typedef struct
{
    int *a;

       
}info_struct;
info_struct *info;

 

now normally i'd simply say... "new info" and tadaa would have another struct to access... but this being limited language.. don't have that option.. or at least not part of my header info.

 

SO.. can anybody out there help me with this please... I'd like to eventually access the information in the followig way...

 

info[info_ptr].a[ptr];

 

or in simpler terms...

 

info[0].a[0];

 

doing it like below does work but does not make it dynamic...

 

typedef struct
{
    int a[10];

       
}info_struct;
info_struct info[10];

 

and then accessing it...  

 

 

info[info_ptr].a[ptr];

 

or in simpler terms...

 

info[0].a[0];

 

any help or explanation on what to do will be greatly appreciated... I am using a HS08 = MC9S08GT32A processor.

 

Thanks,

 

G :smileywink:

标签 (1)
0 项奖励
回复
1 回复

656 次查看
Lundin
Senior Contributor IV
This thread should answer the question:

http://forums.freescale.com/freescale/board/message?board.id=CW816COMM&thread.id=5805

But please! Consider what I wrote in that thread - personally I can't see a single situation where dynamic allocation would be needed. Main reasons to avoid it: you have a limited amount of RAM. At all times you need to know how much RAM you are using. You want your programs to behave in deterministic ways.

The main design issue is that there should not exist a situation in an embedded system where you want to have a completely arbitrary number of items. There must always be a maximum size, a worst case limit. And since an engineer must always design products for the worst case scenario, you must always allocate those 10 items for your structure no matter where you do it, if you suspect that 10 items is the worst case that could ever happen.

People who use dynamic allocation on 8 bit micros are typically PC programmers who are switching to the embedded systems branch, people who don't know how the heap is actually implemented. Adding a heap to your program will not magically give you more physical RAM! The static allocation int a[10] may hold 10 bytes allocated at all times. But a heap will literally do this:

#define HEAPSIZE 500

char heap[500];

where HEAPSIZE is typically something between 100 and 500 bytes. Unless your program relies very heavily on dynamic allocation, this is a huge waste of memory. And if your program actually relies heavily on dynamic allocation, your program is very vulnerable to the most well-known problem with dynamic allocation: memory leaks.

On top of that, you get meaningless execution time overhead when the malloc() allocates the segments and handles segmentation.
0 项奖励
回复