Skip to content

Posts from the ‘C Noise’ Category

23
Feb

duplicate symbol MyFunction in xcode

If you get the following error when trying to compile a C (or Objective C) program in XCode:

duplicate symbol MyFunction

(null): 1 duplicate symbol for architecture x86_64

then the problem is that you’ve included a .c file using #include directive or #import directive instead of including the .h file.

If you’ve created some functions in a separate .c file (or .m file) don’t just import that file in your main file but create a header file too and move header definitions to that file.

16
Feb

How To Allocate Memory in C With malloc()

If you use pointers in C you must first allocate memory, before you will use the pointer.

To allocate memory in C we will use malloc() function which is defined in header. To free memory allocated with malloc() we will use free() function which is also located in header.

Example 1

See next example:

#include <stdio.h>
#include <stdlib.h>	// for malloc() and free() functions
 
#include <string.h>
 
int main(void) {
 
    char str1[9] = "a string";
 
    char *str2 = 0;	// it's safer to make str2 = 0
                        // which is usefull if we forget
                        // to alloc memory before we will
                        // use it
 
    str2 = (char*)malloc(9);	// will alocate memory
 
    strcpy(str2, str1);		// will copy str1 into str2
 
    printf("str1 value is = %s\n", str1);
 
    printf("str2 value is = %s\n", str2);
 
    free(str2);		// we deallocate memory allocated
                        // for str2
}

Read more »