A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Create a Perl module structure

h2xs -cn MyModule

Freeing memory in C

If you allocate memory in a subroutine for a variable that you then return, you’ve got to free the variable you return:
 
char * allocstring(char * text)
{
    char * s = malloc(s, strlen(text)+1);
    strcpy(s, text);
    return s;
}
 
void main()
{
    char * s = allocstring(“Hello World”);
    free(s);
}

Regular expressions in C: Advice

If you can avoid using regular expressions in C, then *do not use them!!*

Instead try the strstr (find substring) and strtok (like perl's split) routines.

These warnings:

warning: initialization makes pointer from integer without a cast
warning: assignment makes pointer from integer without a cast

...for a line containing malloc could mean that you forgot to #include "stdlib.h".
Functions are assumed to return an integer type by default if not declared.

C printf and scanf format specifiers

Format Specifier

Type

%d (or %i)

int

%c

char

%f

float

%lf

double

%s

string

%xhexadecimal

Comparing strings in C

In c it is not possible to directly compare two strings so a statement like if (string1==string2) is not valid. Most c libraries contain a function called the strcmp().This is used to compare two strings in the following manner.

if(strcmp(name1,name2)==0)
 puts("The names are the same");
else
 puts("The names are not the same.");

Thanks johnt

debugging technique

When programming C, it can be difficult to track down a segmentation fault.
It is often caused by attempting to read uninitialised memory.

Try skipping parts of the program until you locate the statement causing it.

Then replace each part of the data by hard-coded values, one by one, until you find the uninitialised variable/pointer.

c logging

if your fprintf statements don't reach the apache error log, you may have to flush the output buffer:

fprintf(stderr, "error statement\n");
fflush(stderr);


Logging in an apache handler

ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "debug message");

C pointer error

This error:

    request for member `xxx' in something not a structure or union

...could mean you need to use structure->member instead of structure.member, as sturcture is a pointer.

c function errors

Errors like these:

previous implicit declaration of `function'

`function' was previously implicitly declared to return `int'

...point to a missing function declaration at the top of the file.

Concatenate C strings, and return them from a function

Don't do this. Just use strcat() and strncpy() instead.

#include <stdlib.h>

char* concat(char*, char*);

int main (int argc, char** argv)
{
char *x = "something";
char *y = " completely different.";
  char *combined = concat(x, y);
printf("And now for %s\n", combined);
}

char* concat(char *a, char *b)
{
  char *target = malloc( strlen(a) + strlen(b) + 1 );
  strcpy(target,a);
strcat(target,b);
return target;
}

C static

The keyword 'static' before a function name means that the function will only be available to callers in the file where it is declared.

Regular expressions in C

Using glibc's regex.h
  • Make sure the number of expected matches (nmatch) is high enough, or there will be garbage at the end of the matchptr array
  • If regexec() succeeds [returns zero], matchptr contains:
    • 0: the whole regex
    • 1: first parenthesis match
  • if regexec() fails [returns a value], don't even look in matchptr
  • Characters that must be escaped:
    • parentheses
    • plus signs
  • If you're matching the end of line, it must be inside any parenthesis!

location of C libs on nolsearch09

according to gcc -print-search-dirs

/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../bcc/include

or

/usr/lib/bcc/include