Hi,
I was wondering if I can do the following without creating memory problems:
Create something like -- int * array1 -- as a public member my class
then initiate it with a new value every time I run a certain function, like this --- array1 = new int[somenumber] ---
but ONLY delete it in the destructor --- delete [] array1 ---
If I reinitiate it with a new value before deleting it, what happens to the old memory which it occupies? Also, can I call ---- array1 = NULL --- or does that not work for arrays?
Thanks a lot,
Crispin
3 1301
If you allocate new memory for the pointer, then whatever memory it used to point to still exists. This isn't a major problem in small programs, but it is not a practice you should make a habit of. You should always use the delete command to deallocate that memory, or you may run out of memory in larger applications.
Setting the pointer to NULL will create the same problem. You are changing the location to which the pointer points (in effect, pointing it to nothing), but the data and memory allocated is still embedded in memory.
If you want to create new data for the same pointer in different functions, make sure you delete the old content before creating new content. If you cannot use delete, consider using an alternate method of having multiple arrays of data.
Thanks a lot Ganon, that really clears things up.
Does the same thing apply for char strings... i.e. if you initiate a char array (unsigned char *b). Do you deallocate the string like an array (delete []b) or like a single pointer (delete b).
Cheers,
Crispin
I haven't done a lot with character arrays per se, but I imagine they're just like all other pointer arrays - you should use delete [] b.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: overbored |
last post by:
I can do this:
int asdf;
int* zxcv = asdf;
but not this:
int asdf;
int** zxcv = asdf;
|
by: sangeetha |
last post by:
Hello,
Is there any performance difference in using of the following two declaration?
int (*ptr); //Array of 10 int pointers
int *ptr; // pointer-to-array of 10.
Regards,
Sangeetha.
|
by: x-pander |
last post by:
given the code:
<file: c.c>
typedef int quad_t;
void w0(int *r, const quad_t *p)
{
*r = (*p);
}
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: Wonder |
last post by:
Hello,
I'm confused by the pointer definition such as int *(p);
It seems if the parenthesis close p, it defines only 3 integers. The
star
is just useless. It can be showed by my program:
...
|
by: sheroork |
last post by:
Can anyone help me in getting to understand pointer to pointer with
examples?
Appritiate in advance.
Sagar
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: Zhou Yan |
last post by:
I want to define a pointer to a multiple-subscripted array.
For instance, I have an array defined as below( I have reduced the
size of the array as well as the dimension, but I think it OK to ask...
|
by: Ioannis Vranos |
last post by:
Are the following codes guaranteed to work always?
1.
#include <iostream>
inline void some_func(int *p, const std::size_t SIZE)
{
|
by: subramanian100in |
last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list ·
Question 6.13
int a1 = {0, 1, 2};
int a2 = {{3, 4, 5}, {6, 7, 8}};
int *ip; /* pointer to int */
int (*ap); /* pointer to...
|
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: 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...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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: 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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |