473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

malloc in .net..

hi is there malloc command for native arrays to enlarge size of the array
???
Nov 17 '05 #1
7 3179
"Sadun Sevingen" <b-saduns@ms> wrote in message
news:eb*************@TK2MSFTNGP09.phx.gbl...
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
Nov 17 '05 #2
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

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

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

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

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

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

return 0;
}
Nov 17 '05 #3
#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;
}
}
Nov 17 '05 #4
"Sadun Sevingen" <b-saduns@ms> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
#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
Nov 17 '05 #5
"Sadun Sevingen" <b-saduns@ms> wrote in message
news:eI**************@TK2MSFTNGP10.phx.gbl...
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
Nov 17 '05 #6
Sadun Sevingen wrote:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

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

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

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

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

// 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.
Nov 17 '05 #7
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...
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.