how to use *abs* section symbol - c

I generate a .o file based on a bin file, but when I use it .o file symbols, “start” and “end” can be output, but the “size” is always changing (start end suze is in the symbol of the. o file)
#include "stdio.h"
#include "stdlib.h"
// #include "ding.h"
extern char _binary_DingPCM_bin_start[];
extern char _binary_DingPCM_bin_end[];
extern char _binary_DingPCM_bin_size[];
int main()
{
char *pDingSound = NULL;
char *pDingSound_end = NULL;
pDingSound = (char*)_binary_DingPCM_bin_start;
pDingSound_end = (char*) _binary_DingPCM_bin_end ;
short int u32DingSoundSize = (long int)_binary_DingPCM_bin_size; //Ding PCM data bytes;
printf ("data_start %p\n", pDingSound);
printf ("data_end %p\n", pDingSound_end);
printf ("size %ld %d\n", pDingSound_end-pDingSound,u32DingSoundSize);
printf ("data_size %x\n", u32DingSoundSize);
printf("0x%hx Ding_sound\n",pDingSound[2]);
pause();
return 0;
}
I want to know why the u32DingSoundSize always changing and I want to use u32DingSoundSize

Related

Undefined reference when compiling a C file with a relative path header included

I cannot compile a C file that contain a function-call of a function, which is in another file. The compilation gives an error which says that there is an undefined reference even if I included the relative path to the Header file in the compilated file.
#include <stdio.h>
#include "../libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *d;
unsigned char *s;
size_t i;
d = (unsigned char *)dest;
s = (unsigned char *)src;
i = 0;
if (s < d)
{
while (n--)
d[n] = s[n];
}
else
ft_memcpy(d, s, n);
return (d);
}
int main()
{
char str[] = "memmove can be very useful.....";
ft_memmove (str+20, str+15, 11);
puts (str);
return (0);
}
The error that I get : gcc complier error
The header file : the header file
Can you help me please to resolve this problem ?

Unable to retrieve data from array of character inside structure

I am using below code in one file
file 1
//structure is global
struct abc
{
char var;
char *a[5];
}*p;
struct abc q;
int main()
{
char t[] = "sample"
p = &q;
p->a[0] = &t[0];
p->var = 10;
printf("var = %d, string = %s\n", p->var, p->a[0]);
func();
exit(0);
}
But if I try to access the structure member (a[]) in func() that is in another file I don't get the data that is assigned in another file (above).
file2
int fucn()
{
char var1;
var1 = p->var;
printf("var1 = %d\n", var1);
//since i am unable to copy p->a[0] to some other string i am trying to print the contents of p->a[0].
printf("a = %s\n", p->a[0]);
}
program crashes executing the second printf but I can print the content of p->var which is assigned in some other file.
Something like below is what you need.
An include file.
prompt> cat foo.h
struct abc {
char var;
char *a[5];
};
extern struct abc *p;
main function
prompt> cat main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "foo.h"
struct abc q, *p;
extern int func();
int
main()
{
p = &q;
/*
* Using strcpy after allocating memory.
*/
p->a[0] = malloc(strlen("zero") + 1);
strcpy(p->a[0], "zero");
/*
* strdup is equivalent to malloc and strcpy
*/
p->a[1] = strdup("one");
p->a[2] = "two";
p->a[3] = "three";
p->a[4] = "four"
p->var = 10;
printf("main var = %d, string = %s %s %s %s %s\n",
p->var, p->a[0], p->a[1], p->a[2], p->a[3], p->a[4]);
func();
return(0);
}
func function
prompt> cat func.c
#include <stdio.h>
#include "foo.h"
int
func()
{
int r;
r = printf("func var = %d, string = %s %s %s %s %s\n",
p->var, p->a[0], p->a[1], p->a[2], p->a[3], p->a[4]);
return(r);
}
Compile and run
prompt> gcc mainc.c func.c
prompt> a.out
main var = 10, string = zero one two three four
func var = 10, string = zero one two three four

Why can't I call my function(C)?

This is part of a program where I call a function that reads components from a ".dat" file and save the input to members of a Struct. When I try calling the function from my main.c it gives various errors depending on what I try. Most notably: conflicting types of 'ReadFile' and too few arguments to function 'ReadFile'. I also get a warning "passing argument from 'ReadFile' makes integer from pointer without cast" and some infos.
This is main.c
#include "MyData.h"
#include "NodalA.h"
#include "FileHandling.h"
#include <stdio.h>
#include "windows.h"
int main(){
ComponentType *CircuitData;
int numComp = 6;
int numEl = 0;
int numNodes = 0;
CircuitData = malloc((numComp)*sizeof(ComponentType));
ReadFile(CircuitData, &numEl, &numNodes);
return 0;
}
This is FileHandling.c:
#include "FileHandling.h"
#include "stdio.h"
void ReadFile(ComponentType *CircuitData, int *numEl, int *numNodes){
numEl = 0;
numNodes = 0;
int index = 0;
FILE *data;
data = fopen("mydata.dat", "r");
if (data == NULL){
printf("Error: \"mydata.dat\" could not be opened");
}
else {
while(!feof(data)){
fscanf(data, "%s, %s, %s, %f", CircuitData[index].name, CircuitData[index].node1, CircuitData[index].node2, CircuitData[index].value);
*CircuitData[index].node1 = extractInteger(CircuitData[index].node1);
*CircuitData[index].node2 = extractInteger(CircuitData[index].node2);
if(*CircuitData[index].node1 > *numNodes)
*numNodes = *CircuitData[index].node1;
if(*CircuitData[index].node2 > *numNodes)
*numNodes = *CircuitData[index].node2;
numEl++;
index++;
}
}
fclose(data);
}
And this is MyData.h
#ifndef MYDATA_H_
#define MYDATA_H_
typedef struct Comp{
char name[5]; //Name of circuit component
char node1[5], node2[5]; //2 nodes
float value[5]; //value
}ComponentType;
#endif /* MYDATA_H_ */
Any help would be appreciated. There are more code but I think this is the most important part.
The ReadFile function name used in the program is the same as a ReadFile function in "windows.h". The error "too few arguments to function 'ReadFile'" is most likely caused by the program trying to call the the function from windows with the wrong arguments. Removing "windows.h" or renaming the function ReadFile to something else solves the problem.

How to link main function with header file and create it's dll file?

I am very beginner to work with dll and linking various file.
I just know write main() function and all other in same .c file and run it.
I have one program which works for pattern matching. It takes the string and check whether it exist in entire text string or not. like
Text string: my name is john
string to be matched: name
Answer: Yes
main function is like this:
int main(int argc, const char *argv[])
{
char target[200];
char *ch = target;
char pattern[20];
int i,k,count,l;
printf("\nEnter the string: \n");
fgets(target,100,stdin);
printf("Enter the string to be matched: \n");
fgets(pattern,20,stdin);
l=strlen(pattern);
i = kmp(target, strlen(target)-1, pattern, strlen(pattern)-1);
//printf("I is : %d\n",i);
if (i == -1)
puts("False");
else
puts("True");
getch();
return 0;
}
Which calls function kmp() and get result back. We can also print the result in kmp() function. kmp() function is as follow:
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
int *pi = compute_prefix_function(pattern, psize);
int k = -1;
if (!pi)
return -1;
for (i = 0; i < tsize; i++) {
while (k > -1 && pattern[k+1] != target[i])
k = pi[k];
if (target[i] == pattern[k+1])
k++;
if (k == psize - 1) {
free(pi);
return i-k;
}
}
free(pi);
return -1;
}
In kmp we call compute_prefix_function(pattern, psize); which is as below:
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
int i = 1;
int *pi = malloc(sizeof(int)*psize);
if (!pi)
return NULL;
pi[0] = k;
for (i = 1; i < psize; i++) {
while (k > -1 && pattern[k+1] != pattern[i])
k = pi[k];
if (pattern[i] == pattern[k+1])
k++;
pi[i] = k;
}
return pi;
}
Header files need to be called:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
What I want to do is:
Creating an implementations in a dll/shared library format. essentially, the dll should have a function extension which take a string and return a bool saying if the string exists or not.
For that which function I need to put in .c file and header file and how to create .dll file for this?
I am using windows 7, VS 2010 and C programming.
Please explain me step by step.
I'll say more about the DLL further down, but for a start, here is the layout of the source files you'll need to do that.
You'll need three files:
main.c
kmp.h
kmp.c.
Code structure:
File main.c
#include <stdio.h>
#include "kmp.h" // this will make the kmp() function known to main()
int main(int argc, const char *argv[])
{
char target[200];
... same code as you aready have
}
File kmp.h
// prototype to make kmp() function known to external programs (via #include)
extern int kmp(char *target, int tsize, char *pattern, int psize);
File kmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// declare kmp prototype as DLL-export
_declspec(dllexport) int kmp(char *target, int tsize, char *pattern, int psize);
// prototype for internal helper function
static int *compute_prefix_function(char *pattern, int psize);
//
// implementation of kmp() function (and helper)
//
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
... same program code as you aready have
}
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
... same program code as you aready have
}
.
Now, for a first step, you can make these three files, and compile them in your current project (i.e. split your current project source into those three files, just leave out the line in kmp where it says __declspec(dllexport) and compile as before (non-DLL) to see if all works).
.
You will then need to create a DLL project for kmp.h and kmp.c (that will compile a KMP.DLL and KMP.LIB). Then you create a normal program (like your current sample) with main.c and need to link it with KMP.LIB / KMP.DLL
The following may be a bit fuzzy, because I only have VS2005 here, but the steps to create the DLL project should be essentially somewhat like this:
new project: Type Win32 / Win32-Project
name KMP
in the wizard choose Type DLL and check "Empty Project"
add your kmp.c and kmp.h files
In your main project (the one with the main.c program), you can then do
File Menu > Add > Existing Project > KMP.vcproj
This will automatically build and link the DLL from with your main.c program project.

File handling using API thread in C

Im making an application that uses of API-threads in C, The program takes N-files (N>2) with names disordered,per each file is generated a thread of execution which sort the files using the function qsort, after being ordered files, each thread should create a file keeping the original file intact and displaying the sorted file to another file with the extension <.sorted>. The program sorts the numbers without problems, even if I put standard output displays the result on screen, but when I try to create the output file with extension .sorted the program breaks out.
this is my code file.c
#include <stdio.h> /* Standard buffered input/output */
#include <stdlib.h> /* Standard library functions */
#include <string.h> /* String operations */
#include <pthread.h> /* Thread related functions */
#include "pf1.h" /* Header specific to this app */
pthread_attr_t attr;
void *thread_worker(void *name_file)
{
FILE *entrada, *salida;
char* nombres = (char*)name_file;
int numero;
char temp [10000];
int i;
stats_t estadisticas;
printf ("File_name:%s\n", nombres);
entrada = fopen(nombres, "r");
salida = fopen (strcat(nombres, ".sorted"), "w");
while (!feof(entrada)){
fscanf (entrada, "%s\n",temp);
numero++;
}
char* lista[numero]; //array to sort the file
rewind (entrada);
for (i=0;i<numero;i++)
{
fscanf(entrada," %[^\n]", temp);
lista[i] = (char*)malloc((strlen(temp)+1)*sizeof(char));
strcpy(lista[i], temp);
}
size_t large = sizeof(lista) / sizeof(char *);
qsort(lista,large ,sizeof(char *) ,cstring_cmp );
printf ("Archivo Ordenado\n", i+1);
for (i=0;i<large;i++)
printf("%s\n",lista[i]);
pthread_exit(NULL);
}
int main(int argc, char *argv [])
{
stats_t **stats;
int i, rc;
pthread_t my_threads[argc-1];
pthread_attr_init(&attr);
if (argc <3) {
printf ("|files| > 2\n");
}else{
printf("files to sorted: %d\n", argc - 1);
for (i = 1; i < argc; i++){
//printf("%s%s\n", argv[i], (i < argc-1) ? " " : "");
rc = pthread_create(&my_threads[i], &attr, thread_worker, (void *)argv[i]);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n",rc);
return -1;
}
}
}
return 0;
} /*end main */
this is mi file.h
#ifndef PF1_H_
#define PF1_H_
typedef struct _stats_t
{
char *longest, *shortest;
unsigned int numlines;
} stats_t;
int cstring_cmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return -strcasecmp(*ia, *ib);
/* strcmp functions works exactly as expected from
comparison function */
}
void print_cstring_array(char **array, size_t len)
{
size_t i;
for(i=0; i<len; i++)
printf("%s | ", array[i]);
putchar('\n');
}
#endif /* PF1_1_H_ */
I would like some help with this problem because I can not see which is the fault ... thanks to all in advance and excuse my English
This line here may be your problem:
salida = fopen (strcat(nombres, ".sorted"), "w");
From what I can tell, that nombres variable is coming from argv. Since you're not the one allocating memory for argv, you don't know that there will be extra space for the ".sorted" (and there probably won't be). If you strcpy it to your own buffer with space for the ".sorted", you should be fine.
#define EXT_LEN 7
#define MAX_TOTAL_LEN 250
#define MAX_FILE_LEN 242 //MAX_TOTAL_LEN - EXT_LEN - 1
char *name_ptr;
char nombres[MAX_TOTAL_LEN];
int len;
name_ptr = (char*)name_file;
len = strlen(name_ptr);
if (len > MAX_FILE_LEN) {
len = MAX_FILE_LEN;
}
strncpy(nombres, name_ptr, len);
strcpy(nombres+len, ".sorted");
salida = fopen (nombres, "w");
I once had issues about not passing an int identifier while calling thread execution functions. Try building a struct with both an integer identifier and the filename, then pass it as a parameter to your thread_worker() function.

Resources