By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Python calling C functions Segment Fault

P: 4
Hi, i'm new here so this is my first post. I have a problem and i decided to post it here in case somebody knows the answer.
I have a program in python which is doing bit manipulations. Due to the big size of those binary dumps i'm using and to speed thing up i decided to do the manipulations in C. I use the ctypes extention with libraries to achieve this.
So what i have is:
IN PYTHON:
- a BitStream which i'm deviding in a list of Bytes
ex: [10101010,01010101,....]
- Then i encode thos bytes into number with the function
int("10101010",2) of python which gives me a number.
- then i put all those numbers in an list.
- [34,35,65]
- then with a loop i send those numbers 1 by 1 to my C function.
IN C:
- my C function gets the element 1 by 1 as they come and stores them into an globaly declared array of integers.
PROBLEM:
- The problem i have is that after a certain time or number of transactions, i get an Segment fault error
in my case to test if the numbers arrive to my C function i always send a counter back to python.
i send 4096 values to my C function after getting 2552 values back i get the Segment Fault error.
Here is my Code:
Python code:
Expand|Select|Wrap|Line Numbers
  1. for i in range(len(IntDump)):
  2.     value = mylib.put_bin_into_array(IntDump[i])
  3.     print value
  4.  
C code:
Expand|Select|Wrap|Line Numbers
  1. int Array_Of_Numbers[4096][1];(global array)
  2. int Num_of_elements=0; (global variable)
  3.  
  4. int put_bin_into_array(int number)
  5. {
  6.    Array_Of_Numbers[Num_of_elements][0]=number;
  7.    Num_of_elements++;
  8.    return Num_of_elements;
  9. }
  10.  
Could somebody please help me i need this to work.
Thank You
Best Regards
Conter Thierry
May 20 '11 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 621
then i put all those numbers in an list.
- [34,35,65]
- then with a loop i send those numbers 1 by 1 to my C function as the file is read.
IN C:
There is no reason to store them in a list. Just send the numbers, one by one to the C function. A "Segment Fault" error is not enough information. It sometimes means that not enough memory has been allocated, possibly for the C array, especially since it happens at the same place.
May 20 '11 #2

P: 4
Well i have no other informations concerning the Segment Fault BUT i managed to get it to run. My error was the declaration of the array in C.
I declared it as:
Expand|Select|Wrap|Line Numbers
  1. int Array_Of_Numbers[4096][1]; 
istead of
Expand|Select|Wrap|Line Numbers
  1.  int Array_Of_Numbers[4096]; 
infact i know that it would be better to allocate the memory but i'm not that good in C
May 20 '11 #3

Expert 100+
P: 621
i know that it would be better to allocate the memory but i'm not that good in C
The problem is solved and the program works which is the important thing.
May 21 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.