aspose file tools*
The moose likes C / C++ and the fly likes Segmentation fault realloc array from a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Segmentation fault realloc array from a function" Watch "Segmentation fault realloc array from a function" New topic
Author

Segmentation fault realloc array from a function

andrea casini
Greenhorn

Joined: Jul 27, 2010
Posts: 13
Hello to everyone.

This is a really embarassing problem that I can't solve, probably because I don't remember very much the C language..

I give you a sample of my problem. Lets suppose I have this function "loading" which should allocate 5 elements of my vector v, The code should be like this:



Then from main I call the function


Now I tried everything malloc, realloc, using pointers and I always get that f****g segmentation fault error.

Please someone help...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
Try printing v with the %p tag before and after allocation. C is (like Java™) pass-by-value. Because you are not returning the pointer, you are not reassigning it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
Try . . . or
andrea casini
Greenhorn

Joined: Jul 27, 2010
Posts: 13
Thanks for reply..

I know that with a function it works but I can't do it with a procedure probably it's not even possible. I tried your code for printing and this is he output:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
You're welcome
When I tried without the v = bit, I got (nil) for both the pointer locations. The problem is that you are not returning a pointer, so the original pointer doesn't have enough space for your array. Try what I suggested yesterday in my second post.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Just as would be the case in Java, you're passing a pointer by value. When this method is called:

void loading(int *v)

, as Campbell points out, the pointer-to-int v in main() is copied into the pointer-to-int v in loading(). You assign a value to the local variable v in loading(), and the one in main() is unaffected. This is just what you'd expect in pass-by-value -- just like Java.

What you want is to pass a reference to a pointer-to-int, an easy thing to do. That would look like this:

void loading(int* &v) {

Then "v" in main() and "v" in loading() are the same variable, and when you assign to it in loading(), the variable in main() is affected. By adding that one single character to your original program, you'll find it works fine (once you fix the typos!)


[Jess in Action][AskingGoodQuestions]
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Ernest Friedman-Hill wrote:

What you want is to pass a reference to a pointer-to-int, an easy thing to do. That would look like this:

void loading(int* &v) {

Then "v" in main() and "v" in loading() are the same variable, and when you assign to it in loading(), the variable in main() is affected. By adding that one single character to your original program, you'll find it works fine (once you fix the typos!)


That would make the code C++ and no longer C.

Also, the OP is missing include directives and since the printf calls don't include a newline, the output stream is never flushed.


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Anand Hariharan wrote:
That would make the code C++ and no longer C.


Fine, whatever, call it



and call it like



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Segmentation fault realloc array from a function