Question:
C/C++ question, why do we can't use memory address spaces before/without allocating it..?
anonymous
2013-10-16 06:13:56 UTC
C/C++ question, why do we can't use memory address spaces before/without allocating it..?
Three answers:
?
2013-10-16 06:53:21 UTC
Because there is no memory before allocating. The physical memory will be shift between allocated addresses. Before shifting an interrupt writes the data to the hard disc. ( swap) so u can use more memory then u have physical memory.
roger
2013-10-16 09:57:32 UTC
int * n;

// now n contains a random address (garbage)

if you try to do

*n=12;

you are putting the value 12 at a random address in your computer (OOPS)

some other programme might be using that bit of memory (maybe even the operating system)

Do not do that

what you need to do is allocate some memory

n=malloc(sizeof (int));

// now n points somewhere valid

*n=34;// now ok

or you could do

...

int j;

n=&j;// give it a known address

*n=45;



or

int p[10];// array allocated with space for 10 ints

int *n;

n=p;



remember that p[a] the same as *(p+a) by definition
VP
2013-10-16 06:36:45 UTC
Back in the caveman days of programming, computers did not have the great quantities of memory that they have today. So programs had to request or state their memory requirements up front so that the system could see if it had enough to run that particular program and still do the other "system things" it was supposed to do.



Nowadays, the memory requirements for students' programs are almost an after-thought to most systems. What required a pretty hefty mini- or mainframe computer when I started now sits on the corner of a desk.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...