Array dimensions must be compile-time constants (unless you are using gcc, which provides an extended feature).
You'll have to use malloc or calloc, and remember to call free before the function returns.
Array dimensions must be compile-time constants (unless you are using gcc, which provides an extended feature).
You'll have to use malloc or calloc, and remember to call free before the function returns.
Ary = calloc(size, sizeof(*Ary));
The calloc function allocates space for an array of size objects, each of whose size is sizeof(*Ary). The space is initialized to all bits zero. The function returns either a null pointer (in case of error) or a pointer to the allocated space.
sizeof(*Ary)
That is the size of one element of Ary.
free(Ary);
The free function causes the space pointed to by Ary to be deallocated, that is, made available for further allocation.
You can probably eliminate the C++ error message like this: Ary = (int*)calloc(size, sizeof(*Ary));
Be sure you've named your source file something.c and not something.cpp, because cpp may select the C++ compiler.
Ya thanks alot... i change to .c file then it is work..
At this moment i stil 50% understand, nevermind let me play around the program that you give me and i try to understand from there... if got anything that i not understand i will try to futher discuss with you...
Q1: Your question is unclear, but I think you asking why I wrote sizeof(*Ary) instead of sizeof(int). Both are correct, but sizeof(*Ary) is better because if you someday change Ary from int* to a larger or smaller type such as double*, then sizeof(*Ary) will still be correct.
Q2: if calloc or malloc fails, it is usually because your computer has run out of memory. If your program fails to detect the error, and tries to use the array, the program will probably crash. That's a common bug in many programs.
You should search your compiler's library reference manual (or a good C book) for full descriptions of calloc, malloc, free, and other memory allocation functions.
Now i understand the code already... Thank you very much....
Do you got any idea... if i apply this code at Keil Compiler for uC. I try already but got some worning there.... Bellow the code similar that you giving me (only the int *Ary; change it to int xdata *Ary; at Keil Compiler)
Warning Display:
Warning: Module Name Not Unique
Module: C:\Keil\C51\LIB\C51s.LIB (CALLOC)
Warning: Unresolved External Symbol
Symbol: _CALLOC
Module: calloc.obj (CALLOC)
Warning: Reference Made to Unresolved External
Symbol: _CALLOC
Module: calloc.obj (CALLOC)
Code:
#include <stdlib.h>
#include <stdio.h>
#include "reg52.h"
void tst_calloc(const int size)
{
int xdata *Ary;
int s=2, n=3;
Ary = calloc(size, sizeof(*Ary));
if (!Ary)
{
return;
}
Ary[s]=n;
// free(Ary);
}
void main(void)
{
while(1)
{
tst_calloc(100);
}
}
The answer is simple. You don't have calloc in the libraries which were searched when building your program. Check your manual and see which library you need to link with your program to include calloc. Also pay attention not to change your pointer when calling free or your program will crash.