How to allocate different memory using 'new'

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

How to allocate different memory using 'new'

Postby palakdebnath » Fri Jun 05, 2009 4:57 am

Hi,

I wrote a small program for memory allocation using new operator. Following is the code -

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
int* p=NULL;
p= new int[2];
printf("\n address of p = %X\n",p);
}

Every time I'm running this code, I'm getting same memory address. But I want to get different address in different run. How can I achieve this requirement using 'new' operator? Shall I have to use any option? Please help me out. Any help will be appreciated.

Thanks in advance.

Thanks,
Palak
palakdebnath
 
Posts: 24
Joined: Wed Jun 04, 2008 4:21 pm

Re: How to allocate different memory using 'new'

Postby Alvaro » Fri Jun 05, 2009 6:33 am

You have no control over what the specific address is of the block returned by new. Why should you care what the address of the block returned is? This is like complaining that at the coat check in a restaurant they always give you the same number: The number is only a mechanism to recover your coat, so it doesn't really matter what it is.

Minor issues with the code you posted:
* You didn't use
Code: Select all
tags around it.
* p is initialized to a value (NULL) that is never used.
* There is no delete[] in your code, so you are technically leaking memory (it's just a good habit to always delete what you new).
* To print addresses you should use "%p" as the format, not "%X". "%X" expects an unsigned int, which may or may not be able to hold a pointer, depending on the platform. "%p" actually expects a pointer of type `void *', so a cast should be used.
* Those header files ending in ".h" are obsolete (there is a sticky somewhere about why you shouldn't use them).
* You don't really need anything in <iostream.h> or <stdlib.h>.

Code: Select all
#include <cstdio>

int main() {
  int *p = new int[2];
  std::printf("Address of p = %p\n", static_cast<void *>(p));
  delete[] p;
}
User avatar
Alvaro
Moderator
 
Posts: 5180
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: How to allocate different memory using 'new'

Postby tonyadams » Mon Jan 11, 2010 3:00 am

hi...
you put int *p=NULL that why not allocate different memory using 'new'.you just write down int *p=new int[2].
it may possible run program.try this ok.
tonyadams
 
Posts: 3
Joined: Mon Jan 11, 2010 2:51 am

Re: How to allocate different memory using 'new'

Postby lijippy » Fri Jun 25, 2010 8:07 am

First,In standrad cpp,int size 4 bytes,and yet if you want to print a address on sreen ,you need to use "%p"by using printf.For example
#include<stdio.h>

int main()
{
int *p=NULL;
p=malloc(sizeof(int));
printf("This is the address of point p%p\n",p);
}
lijippy
 
Posts: 5
Joined: Sat Jun 19, 2010 11:56 pm


Return to Unix/Linux

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron