0

I am receiving the error "User store segfault @ 0x000000007feff598" for a large convolution operation. I have defined the resultant array as

        int t3_isize = 0;
        int t3_irowcount = 0;
        t3_irowcount=atoi(argv[2]);
        t3_isize = atoi(argv[3]);
        int iarray_size = t3_isize*t3_irowcount;

        uint64_t t_result[iarray_size];

I noticed that if the array size is less than 2^16 - 1, the operation doesn't fail, but for the array size 2^16 or higher, I get the segfault error.

Any idea why this is happening? And how can i rectify this?

  • What platform are you using, and are you trying to put that array on the stack? – Phillip Kinkade Dec 10 '13 at 06:29
  • Is this array global or on the stack? – Retired Ninja Dec 10 '13 at 06:29
  • 1
    8 * 65536 = 524288 bytes for that array. I smell a stack overrun. You may do well to [**read this answer**](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap#79936), as it is easily one of the very best posted on SO. – WhozCraig Dec 10 '13 at 06:29
  • Did you mean **less than** ` 2^16 - 1` just to avoid further confusion. – qrikko Dec 10 '13 at 06:36
  • Yes sorry about that typo. – user3039976 Dec 10 '13 at 07:12
  • `std::cout << iarray_size << std::endl;` I'm curious what that says right *before* your array/vector sizing is done, *exactly* as I showed. And you may want to mention the platform and tools you're using, as well as a debugger walk through this code, as using a `std::vector t_result(iarray_size);` will occupy literally 12-bytes on a normal stack. I don't supposed you're returning this "thing" from a function, are you? (the name somewhat hints to that). – WhozCraig Dec 10 '13 at 07:50
  • I am on Linux XXXXXXXX 2.6.32-5-amd64 #1 SMP Sun May 6 04:00:17 UTC 2012 x86_64 GNU/Linux std::cout << iarray_size << std::endl; prints 90000, which is the expected array size. – user3039976 Dec 10 '13 at 08:05

3 Answers3

0

“I noticed that if the array size is greater than 2^16 - 1, the operation doesn't fail, but for the array size 2^16 or higher, I get the segfault error”

↑ Seems a bit self-contradictory.

But probably you're just allocating a too large array on the stack. Using dynamic memory allocation (e.g., just switch to using std::vector) you avoid that problem. For example:

std::vector<uint64_t> t_result(iarray_size);

In passing, I would ditch the Hungarian notation-like prefixes. For example, t_ reads like this is a type. The time for Hungarian notation was late 1980's, and its purpose was to support Microsoft's Programmer's Workbench, a now dicontinued (for very long) product.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

You're probably declaring too large of an array for the stack. 216 elements of 8 bytes each is quite a lot (512K bytes).

If you just need static allocation, move the array to file scope.

Otherwise, consider using std::vector, which will allocate storage from the heap and manage it for you.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • How is `iarray_size` declared? And are you remembering to either declare your `vector` with the desired initial size, or at least calling `resize()` on it? – Joe Z Dec 10 '13 at 07:24
  • iarray_size is being calculated in the previous line and does not change after that. I am declaring the vector like this: std::vector t_result(iarray_size); – user3039976 Dec 10 '13 at 07:27
  • @user3039976 Is `iarray_size` at least of type `int` or larger? Can you edit your question to show that calculation, also? – Joe Z Dec 10 '13 at 07:29
  • @user3039976 : Hmmm, that looks pretty innocuous. The actual fault you're getting (the store) must be further down when you try to write something to this. The 2^16 boundary makes it feel weird, like something got truncated to a `short` or something... but I don't see anything immediately that jumps out. – Joe Z Dec 10 '13 at 07:35
0

Using malloc() solved the issue.

            uint64_t* t_result = (uint64_t*) malloc(sizeof(uint64_t)*iarray_size);