Delerna 1,134
Recognized Expert Top Contributor
Been playing around with DirectX 10 in C++ and I have been using arrays to store info for vertex buffers. To simplify my question I will drop DX10 from the equation. I am an experienced programmer but have only dabbled in C++.
To The Point.
If i declare a variable ready for use as a dynamic array
int* ArrayName;
and initialize it so it has a size of 1
ArrayName=new int [1];
and then in a function in my code I want to expand the array to 2
so I create another array with the new dimensions
int* NewArray;
NewArray= new int[2];
and copy the data from ArrayName to NewArray
(I wont show the code as its not my question)
and then point ArrayName to the NewArray
ArrayName= &NewArray;
My question is this
What happens to the memory that ArrayName was pointing at before I pointed it at NewArray ??
Do I need to release it befor I repoint it??
How ??
5 1739 gpraghuram 1,275
Recognized Expert Top Contributor
memory of ArrayName leaks.
Since you overwrite the memory with new value you dont have a chance to free the memeory of ArrayName variable
Raghuram
and copy the data from ArrayName to NewArray
(I wont show the code as its not my question)
and then point ArrayName to the NewArray
ArrayName= &NewArray;
Further, this does not copy the NewArray to the ArrayName array. It just copies the address.
Read this for a quick refresher on C++ arrays:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
In each case value is the number of elements. Any other brackets only describe the elements.
Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
With the above in mind this array: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Kinda the same, right?
So if your disc file contains
0 1 2 3 4 5 6 7 8 9
Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.
Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Delerna 1,134
Recognized Expert Top Contributor
memory of ArrayName leaks.
Since you overwrite the memory with new value you dont have a chance to free the memeory of ArrayName variable
Raghuram
Thanks Raghuram
I thought it would leak.
So how do I release it before repointing to NewArray
Delerna 1,134
Recognized Expert Top Contributor
[quote=weaknessf orcats]Further, this does not copy the NewArray to the ArrayName array. It just copies the address.
QUOTE]
Thanks weaknessforcats
I realise that I need to copy the the contents of ArrayName over to NewArray
Thats why I stated that I wouldn't show the code because it wasn't my question :-)
I appreciate your time and effort and although I am familiar with the principles of your refresher it has been presented well, Short and concise.
I will be copying the refresher over to my help documents, hope you don't mind.
Thanks for the comments
I will be copying the refresher over to my help documents, hope you don't mind.
Not at all. I'm glad you found it useful.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: R.Z. |
last post by:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque depends on the size of its
members even if the deque is empty? is there at all a way to check out how
much memory my deque occupies? i've read that the sizeof operator cannot be
used with dynamically allocated arrays so i figured it wouldn't give...
|
by: Sourin |
last post by:
Hi all,
I am trying to write code for my experiments. My work involves huge
datasets, and as such my code needs to be memory efficient. I did some
hand calculations regarding the amount of dynamic memory required for
my datastructures ( used sizeof() to get the size of the structures ).
But mallinfo() function show that approximately double that amount of
memory is being allocated. I am working with gcc 3.2.2 on a redhat 9
machine. I...
|
by: Roman Hartmann |
last post by:
hello,
I do have a question regarding structs. I have a struct (profil) which
has a pointer to another struct (point). The struct profil stores the
coordinates of points.
The problem is that I don't know how many points
there will be in every struct in the end, so I have to allocate memory
dynamically for
them and can't use an array of fixed size, unfortunately.
I would like to know if there is a better way to access struct members...
|
by: ira2402 |
last post by:
Hi All,
We are developing sw for a small embedded OS and we have limited
memory.
We are looking for algorithms, links, and articles about this.
The goal is efficient utilization of small amount of memory - means -
allocation for fixed length blocks / variable length blocks.
Thanks.
|
by: Rodrigo Dominguez |
last post by:
there are sometimes that I use third party libraries, I use some functions
that returns char * or structs, etc. sometimes the memory that is returned
by those libraries, when I try to free this memory whith the function free,
it brokes my application, and sometimes it's ok, why? how do I realize when
I have to free the memory that is allocated by third party libraries and
why sometimes I don't have to free this memory?
Thank you
| |
by: ranjeetasharma81 |
last post by:
Hi all,
I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am facing:
1- From structure and dynamic memory allocation point of view, the code is
very complicated. The code has various “nested structures” with a number
of levels. The size of memory allocated for pointer to structure or its
pointer...
|
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 *after* calling increase_arrays(), I
received segmentation fault. I tried to step it through gdb, and I
found out that after calling increase_arrays(), words's original
value is modified, and if I tried to access it, I get <address 0x11
out of...
|
by: George2 |
last post by:
Hello everyone,
Should I delete memory pointed by pointer a if there is bad_alloc when
allocating memory in memory pointed by pointer b? I am not sure
whether there will be memory leak if I do not delete a.
try {
a = new int ;
|
by: headware |
last post by:
Do you have to manually release memory allocated by creating a dynamic
array using ReDim? In other words, if I have the following code:
ReDim Test(1000)
For i = 0 To 1000
Test(i) = "test value " & i
Next
Do have I have set Test = Nothing to prevent a memory leak?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |