HI !
Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int
int i;
t = new int*[10]; // memory allocation
for( i=0 ; i < 10; i++ )
{
t[i] = new int(); // memory allocation
}
for( i=0 ; i < 10; i++ )
{
printf("%d\n",t[i]); // shows some random data. Its ok !
}
for( i=0 ; i < 10; i++ ) // as far as I know *t[i] - is data to what
t[i] points
{ // so *t[i] = 202, copy that number in t[i] location
*t[i] = 202 ; // so now,*t[i] contains number 202
}
for( i=0 ; i < 10; i++ )
{
printf("%d\n",t[i]); // what ?? AGAIN GARBAGE!!! WHERE IS NUMBER 202
??
}
for( i=0 ; i < 10; i++ )
{
delete t[i] ; // free memory up
}
delete t; // free memory up 7 1972
Ciur Eugen wrote: Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int int i;
t = new int*[10]; // memory allocation for( i=0 ; i < 10; i++ ) { t[i] = new int(); // memory allocation }
Although your code is C++, the style is very close to C (you just
replaced malloc with new). You're not benefitting from the modern C++
facilities, and getting confused with pointers and memory handling.
Unless this is an academic exercise, I suggest you look into the
standard template library (STL) and specifically the vector container.
--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006) http://www.spinellis.gr/codequality?clcpp
Ciur Eugen wrote: Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int int i;
t = new int*[10]; // memory allocation for( i=0 ; i < 10; i++ ) { t[i] = new int(); // memory allocation } for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // shows some random data. Its ok ! }
Don't use printf. It has thrown away the type of t[i], and is treating a
pointer as an integer. On some platforms, where pointers cannot easily
represent as integers, this could cause undefined behavior.
Read /Accelerated C++/, and don't use new, either. Use a std::vector<>, to
take care of these low level details.
for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // what ?? AGAIN GARBAGE!!! WHERE IS NUMBER 202 ?? }
It's at printf("%d\n", *t[i]);
You were printing the address again.
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
What is it supposed to do
what is it actually doing?
Is it homework?
In article <11**********************@t31g2000cwb.googlegroups .com>, Ciur
Eugen <ci********@gmail.com> writes HI !
Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int int i;
t = new int*[10]; // memory allocation for( i=0 ; i < 10; i++ ) { t[i] = new int(); // memory allocation } for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // shows some random data. Its ok ! }
for( i=0 ; i < 10; i++ ) // as far as I know *t[i] - is data to what t[i] points { // so *t[i] = 202, copy that number in t[i] location *t[i] = 202 ; // so now,*t[i] contains number 202 }
for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // what ?? AGAIN GARBAGE!!! WHERE IS NUMBER 202 ?? }
for( i=0 ; i < 10; i++ ) { delete t[i] ; // free memory up }
delete t; // free memory up
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Chris Hills wrote: What is it supposed to do what is it actually doing? Is it homework?
While it's not the greatest post ever, I think its comments covered your
first two questions. And its homework status is irrelevant if it's a
well-formed post.
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Ciur Eugen wrote: HI !
Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int int i;
t = new int*[10]; // memory allocation for( i=0 ; i < 10; i++ ) { t[i] = new int(); // memory allocation } for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // shows some random data. Its ok ! }
for( i=0 ; i < 10; i++ ) // as far as I know *t[i] - is data to what t[i] points { // so *t[i] = 202, copy that number in t[i] location *t[i] = 202 ; // so now,*t[i] contains number 202 }
for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // what ?? AGAIN GARBAGE!!! WHERE IS NUMBER 202 ?? }
for( i=0 ; i < 10; i++ ) { delete t[i] ; // free memory up }
delete t; // free memory up
Look at your own comments! You day that *t[i] contains the number 202,
but you're printing t[i] which points to where the 202 is stored.
"Ciur Eugen" <ci********@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com... HI !
Could somebody help me in finding error in the following code ?
int ** t; // a table of pointers to int, t[i], is a pointer to int int i;
t = new int*[10]; // memory allocation for( i=0 ; i < 10; i++ ) { t[i] = new int(); // memory allocation } for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // shows some random data. Its ok !
This is actually not random data, it is the pointers expressed as integers.
}
for( i=0 ; i < 10; i++ ) // as far as I know *t[i] - is data to what t[i] points { // so *t[i] = 202, copy that number in t[i] location *t[i] = 202 ; // so now,*t[i] contains number 202
Right, the contents of the pointer t[i] now contains the value 202.
}
for( i=0 ; i < 10; i++ ) { printf("%d\n",t[i]); // what ?? AGAIN GARBAGE!!! WHERE IS NUMBER 202
t[i] is the pointer, remember? If you want to see the value you have to
look at the contents. simply change t[i] to *t[i]
?? }
for( i=0 ; i < 10; i++ ) { delete t[i] ; // free memory up }
delete t; // free memory up This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thelma Lubkin |
last post by:
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.
A form that works for me on the system I'm using, apparently...
|
by: wana |
last post by:
I was reading through original source code of ping for some insight
and was confused by unusual code.
Entire listing available at:
http://www.ping127001.com/pingpage/ping.html
#include...
|
by: __frank__ |
last post by:
The following code use a macro and a label.
I would to change it and use instead
a more readable function and avoid the label.
The macro DAQmxFailed checks for the return code
of the various...
|
by: Aaron Queenan |
last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the
following error message:
Linking...
LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport
LINK : error...
|
by: mike_li |
last post by:
On Window 2000 Professional Server
DB2 UDB
Level:
DB2 code release "SQL07029" with level identifie
"030A0105" and informational tokens "DB2 v7.1.0.98", "n040510" and
"WR21337".
In the...
|
by: dbuchanan |
last post by:
Hello,
>From time to time my vb2005 form disappears and is replaced by the
following errors. Rebuilding the application never helps. However the
errors never affects the operation of my...
|
by: Jens Jensen |
last post by:
Hello all,
I need some design advice for my web service.
I' have written a web service that exposes a function that takes some
parameters
and return an xml.
|
by: Pool |
last post by:
I tried to connect DB2 (Sitting in Unix server at my client location) using Db2 connect V8. I am getting the following error message. I tried all the possible options BUt the error is same.. See each...
|
by: walve_wei |
last post by:
<1>use the D3D control panel, enable the debug DLL and maximum
validation,for D3D control panel ,you need to install the directx sdk.
<2>Start up the debug monitor (<MSVC install...
|
by: sanctus |
last post by:
I have a Matlab code which works fine in windows. Now I installed Xubuntu (because this way I have admin rights(=root) which I don't have on windows) and want to use the same code.
To be able to...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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,...
| |