473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Python calling C functions Segment Fault

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
3 2307
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
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

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

Similar topics

3
by: Charles Larry | last post by:
Is there a way to define functions after the main part of a Python script? Example: #!/usr/local/bin/python # this code yields a NameError print_message("hello world")
18
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
8
by: Steven Bethard | last post by:
I'm playing around with some threading stuff right now, and I'm having a little trouble calling a function from one thread that affects another. Here's my setup: py> import os, threading, time...
1
by: Ben | last post by:
I am having trouble calling functions in another file.. Lets look at the scenario below: File xyz.cc: (I have both xyz.h and xyz.cc, for simplicity I've avoided xyz.h) ------------ struct xyz...
2
by: linq936 | last post by:
Hi, I am using gcc3.2.3 as the compiler for my c++ code on Red Hat Linux Enterprise 3. I find that even an exception is caught, "Segment Fault" is still thrown out and program crashes. Here is...
2
by: anthonyberet | last post by:
This is the first time I have tried out functions (is that the main way of making subroutines in Python?) Anyway, my function, mutate, below #make a child string by randomly changing one...
2
by: Rolf Wester | last post by:
Hi, I' trying to make an extension module that passes Numeric arrays. The wrapper function is (swig generated and modified by myself): static PyObject *_wrap_my_func(PyObject *self, PyObject...
3
by: Juggernaut | last post by:
I have a program that compiles without errors, but when I run it I get a segment fault. I have managed to trace where it happens, but since im new to C I don't know how to fix this or how I did...
1
by: pascal | last post by:
hi friends i am new to C on Linux env. My prob is why do we not get a segment fault when we try to find the value at 0 memory location where as a segment fault is shown if attempt is made to...
1
by: freestyle12 | last post by:
Hey i've been trying to get this to work for a while now but nothing i do fixe it. causes a seg fault at strcat. Here is the code snippet: int i=1; char *opt = " "; char s = '-'; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.