Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 02-18-2006, 05:33 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
Creating a Dynamic One Dimensional Array

Anakashar created a new c++ article here:

http://cpp.codenewbie.com/articles/c...ay-Page_1.html

Thanks Anakashar!
sde is offline   Reply With Quote
Old 02-20-2006, 04:10 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 701
DJMaze is on a distinguished road
Nice but it doesn't always work.
I got my compiler fail a few times if the array is defined outside main.
Instead i had to use vector or alloc()
Code:
int array = new int[10];
// but can't resize it now
array = new int[100]; // error
Code:
array = (int *)malloc(10 * sizeof(int *));
array = (int *)realloc(array, 100 * sizeof(int *));
DJMaze is offline   Reply With Quote
Old 02-20-2006, 05:30 AM   #3 (permalink)
Anakashar
C++ Intermediate
 
Anakashar's Avatar
 
Join Date: Feb 2006
Posts: 15
Anakashar is on a distinguished road
I think I see what you're doing, you gotta delete [] array the array before you re-declare it as a different new int[]

Last edited by Anakashar; 02-20-2006 at 12:51 PM.
Anakashar is offline   Reply With Quote
Old 02-20-2006, 12:04 PM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 701
DJMaze is on a distinguished road
Quote:
Originally Posted by Anakashar
(and I think I see what you're doing, you gotta delete [] array the array before you re-declare it as a different new int[])
That's actualy my point. Your not realy creating a dynamic array since you either need to delete it or copy data from one array into the other and then delete the old one.
So mainly your example is dynamic but not sufficient when you need to resize it after declaration.

So using alloc functions to resize the array you are realy working dynamical without creating memory overhead and without process abuse

I've that path many times before and it bugged me BUT it is a nice newbie example how to create a dynamic array.
DJMaze is offline   Reply With Quote
Old 02-21-2006, 10:28 AM   #5 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
Quote:
Originally Posted by DJMaze
Nice but it doesn't always work.
I got my compiler fail a few times if the array is defined outside main.
Instead i had to use vector or alloc()
Code:
int array = new int[10];
// but can't resize it now
array = new int[100]; // error
Use an int*, not an int. In C, arrays are basically pointers with prettier semantics.

Code:
int *array = new int[ 10 ];
array = new int[ 100 ];
It'll compile and run, but it is an automatic memory leak.
kyoryu is offline   Reply With Quote
Old 02-28-2006, 08:43 AM   #6 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Errata

I don't know how anyone missed this one.

Page 2:
Quote:
Code:
int Array[50];
assigns the array Array to hold 51 integer values.
Like hell it does.
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Old 02-28-2006, 01:30 PM   #7 (permalink)
Anakashar
C++ Intermediate
 
Anakashar's Avatar
 
Join Date: Feb 2006
Posts: 15
Anakashar is on a distinguished road
Yeah, I made one or two errors in some places, mostly it was typed at 3 in the morning.. I changed some things around in there without changing something elsewhere when I made it.

It was my first one, geez..
Anakashar is offline   Reply With Quote
Old 02-28-2006, 01:54 PM   #8 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Quote:
Originally Posted by Anakashar
Yeah, I made one or two errors in some places, mostly it was typed at 3 in the morning.. I changed some things around in there without changing something elsewhere when I made it.

It was my first one, geez..
It may also be the first one some newbie reads.
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Old 02-28-2006, 02:13 PM   #9 (permalink)
Anakashar
C++ Intermediate
 
Anakashar's Avatar
 
Join Date: Feb 2006
Posts: 15
Anakashar is on a distinguished road
I'll fix it in a bit, then. It's in the intermediate section, but I'll fix it with permission of a mod.
Anakashar is offline   Reply With Quote
Old 03-02-2006, 07:39 AM   #10 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
We learn from eachother. That's the way to go.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating and Using a 2 dimensional Vector (vector of vectors) xytor Standard C, C++ 2 09-12-2005 02:35 AM
C++ Multi Dimensional Array using Vectors. abs Standard C, C++ 16 02-26-2005 04:40 AM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
creating dynamic variable names sde PHP 5 11-02-2002 09:03 AM
creating an array of an object on the freestore in c++ ?? sde Standard C, C++ 7 08-04-2002 01:39 PM


All times are GMT -8. The time now is 01:19 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting