EmbeddedRelated.com
Forums
Memfault Beyond the Launch

How to create dynamic array in dynamic c ?

Started by "sip...@ymail.com" December 5, 2011
Hello,

How to create dynamic array in dynamic c ?

I know in vb use:

1. ReDim (change the size of the array with destroying the existing data in the array)

2. ReDim Preserve (change the size of the array without destroying the existing data in the array)
Thank You.
SHIP4

Hello,

I'm trying to make a program like this to change the array size from 10 to 5 character, but it always appears an error like this: (Run Time Error: Heap usage error detected Address 08:7100). Is there a solution?

Thank You
SHIP4
#use "malloc.lib"

int main (){
char *pData, *pResize, i;

pData='\0';
pResize='\0';

pData = malloc(10 * sizeof(char));
if (pData != '\0'){
printf("Size pData: %d\n",strlen(pData));
for (i=0; i<10; i++){
pData[i]=i+1;
}
pData[i]='\0';

for (i=0; i<10; i++){
printf("%d.", *pData++);
}

// Error thrown frow here ..
// Run Time Error: Heap usage error detected Address 08:7100

pResize = realloc(pData, 5 * sizeof(char));
if(pResize != '\0'){
pData = pResize;
printf("Size pData: %d\n",strlen(pData));

for (i=0; i<10; i++){
printf("%d.", *pData++);
}
}
}
free (pData);
free (pResize);

return 1;
}
--- In r..., "siph4@..." wrote:
>
> Hello,
>
> How to create dynamic array in dynamic c ?
>
> I know in vb use:
>
> 1. ReDim (change the size of the array with destroying the existing data in the array)
>
> 2. ReDim Preserve (change the size of the array without destroying the existing data in the array)
> Thank You.
> SHIP4
>

There is no malloc.lib in Dynamic C, so is this library something you
wrote or acquired from someone else?

--- In r..., "siph4@..." wrote:
>
> Hello,
>
> I'm trying to make a program like this to change the array size from 10 to 5 character, but it always appears an error like this: (Run Time Error: Heap usage error detected Address 08:7100). Is there a solution?
>
> Thank You
> SHIP4
> #use "malloc.lib"
>
> int main (){
> char *pData, *pResize, i;
>
> pData='\0';
> pResize='\0';
>
> pData = malloc(10 * sizeof(char));
> if (pData != '\0'){
> printf("Size pData: %d\n",strlen(pData));
> for (i=0; i<10; i++){
> pData[i]=i+1;
> }
> pData[i]='\0';
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
>
> // Error thrown frow here ..
> // Run Time Error: Heap usage error detected Address 08:7100
>
> pResize = realloc(pData, 5 * sizeof(char));
> if(pResize != '\0'){
> pData = pResize;
> printf("Size pData: %d\n",strlen(pData));
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
> }
> }
> free (pData);
> free (pResize);
>
> return 1;
> }
> --- In r..., "siph4@" wrote:
> >
> > Hello,
> >
> > How to create dynamic array in dynamic c ?
> >
> > I know in vb use:
> >
> > 1. ReDim (change the size of the array with destroying the existing data in the array)
> >
> > 2. ReDim Preserve (change the size of the array without destroying the existing data in the array)
> >
> >
> > Thank You.
> > SHIP4
>

On 12/5/2011 4:51 PM, s...@ymail.com wrote:
> Hello,
>
> I'm trying to make a program like this to change the array size from 10 to 5 character, but it always appears an error like this: (Run Time Error: Heap usage error detected Address 08:7100). Is there a solution?
>
> Thank You
> SHIP4
> #use "malloc.lib"
>
> int main (){
> char *pData, *pResize, i;
>
> pData='\0';
> pResize='\0';
>
> pData = malloc(10 * sizeof(char));
> if (pData != '\0'){
> printf("Size pData: %d\n",strlen(pData));
> for (i=0; i<10; i++){
> pData[i]=i+1;
> }
> pData[i]='\0';
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
>
> // Error thrown frow here ..
> // Run Time Error: Heap usage error detected Address 08:7100
>
> pResize = realloc(pData, 5 * sizeof(char));
> if(pResize != '\0'){
> pData = pResize;
> printf("Size pData: %d\n",strlen(pData));
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
> }
> }
> free (pData);
> free (pResize);
>
> return 1;
> }

So, you allocate 10 bytes.
You fill 11 bytes of data (first bug: pData[i]='\0'; will set the 11th
byte).
You resize the array down to 5 bytes.
Then you access it still as a 10-byte array (2nd bug.)
Then you free the same buffer twice (3rd bug.)

Also:

if(pResize != '\0'){

Should give a warning about comparing a pointer to a char. Should be != NULL.

--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------

Hello seecwriter,

I'm used Dynamic C 10.56 and RCM5600W. Malloc.lib built from the rabbit, the location was in \Lib\Rabbit4000
--- In r..., "seecwriter" wrote:
>
> There is no malloc.lib in Dynamic C, so is this library something you
> wrote or acquired from someone else?
>
> --- In r..., "siph4@" wrote:
> >
> > Hello,
> >
> > I'm trying to make a program like this to change the array size from 10 to 5 character, but it always appears an error like this: (Run Time Error: Heap usage error detected Address 08:7100). Is there a solution?
> >
> > Thank You
> > SHIP4
> >
> >
> > #use "malloc.lib"
> >
> > int main (){
> > char *pData, *pResize, i;
> >
> > pData='\0';
> > pResize='\0';
> >
> > pData = malloc(10 * sizeof(char));
> > if (pData != '\0'){
> > printf("Size pData: %d\n",strlen(pData));
> > for (i=0; i<10; i++){
> > pData[i]=i+1;
> > }
> > pData[i]='\0';
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> >
> > // Error thrown frow here ..
> > // Run Time Error: Heap usage error detected Address 08:7100
> >
> > pResize = realloc(pData, 5 * sizeof(char));
> > if(pResize != '\0'){
> > pData = pResize;
> > printf("Size pData: %d\n",strlen(pData));
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> > }
> > }
> > free (pData);
> > free (pResize);
> >
> > return 1;
> > }
> >
> >
> > --- In r..., "siph4@" wrote:
> > >
> > > Hello,
> > >
> > > How to create dynamic array in dynamic c ?
> > >
> > > I know in vb use:
> > >
> > > 1. ReDim (change the size of the array with destroying the existing data in the array)
> > >
> > > 2. ReDim Preserve (change the size of the array without destroying the existing data in the array)
> > >
> > >
> > > Thank You.
> > > SHIP4
> > >
>

On Mon, December 5, 2011 1:51 pm, s...@ymail.com said:
> Hello,
>
> I'm trying to make a program like this to change the array size from 10 to
> 5 character, but it always appears an error like this: (Run Time Error:
> Heap usage error detected Address 08:7100). Is there a solution?
>
> Thank You
> SHIP4
> #use "malloc.lib"
>

SHIP4 --

Yes, there is a malloc() library in the file section of this
group. It's more for porting from other platforms than
actual Rabbit code. The fact that it didn't come with
Dynamic C gives you an idea of what the Rabbit developers
think of dynamic memory allocations on an embedded system.

I figure your 10 byte array is so you can learn things.
If you look at the HTTP server, it does not dynamically
allocate memory. Instead, for each listen, there is a
work-buffer as part of the big HTTP structure passed
around. With limited memory on the Rabbit, that is
the best approach.
> int main (){
> char *pData, *pResize, i;
>
> pData='\0';
> pResize='\0';

Use NULL for a pointer instead of character 0.

>
> pData = malloc(10 * sizeof(char));
> if (pData != '\0'){
> printf("Size pData: %d\n",strlen(pData));

malloc() will return you a pointer to memory. THe bytes
there can have any value. You can use calloc() if you
want those original bytes zeroed....

pData = calloc( 10, sizeof(char) );
if (pData != NULL) {
/* don't even try strlen() until you've got some
* non-zero data there, OK?
*/

> for (i=0; i<10; i++){
> pData[i]=i+1;
> }
> pData[i]='\0';

ERROR: This writes to the 11 th byte, pdata[10]. Not good.
You've only allocated pData[0] through pData[9].
To store 10 printable chars in C, you need to allocate
11 bytes: 10 for you and 1 '\0' for C to terminate it.
When I was learning C, writing a correct string duplicate
function was a good learning experience!
newstr = malloc( strlen(s)+1 );
With BASIC, it will tell you about boundary errors.
C is ...... well, C provides you all "The power,
elegance and simplicity of a hand grenade".

take care,
*the other brian
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
>
> // Error thrown frow here ..
> // Run Time Error: Heap usage error detected Address 08:7100
>
> pResize = realloc(pData, 5 * sizeof(char));
> if(pResize != '\0'){
> pData = pResize;
> printf("Size pData: %d\n",strlen(pData));
>
> for (i=0; i<10; i++){
> printf("%d.", *pData++);
> }
> }
> }
> free (pData);
> free (pResize);
>
> return 1;
> }
> --- In r..., "siph4@..." wrote:
>>
>> Hello,
>>
>> How to create dynamic array in dynamic c ?
>>
>> I know in vb use:
>>
>> 1. ReDim (change the size of the array with destroying the existing data
>> in the array)
>>
>> 2. ReDim Preserve (change the size of the array without destroying the
>> existing data in the array)
>> Thank You.
>> SHIP4
>

Hello Scott,

Thank you for the advice. I tried to fix a program like this and I still get the Run Time Error: Heap usage error detected.

What's Dynamic C doesn't support dynamic memory allocations in heap memory?

Thank You
SHIP4

#use "malloc.lib"

int main (){
auto char *pData, *pResize, i;

pData=NULL;
pResize=NULL;

pData = (char*)malloc(10 * sizeof(char));
if (pData != NULL){
printf("Size pData: %d\n",strlen(pData));
for (i=0; i<10; i++){
pData[i]=i+1;
}
//pData[i-1]='\0';

for (i=0; i<10; i++){
printf("%d.", *pData++)
}

// Error thrown from realloc
// Run Time Error: Heap usage error detected Address 04:f1df

pResize = (char*)realloc(pData, 5 * sizeof(char));
if(pResize != NULL){
pData = pResize;
printf("Size pData: %d\n",strlen(pData));

for (i=0; i<5; i++){
printf("%d.", *pData++);
}
}
}
free (pData);
// free (pResize);

return 1;
}
--- In r..., Scott Henion wrote:
>
> On 12/5/2011 4:51 PM, siph4@... wrote:
> > Hello,
> >
> > I'm trying to make a program like this to change the array size from 10 to 5 character, but it always appears an error like this: (Run Time Error: Heap usage error detected Address 08:7100). Is there a solution?
> >
> > Thank You
> > SHIP4
> >
> >
> > #use "malloc.lib"
> >
> > int main (){
> > char *pData, *pResize, i;
> >
> > pData='\0';
> > pResize='\0';
> >
> > pData = malloc(10 * sizeof(char));
> > if (pData != '\0'){
> > printf("Size pData: %d\n",strlen(pData));
> > for (i=0; i<10; i++){
> > pData[i]=i+1;
> > }
> > pData[i]='\0';
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> >
> > // Error thrown frow here ..
> > // Run Time Error: Heap usage error detected Address 08:7100
> >
> > pResize = realloc(pData, 5 * sizeof(char));
> > if(pResize != '\0'){
> > pData = pResize;
> > printf("Size pData: %d\n",strlen(pData));
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> > }
> > }
> > free (pData);
> > free (pResize);
> >
> > return 1;
> > }
>
> So, you allocate 10 bytes.
> You fill 11 bytes of data (first bug: pData[i]='\0'; will set the 11th
> byte).
> You resize the array down to 5 bytes.
> Then you access it still as a 10-byte array (2nd bug.)
> Then you free the same buffer twice (3rd bug.)
>
> Also:
>
> if(pResize != '\0'){
>
> Should give a warning about comparing a pointer to a char. Should be != NULL.
>
> --
> ------
> Scott G. Henion, Consultant
> Web site: http://SHDesigns.org
> ------
>

On 12/5/2011 6:30 PM, s...@ymail.com wrote:
> Hello Scott,
>
> Thank you for the advice. I tried to fix a program like this and I still get the Run Time Error: Heap usage error detected.
>
printf("Size pData: %d\n",strlen(pData));

This will run past the 5 byte array

You need to learn C more, especially how strings work.

> What's Dynamic C doesn't support dynamic memory allocations in heap memory?
>
> Thank You
> SHIP4

Using malloc()/Free() in Embedded systems is usually bad idea. Memory
is limited and you can end up where the memory pool is fragmented and
you can't allocate a buffer. Then you get stuck as you cant recover.

I wrote the malloc.lib in the files area. I used it in one app that its
use was only in one lib and it freed all memory when done so
fragmentation was not an issue. I would have rewritten the app but it
was huge.



--
------
Scott G. Henion, Consultant
Web site: http://SHDesigns.org
------

Hello sacnorthern,

Thanks for advice. I just found out, If dynamic-c no support for it.. :(
Thank You,
SHIP4
--- In r..., wrote:
>
> On Mon, December 5, 2011 1:51 pm, siph4@... said:
> > Hello,
> >
> > I'm trying to make a program like this to change the array size from 10 to
> > 5 character, but it always appears an error like this: (Run Time Error:
> > Heap usage error detected Address 08:7100). Is there a solution?
> >
> > Thank You
> > SHIP4
> >
> >
> > #use "malloc.lib"
> > SHIP4 --
>
> Yes, there is a malloc() library in the file section of this
> group. It's more for porting from other platforms than
> actual Rabbit code. The fact that it didn't come with
> Dynamic C gives you an idea of what the Rabbit developers
> think of dynamic memory allocations on an embedded system.
>
> I figure your 10 byte array is so you can learn things.
> If you look at the HTTP server, it does not dynamically
> allocate memory. Instead, for each listen, there is a
> work-buffer as part of the big HTTP structure passed
> around. With limited memory on the Rabbit, that is
> the best approach.
> > int main (){
> > char *pData, *pResize, i;
> >
> > pData='\0';
> > pResize='\0';
>
> Use NULL for a pointer instead of character 0.
>
> >
> > pData = malloc(10 * sizeof(char));
> > if (pData != '\0'){
> > printf("Size pData: %d\n",strlen(pData));
>
> malloc() will return you a pointer to memory. THe bytes
> there can have any value. You can use calloc() if you
> want those original bytes zeroed....
>
> pData = calloc( 10, sizeof(char) );
> if (pData != NULL) {
> /* don't even try strlen() until you've got some
> * non-zero data there, OK?
> */
>
> > for (i=0; i<10; i++){
> > pData[i]=i+1;
> > }
> > pData[i]='\0';
>
> ERROR: This writes to the 11 th byte, pdata[10]. Not good.
> You've only allocated pData[0] through pData[9].
> To store 10 printable chars in C, you need to allocate
> 11 bytes: 10 for you and 1 '\0' for C to terminate it.
> When I was learning C, writing a correct string duplicate
> function was a good learning experience!
> newstr = malloc( strlen(s)+1 );
> With BASIC, it will tell you about boundary errors.
> C is ...... well, C provides you all "The power,
> elegance and simplicity of a hand grenade".
>
> take care,
> *the other brian
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> >
> > // Error thrown frow here ..
> > // Run Time Error: Heap usage error detected Address 08:7100
> >
> > pResize = realloc(pData, 5 * sizeof(char));
> > if(pResize != '\0'){
> > pData = pResize;
> > printf("Size pData: %d\n",strlen(pData));
> >
> > for (i=0; i<10; i++){
> > printf("%d.", *pData++);
> > }
> > }
> > }
> > free (pData);
> > free (pResize);
> >
> > return 1;
> > }
> >
> >
> > --- In r..., "siph4@" wrote:
> >>
> >> Hello,
> >>
> >> How to create dynamic array in dynamic c ?
> >>
> >> I know in vb use:
> >>
> >> 1. ReDim (change the size of the array with destroying the existing data
> >> in the array)
> >>
> >> 2. ReDim Preserve (change the size of the array without destroying the
> >> existing data in the array)
> >>
> >>
> >> Thank You.
> >> SHIP4
> >
> >
>

Hello Scott,

Thank you for the advice. I will learn more about c, because I am starting to like this language.So far I only know intel assembly and vb6.

Thank You
SHIP4

--- In r..., Scott Henion wrote:
>
> On 12/5/2011 6:30 PM, siph4@... wrote:
> > Hello Scott,
> >
> > Thank you for the advice. I tried to fix a program like this and I still get the Run Time Error: Heap usage error detected.
> >
> printf("Size pData: %d\n",strlen(pData));
>
> This will run past the 5 byte array
>
> You need to learn C more, especially how strings work.
>
> > What's Dynamic C doesn't support dynamic memory allocations in heap memory?
> >
> > Thank You
> > SHIP4
>
> Using malloc()/Free() in Embedded systems is usually bad idea. Memory
> is limited and you can end up where the memory pool is fragmented and
> you can't allocate a buffer. Then you get stuck as you cant recover.
>
> I wrote the malloc.lib in the files area. I used it in one app that its
> use was only in one lib and it freed all memory when done so
> fragmentation was not an issue. I would have rewritten the app but it
> was huge.
>
> --
> ------
> Scott G. Henion, Consultant
> Web site: http://SHDesigns.org
> ------
>


Memfault Beyond the Launch