Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i was wondering if i freed the first line in a column if i could still access the rest of the array for example:
my array : 1,2,3,4 5,6,7,8
if i freed the 3 , could i access the last column?
jeron1 14-Jan-15 14:22pm    
Your question is confusing (at least to me). Perhaps show how you are declaring your array, and maybe describe what you mean by 'freed', and show a small 2d example.
Sergey Alexandrovich Kryukov 14-Jan-15 14:23pm    
I think this is clear from the content. But yes, "freeing" depends on how memory is allocated and is generally a wrong idea. Please see Solution 1.

Wrong idea. Arrays are based on using of continuous memory chunks and are not suitable for removing. If want to do this, you will need to reallocate the memory and, essentially, rewrite some part of original array or all of it. This is an inefficient operation which would defeat the purpose of the array's efficiency. Therefore, you should better use some other data structure suitable for deletion, such as vector or linked list, as with the template classes vector or list found in the standard C++ library:[^],[^].

For example, "2D arrays" can be represented as vectors of vectors, lists of lists, vectors of list or lists of vectors, depending on your requirements. In fact, that will make the "jagged arrays", which would be fine for your purposes. See also:[^].

I also want to point out that, due the nature of C and C++ arrays, "1D array" and "2D array" is exactly the same thing. The multi-index access to the array elements is nothing but syntactic sugar:[^].

Share this answer
CPallini 14-Jan-15 15:54pm    
5. Assuming the OP meant "removing an intem from the array".
Sergey Alexandrovich Kryukov 14-Jan-15 17:00pm    
If it says "freed", it certainly meant to be removed as well. :-)
Thank you, Carlo.
Your sample array doesn't look two-dimensional to me.

Generally speaking you cannot free anything unless it is a pointer to dynamic allocated memory, thus an item in a array of integers cannot be freed.
If you meant: "Could I remove an item in an array?" then the answer is "yes, you could do it" but it wouldn't be plain simple (have a look at Sergey's answer).
Share this answer
Sergey Alexandrovich Kryukov 14-Jan-15 17:01pm    
Sure, correct clarification, 5ed.
Probably, OP's "2D" is suggested by the lack of ',' after '4', so it could be understood as {{1,2,3,4}, {5,6,7,8}}... no matter. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900