c

Frame Pointer / Program Counter / Array Overflow

I'm working on a practice problem set for C programming, and I've encountered this question. I'm not entirely sure what the question is asking for... given that xDEADBEEF is the halt instruction, but where do we inject deadbeef? why is the FP relevant in this question? thank you! You’ve been assigned as the lead computer engineer on an ...

Activation Record Length

I get 6,4,3 for the first 3 questions respectively, but I don't know how to figure out the last one. However, the solution manual indicated 7,5,4,18 as the answers. int sum(int x[], int N) { int k = 0; int s = 0; while (k < N) { s = s + x[k]; k = k + 1; } return s; // the activation record for sum will be ____________ ...

return address of memory allocated in function

Hi, When memory is allocated in a function, isn't it impossible to use that memory outside the function by returning its address? Are there exceptions? It seems the following is such an "example": const char * f() { return "HELLO"; } How to explain it? Thanks! ...

Measuring amount of CPU time taken by a piece of code, in C on Unix/Linux.

Hi, Can clock() be used as a dependable API to measure time taken by CPU to execute a snippet of code? When verified usng times() / clock(), both do not seem to measure the CPU time taken precisely. Firstly, can the APIs clock()/times() be used to measure the execution time of a function/snippet of code, as given in the example below?...

Glass Effect - Artistic Effect

I wish to give an effect to images, where the resultant image would appear as if we are looking at it through a textured glass (not plain/smooth)... Please help me in writing an algo to generate such an effect. Here's an example of the type of effect I'm looking for The first image is the original image and the second image is the outp...

Is it good practice to initialize array in C/C++?

I recently encountered a case where I need to compare two files (golden and expected) for verification of test results and even though the data written to both the files were same, the files does not match. On further investigation, I found that there is a structure which contains some integers and a char array of 64 bytes, and not all...

PostgreSQL : SQL timestamp to Unix timestamp using libpq

I know I can convert SQL timestamp to unix timestamp, using the following way. SELECT extract(epoch FROM now()); Now, I have a stored procedure function, which will directly return a table row to the caller. One of the row field is "timestamp" type. In my application, I am using libpq. I wish to use libpq functions (or any c/c++ func...

converting Biginteger to Bytearray(Raw data)

Hi i have used the following code for converting the bigint in decimal to bytearray (raw data) , but i am getting wrong result What is the mistake here. I am trying this in Apple Mac ( for Iphone app) COMP_BYTE_SIZE is 4 Is there any bigendian/ little endian issue , please Help. void bi_export(BI_CTX *ctx, bigint *x, uint8_t *data, i...

start "unrelated" 64 bit app from 32 bit app

Hi, is there an elegant way to start a 64 bit process from an 32 bit compiled application? The problem is that i need to compile one part in 32bit Sparc V8 mode. The other part is written for a faster 64 bit environment (no emulation on 64bit multiplication). I want to synchronize them using semaphore and communicate via shared memory. ...

How to check whether two file names point to the same physical file

I have a program that accepts two file names as arguments: it reads the first file in order to create the second file. How can I ensure that the program won't overwrite the first file? Restrictions: The method must keep working when the file system supports (soft or hard) links File permissions are fixed and it is only required that t...

C code involving {}

I saw this in some C code: Wininfo W = { sizeof(Wininfo) }; What the heck does this mean? ...

How g_main_loop works in gtk programming ?

I am new to GTK+ programming. I came across an API called g_main_loop(). I have used it in my code but I am still unaware that how exactly it works. Can somebody explain g_main_loop() with small code snippet? ...

UML for C programming language

UML is most commonly used for modelling system by using C++. In my projects C is the implementation language. I am looking for resources on UML strategies which are applicable for C. I want to use UML during design and represent the different aspects of the system. ...

how to raise warning if return value is disregarded - gcc or static code check?

I'd like to see all the places in my code (C++) which disregard return value of a function. How can I do it - with gcc or static code analysis tool? Bad code example: int f(int z) { return z + (z*2) + z/3 + z*z + 23; } int main() { int i = 7; f(i); ///// <<----- here I disregard the return value return 1; } Update: it ...

What is the correct type to use for declaring a metavariable that possibly could match either variables or members in a struct?

What is the correct type to use for declaring a metavariable that possibly could match either variables or members in a struct? Take for instance the following example source code: #include <stdio.h> #include <stdlib.h> struct some_struct { int i; char *s; }; void test(void) { struct some_struct *ptr; ...

How can I get/set a struct member by offset

Ignoring padding/alignment issues and given the following struct, what is best way to get and set the value of member_b without using the member name. struct mystruct { int member_a; int member_b; } struct mystruct *s = malloc(sizeof(struct mystruct)); Put another way; How would you express the following in terms of pointers/o...

When do I use xdata?

I am new at embedded system programming. I am working on a device that uses an 8051 chipset. I have noticed in the sample programs that when defining variables, sometimes they use the keyword xdata. like this... static unsigned char xdata PatternSize; while other times the xdata keyword is omitted. My understanding is that the xdata k...

Speed difference between using int and unsigned int when mixed with doubles

I have an application where part of the inner loop was basically: double sum = 0; for (int i = 0; i != N; ++i, ++data, ++x) sum += *data * x; If x is an unsigned int, then the code takes 3 times as long as with int! This was part of a larger code-base, but I got it down to the essentials: #include <iostream> ...

CPU-Core thread classification Function

I'm going to be writing a multi-threaded shared memory messaging system for ultra high-volume message delivery between processes. The messages will originate from the worker threads of a web-server. I'd like to exploit the CPU cache locality that cores on the same CPU share. So that when I wake up a worker thread on the receiving end of ...

interact (stdin/out) with command line programs at runtime in C

I think the thing I want to do is called GUI/command line wrapping sftp(1). I need an easy way to start that program and react on its output while running. Additionally I have to be able to send input to it, full interaction is required. I tried forkpty (emulated TTY), but there wasn't one good example findable using forkpty for that jo...