473,385 Members | 1,693 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,385 software developers and data experts.

Memory allocation for a char array

3
Hi,

I am trying to create a fresh copy of char array each time the testmemory() function(see below) is called:

Expand|Select|Wrap|Line Numbers
  1. void testmemory ()
  2. {
  3.     char * temp = new char [10];
  4.     cout << &temp <<endl;
  5. }
  6.  
  7. int main ()
  8. {
  9.     testmemory();
  10.     testmemory();
  11.     return 0;
  12. }
  13.  
However by observing the output, the memory address of "temp" remains the same at each function call, which is not my expectation. I am not familiar with C++ so pls point out my mistakes.

For your information, I compiled the code under cygwin and mingw, they all give me the same output.

Rick
Jul 30 '07 #1
4 2721
acker
3
Oh I already see the problem. &temp is the address of the pointer variable, not the address of the char array. But now the problem is, how do I print out the address of the char array? if i try to use cout<<temp; it'll print out the c string not the address
Jul 30 '07 #2
gpraghuram
1,275 Expert 1GB
Oh I already see the problem. &temp is the address of the pointer variable, not the address of the char array. But now the problem is, how do I print out the address of the char array? if i try to use cout<<temp; it'll print out the c string not the address
hi,
In C u can get the address like this
Expand|Select|Wrap|Line Numbers
  1. printf("temp is %x\n",temp); // This will print address hold by temp
  2. printf("temp is %s\n",temp); //This will print the value
  3. printf("&temp is %x\n",&temp); //This will print the address of temp 
  4.  
Raghuram
Jul 30 '07 #3
JosAH
11,448 Expert 8TB
Statically cast your temp variable to a void* and print that.

kind regards,

Jos
Jul 30 '07 #4
acker
3
Thanks Raghuram and Jos, the solutions given by you works perfectly :)
Jul 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
10
by: AlabiChin | last post by:
Hello, I noticed that when I dynamically create an array of chars, the resulting size of the allocated memory block is larger by about 15 bytes than what I specified. Here is example code: ...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
3
by: william | last post by:
My situation is here: an array of two dimension can only be defined locally within a function(because the caller don't know the exact size ). Then the question is: how should the caller access...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.