dynamic allocation memory

Hi, I'm trying to implement pointer instead of using stack memory. My program works well using stack memory, but as my program grows, I need to use heap memory instead

In my effort to use dynamic allocation, I got the error

05-08 12:49:25.101: A/libc(12515): @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree
05-08 12:49:25.101: A/libc(12515): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)

I tried to figure out, but could not make it out.

Here is my case.
I created a for loop to generate some data, and store those data into vector array. by using vector array, I manipulated the the order of element and then store those data into dynamic float array.

Previously, using stack array
 
  float myData[100000]; // Succesful, but the size is limited. 


Using dynamic array
1
2
3
4
5
6
7
8
9
10
11
float *myData;

myData = new (nothrow) float[myVectorarray.size()];

if(myData == 0){
cout<<"failed to allocate memory"<<endl;
}

for(int i = 0 ; i <myVectorarray.size() ;i++){
myData[i] = myVectorarray.at(i);
}


I also add to free the memory.
 
 delete [] myData;


Questions:
1. What are the errors trying to say?
2. I realise using stack array provide faster processing data then dynamic array. But I did not know which provide the biggest space to store the data?
3. Is there anything else I have to take into account when using dynamic allocation memory?


Thanks
Last edited on
I think that some other code is the reason of the error.
But I did not know which provide the biggest space to store the data?


Note sure what's you asking (what you mean by biggest space).
Both provide exactly the same amount of storage.
A dynamic allocation has some additional storage overhead which is used for storage management. The amount of this overhead varies with the platform and implementation, but is generally the size of two pointers per allocation.
A dynamically allocated array will give you access to all the memory available to your program which will usually be a much larger amount than what is reserved for the stack and there is no reason why it should be slower to process the data than if it were allocated on the stack.
The stack is easier to use when your data is small as the memory will be automatically freed when the object goes out of scope, whereas you have to keep track of dynamically allocated objects yourself and release the memory when it is no longer needed.
Also the error means you're trying to access memory that has not been allocated to your program. This could happen in the code you present if the new failed to allocate memory. In that case you should not access the mydata array on line 10
Last edited on
Hi Vlad,

So as you do not see any mistakes of this snippet code, I'm more confident now that I should step through other code and find what might be the problem. Thanks.

-------------------------------------------------------------------------------------------

Hi AbstractionAnon,

Yes, what I mean is which one provides the amount of storage.
Thanks for your comment.

-------------------------------------------------------------------------------------------

Hi Plover,

Also the error means you're trying to access memory that has not been allocated to your program. This could happen in the code you present if the new failed to allocate memory. In that case you should not access the mydata array on line 10


Yeah, but I used (nothrow), and it should point to zero when it failed to allocate memory and display predefined message, but it did not. Thanks anyway
closed account (zb0S216C)
Deduu wrote:
"1. What are the errors trying to say?"

"SIGSEGV" is an abbreviation for "signal: segment violation". It basically means that somewhere in your program, you've attempted to either free and/or access memory that your program does not own. Usually, the operating system responds by transmitting a signal, "SIGSEGV", to the troublesome application. When the program receives the signal, the default behaviour is to call "abort( )", which kills the process.

Duduu wrote:
"2. I realise using stack array provide faster processing data then dynamic array. But I did not know which provide the biggest space to store the data?"

First of all, accessing the stack is no faster than accessing heap-based memory. Usually, the default stack size is 1-MB in length whereas the heap's size is relative to the amount of system memory your operating system deems available for use. Use the stack for small, trivial types such as "float", "int" and "double" and use the heap for large arrays or large blocks of memory.

Deduu wrote:
"3. Is there anything else I have to take into account when using dynamic allocation memory? "

There's plenty to learn about DMA, but for now, just ensure all allocated memory is "delete []" (with "new[]") or "free( )'d" (with "malloc( )").

Deduu wrote:
"Yeah, but I used (nothrow), and it should point to zero when it failed to allocate memory"

If "new" fails, it'll return a null-pointer, even if it throws an exception; the standard guarantees it.

Wazzak
Hi Framework,

So do you see any mistake from this snippet code that can potentially cause I got the SIGSEGV? Are there other factor beside of what you've said that can make SIGSEGV occurs?

If "new" fails, it'll return a null-pointer, even if it throws an exception; the standard guarantees it.


Just as you said, I have prepared for the case whenever "new" fails to display error message, but when my program terminates due to SIGSEGV, I never saw that error message.

Also I did debuging to see the content of my dynamic array, and it contain the data as I expected thus never return a null-pointer. =D

Thankyou
closed account (zb0S216C)
Deduu wrote:
"So do you see any mistake from this snippet code that can potentially cause I got the SIGSEGV?"

When you dynamically allocate memory for "myData", does "myVectorarray" actually contain any elements?

Deduu wrote:
"Are there other factor beside of what you've said that can make SIGSEGV occurs?"

Not as far as I know of. Though, accessing a null-pointer can cause a segment-fault, but that pretty much falls under accessing memory that your program doesn't own. Also, you even said "new" returns a non-null pointer.

Wazzak
Hi,

When you dynamically allocate memory for "myData", does "myVectorarray" actually contain any elements?


absolutely yes. ^^

Though, accessing a null-pointer can cause a segment-fault, but that pretty much falls under accessing memory that your program doesn't own


Can you provide an example what might make our program access the memory that it doesn't own?

Thanks
closed account (zb0S216C)
Deduu wrote:
"absolutely yes. ^^"

...then your problem isn't within the code you posted. Post your code at http://pastebin.com/ then give us a link.

Deduu wrote:
"Can you provide an example what might make our program access the memory that it doesn't own?"

I honestly couldn't give an example. Usually, though, accessing an element beyond the end of an array means you're accessing unowned memory, but that may not always be the case.

Wazzak
Topic archived. No new replies allowed.