malloc in .net..

  • Thread starter Thread starter Sadun Sevingen
  • Start date Start date
Sadun Sevingen said:
hi is there malloc command for native arrays to enlarge size of the array

Native arrays? If so, check the docs for the meaning of realloc()?

Regards,
Will
 
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

int main(void)
{
int dizi[10];

for(int i = 0; i < 10; i++)
dizi = i * 2;

size_t size = sizeof(dizi)/sizeof(dizi[0]);

for(size_t i = 0; i < size; i++)
Console::WriteLine(dizi);

// free(dizi); doesn't work...
// also i want to enlarge the array

return 0;
}
 
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#using <mscorlib.dll>

using namespace System;

int main() {
int number;
int *ptr;
int i;

printf("How many ints would you like store? ");
scanf("%d", &number);

ptr = (int *)(malloc(number*sizeof(int))); /* allocate memory */

if(ptr!=NULL) {
for(i=0 ; i<number ; i++) {
*(ptr+i) = i;
}

for(i=number ; i>0 ; i--) {
printf("%d\n", *(ptr+(i-1))); /* print out in reverse order */
}

free(ptr); /* free allocated memory */
return 0;
}
else {
printf("\nMemory allocation failed - not enough memory.\n");
return 1;
}
}
 
Sadun Sevingen said:
int main(void)
{
int dizi[10];
...
// free(dizi); doesn't work...

It shouldn't as dizi is on the stack. The compiler effectively creates the
code to reclaim the memory for this object in the "epilog" code that it adds
to the function at its closing brace ( "}" )

Regards,
Will
 
Sadun Sevingen said:
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#using <mscorlib.dll>

using namespace System;

int main() {
....
}

Is there a question here or is this homework? <g>

Regards,
Will
 
Sadun said:
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

int main(void)
{
int dizi[10];

for(int i = 0; i < 10; i++)
dizi = i * 2;

size_t size = sizeof(dizi)/sizeof(dizi[0]);

for(size_t i = 0; i < size; i++)
Console::WriteLine(dizi);

// free(dizi); doesn't work...
// also i want to enlarge the array
...


Yikes!

Look at std::vector instead, and get a good & current C++ programming book.
 
it was just a question that i was seeking for all kind of arrays that can i
use on VC++.net

i found
native arrays (value type, ref type)
system array called as managed array System:Array ther are ref type
and collections inherits from Array but much more efficient by indexing
functionalty "associativety"

but i don't have any idea about vectors i'ill look @ them...
 
Back
Top