Hello..
Im trying to make a way to have a dynamic array....
I was doing a test with global pointers and the heap. Would someone
tell me thy this doesn't compile:
#include <iostream>
using namespace std;
void Test();
int * ptr;
ptr = new int [3];
main(){
int * ptr;
ptr = new int [3];
ptr[2] = 1;
Test();
cout << ptr[3] << endl;
return 0;
}
void Test(){
delete[] ptr;
int * ptr;
ptr = new int [4];
ptr[3] = 10;
}
I get the error:
Test.cpp:7: error: expected constructor, destructor, or type conversion
before '=' token
Thanks alot 9 6634
int * ptr;
ptr = new int [3];
this is a execute code,and the c++ compiler can't create initialize code for
this status
<fo******@gmail.com> ????
news:11**********************@f14g2000cwb.googlegr oups.com... Hello..
Im trying to make a way to have a dynamic array.... I was doing a test with global pointers and the heap. Would someone tell me thy this doesn't compile:
#include <iostream> using namespace std;
void Test();
int * ptr; ptr = new int [3]; main(){ int * ptr; ptr = new int [3]; ptr[2] = 1; Test(); cout << ptr[3] << endl; return 0; }
void Test(){ delete[] ptr; int * ptr; ptr = new int [4]; ptr[3] = 10; }
I get the error: Test.cpp:7: error: expected constructor, destructor, or type conversion before '=' token
Thanks alot
What can i do to fix it?
Are global pointers that use the heap possible?
Thanks fo******@gmail.com wrote: Hello..
Im trying to make a way to have a dynamic array.... I was doing a test with global pointers and the heap. Would someone tell me thy this doesn't compile:
#include <iostream> using namespace std;
void Test();
int * ptr; ptr = new int [3];
// replace the two lines above with.
int * ptr( new int [3] );
// However, even better ,,,
#include <vector>
std::vector<int> ptr(3);
main(){ int * ptr; ptr = new int [3];
^^^ what's this ? You're leaking the initialization time new int[3]
ptr[2] = 1; Test(); cout << ptr[3] << endl; return 0; }
void Test(){ delete[] ptr; int * ptr; ptr = new int [4]; ptr[3] = 10; }
// if you're using vector
void Test()
{
ptr.clear();
ptr.resize(4);
} I get the error: Test.cpp:7: error: expected constructor, destructor, or type conversion before '=' token
Thanks alot
Wow..
That worked...
Thanks alot!
Sry about that extra thing...
I was doing some tests, and forgot to delete them...
Thanks! fo******@gmail.com wrote: Hello..
Im trying to make a way to have a dynamic array....
You shouldn't use pointers and you shouldn't use globals.
One day you'll see the merit of this. On that day you'll be a better
programmer.
john
John Harrison wrote: fo******@gmail.com wrote:
Hello..
Im trying to make a way to have a dynamic array....
You shouldn't use pointers and you shouldn't use globals.
Ever ? One day you'll see the merit of this. On that day you'll be a better programmer.
john
Gianni Mariani wrote in message ... fo******@gmail.com wrote: Hello.. Im trying to make a way to have a dynamic array.... I was doing a test with global pointers and the heap. Would someone tell me thy this doesn't compile:
#include <iostream> using namespace std; void Test();
int * ptr; ptr = new int [3]; // replace the two lines above with. int * ptr( new int [3] );
// However, even better ,,, #include <vector> std::vector<int> ptr(3);
// >> main(){
int main(){ // as already stated in other post(s). int * ptr; ptr = new int [3]; ^^^ what's this ? You're leaking the initialization time new int[3]
ptr[2] = 1; Test(); cout << ptr[3] << endl; return 0; }
Add to Gianni's post:
void Test(){ delete[] ptr;
This deletes the array pointed to by the global 'ptr'. The global pointer is
still there, and can now be re-assigned to (it wasn't 'const').
int * ptr;
This is another 'error'(see next comment), you are (re)declaring a pointer
with the same name as the global one.
ptr = new int [4];
This assigns to the local you just declared, where you need '::ptr' here to
get to the global. When you leave this function, you have no pointer to the
memory you just allocated - a memory leak. Another vote *for* 'vector'.
ptr[3] = 10;
delete[] ptr; // for the local pointer.
return; }
// if you're using vector void Test(){ ptr.clear(); ptr.resize(4); }
Hope I got that right, corrections are welcome.
--
Bob R
POVrookie
John Harrison wrote in message ... fo******@gmail.com wrote: Hello.. Im trying to make a way to have a dynamic array....
You shouldn't use pointers and you shouldn't use globals.
One day you'll see the merit of this. On that day you'll be a better programmer. john
So, you are saying we should:
int main(){
#include <iostream>
class MyThing{ /* guts go here*/ };
MyThing TheThing;
return 0;
}
Is that going to work in all cases? (Any cases?)
<G>
--
Bob R
POVrookie
Gianni Mariani wrote: John Harrison wrote: fo******@gmail.com wrote:
Hello..
Im trying to make a way to have a dynamic array....
You shouldn't use pointers and you shouldn't use globals.
Ever ?
In the context of the question: yes.
If you need a dynamic array, use an STL collection. If you need global
access via a single instance, don't create a global object but use a
singleton
or dedicated access functions. (i.e. MyClass GetFoo(int)/void
SetFoo(int,MyClass))
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: john townsley |
last post by:
whats the best way to declare Global objects (holding say 10 member
variables and functions) of an unknown size in c++, memory efficient as
possible
thanks
|
by: Robert North |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I've just started programming JS, and one question keeps nagging me:
I can insert a <script> element anywhere in HTML, and when it's
executed...
|
by: Ranga |
last post by:
I was unable to run the statement "CREATE GLOBAL TEMPORARY TABLE" on
unix version of DB2, it gave the follwing error
db2 => create global temporary table temp ( OGI_SYS_NR char(8) )
DB21034E ...
|
by: whisper |
last post by:
My question is whether it is better to use a global variable to hold
a dynamically malloced multidim array or pass around pointers to it.
The details are below (forgive the long winded explanation)...
|
by: Sunil |
last post by:
Hi all,
I want to know how good or bad it is using global variables i.e
advantages
and disadvantages of using global variables.
Sunil.
|
by: TuxC0d3 |
last post by:
Hi!
I'm diving into the some more ++ specific aspects of c++ (and finally
accepting that c++ is more than "a plus added to c" :), so that means
using namespaces, templates, std::strings, lists,...
|
by: istillshine |
last post by:
When I control if I print messages, I usually use a global variable
"int silent". When I set "-silent" flag in my command line
parameters, I set silent = 1 in my main.c.
I have many functions...
|
by: teddysnips |
last post by:
Weird.
I have taken over responsibility for a legacy application, Access 2k3,
split FE/BE.
The client has reported a problem and I'm investigating. I didn't
write the application.
The...
|
by: supa |
last post by:
Hi. I've coded some stuff a long time ago, and didn't have reason to take up programming until now. I've learned Python 'on-the-go' and now ran into the idea of avoiding global variables of any kind....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |