asilter 0 Junior Poster in Training

You are using the wrong function family. exec() isn't supposed to return.

Use one of the spawn() functions instead.

Hope this helps.

I used spawnl instead of execl, but it gives compile time error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <mysql/mysql.h>

.....
spawnl(P_WAIT,"/home/user1/exec_work/binary","/home/user1/exec_work/binary","serdar","ilter",NULL);

it says:
`P_WAIT' undeclared (first use in this function)

what header file am i missing? If I put 1 instead of P_WAIT, this time it says that spawnl undeclared.

asilter 0 Junior Poster in Training

i have 2 executable files. From one of them i'm calling the other one. it is also executed correctly, but does not return 0.

why?

backup.c file :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <mysql/mysql.h>


int main(int argc, char * argv[])
{


   if(!execl("/home/user1/exec_work/binary","/home/user1/exec_work/binary","serdar","ilter",NULL))
   {
      /* This line is not printed to the screen. */
      printf("0\n");
   }
   else
   {
      /* This line is not printed to the screen either. */
      printf("1\n");
   }

   return 1;

}

The other source file:

#include <stdio.h>
int main(int argc, char * argv[])
{

/* I see that this line is printed to the screen */
   printf("%s %s\n",argv[1],argv[2]);

   return 0;

}
asilter 0 Junior Poster in Training

Hi,

The system under my program is using MySql version 4.0.18. I want to have a documentation for using MySql Transactions API from my C program.

Could you give some link?

thank you.

asilter 0 Junior Poster in Training

i have a loop displaying current number of file which is being created.

for(i=1;i<=nFileCount;i++)
{
     ...
     printf("%d. file has been created\n",i);
}

but i dont want the display to write ". file has been created." again and again as a list. I want only the i change. how can i do that?

thanx.

asilter 0 Junior Poster in Training

unsigned int is 4 bytes in the system i'm programming. i printed sizeof(unsigned int) and it said 4.

int main(int argc, char * argv[])
{
        unsigned int a = 0;
        long int b = 2607503366;
/* the b data type must strictly be a long int */ 
        a = (unsigned int) b;
        printf("%d\n",a);
        return 0;
}

it prints out;

-1687463930

but it must print the same thing. what am i doing wrong?

thanks.

asilter 0 Junior Poster in Training
unsigned long nBolum = 0L;
/* I have changed this line to : */
/* unsigned int nBolum = 0; */
 
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned long) ulA/ulB;
 
/* I have changed this line to : */
/* nBolum = (unsigned int) ulA/ulB;*/
 
        nKalan = (unsigned long) ulA%ulB;
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        printf("HVFE.c exiting\n");
        exit(1);

it prints :

ulA=2607503366
ulB=16777215
 
nBolum = 65435
nKalan = 7100321

to the screen.

but nBolum must be 155.419.... Why is this happenning?

thanx.

change the line above and nBolum problem is gone. But I also see that nKalan is not being calculated correctly. I have opened another topic for the problem.

asilter 0 Junior Poster in Training
unsigned int nBolum = 0;
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned int) ulA/ulB;
        nKalan = (unsigned long) (ulA%ulB);
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        ...

it prints;

ulA=2607503366
ulB=16777215
nBolum = 155
nKalan = 7100321

to the screen.

But the nKalan value(the remaining value from the division) must be 7035041. why is it calculating it wrong?
what is the problem?

thanks

asilter 0 Junior Poster in Training

You can't possibly get 155.419 from an unsigned long, there is no decimal point from integers. You need float or double.

And dividing two integers always gives an integer, even if you load it into a floating point number. For example:

double dval;
dval = 25/2;

12.000 is loaded into dval, not 12.5.

i want to get 155 as integer.

asilter 0 Junior Poster in Training
unsigned long nBolum = 0L;
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned long) ulA/ulB;
        nKalan = (unsigned long) ulA%ulB;
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        printf("HVFE.c exiting\n");
        exit(1);

it prints :

ulA=2607503366
ulB=16777215
 
nBolum = 65435
nKalan = 7100321

to the screen.

but nBolum must be 155.419.... Why is this happenning?

thanx.

asilter 0 Junior Poster in Training

my program is written in c and compiled and linked with gcc. In normal way; i execute it by giving an argument after writing executable.

./my_executable abc.dat

and it works.

i need to execute it in gdb debugger. how will i give this abc.dat argument to the gdb debugger? could u plz give a little sample?

thanx

asilter 0 Junior Poster in Training

what do u mean by boost library?

asilter 0 Junior Poster in Training
#include <iostream>
using namespace std;
 
int main(int argc, char * argv[])
{
        hash<const char*> H;
        cout << "foo() ->" << H("foo()") << endl;
}

compilation error after
g++ -c -g main.cc
is :

main.cc: In function `int main(int, char**)':
main.cc:8: error: `hash' undeclared (first use this function)
main.cc:8: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)
main.cc:8: error: syntax error before `char'
main.cc:9: error: `H' undeclared (first use this function)

why?
thanx

asilter 0 Junior Poster in Training

link and your explanation helped me.
thank you very much.

asilter 0 Junior Poster in Training

http://cboard.cprogramming.com/showthread.php?t=93997

i also asked this question to that forum, and the thread is going on.

Originally Posted by laserlight
They mark the beginning of the initialisation list. The ok_flag and time_range member variables are initialised to false, while the prefix member variable is initialised using its default constructor.

ok_flag and time_range are the variables, and prefix is a class.

did i understand correctly?

asilter 0 Junior Poster in Training

i'm trying to read and understand a .cc (c++)file,
i want to know what does ok_flag(false),time_range(false),prefix() do after ":" ?
I did know that ":" was being used for class inheritance but what is ":" for in the bleow code?

ClassA::ClassA(const char *db) : ok_flag(false),time_range(false),prefix()
{
//.. Constructor statements
}

plz help
thanx.

asilter 0 Junior Poster in Training

when the user clicks on a link to download a file form the server, is there any way of getting information whether his download is complete or not?

or can there be an exposed active x event handler of download status, so that linking with javascript and AJAX could send a message to the server?

asilter 0 Junior Poster in Training

i have a .so(shared object) file and i want to debug into it.

could you plz give a little gdb debugger code sample which is adding a shared object for debugging.

PS : My executable is Test.x and I know to start with

gdb Test.x

:)

could u take me further?
thanks.

asilter 0 Junior Poster in Training

how do i send a multidimensional char array to a function.

i try assigning values like this :

char ChrArrayAttributes[2][20];
 
strcpy(ChrArrayAttributes[0],"TIME");                                        
strcpy(ChrArrayAttributes[1],"IDENT");

and then sending it like this:

WriteValues("/pp1/reb","RADR",ChrArrayAttributes);

when i compile it gives a warning:

Test.c:115: warning: passing arg 3 of `WriteValues' from incompatible pointer type

and when i debug with gdb debugger, just before entering into WriteValues function, it gives segmentation fault. It never enters in.

why?

WriteValues function is here:

void WriteValues(char * sDBName, char * sTableName, char * ChrArrayAttr[])
{
      int i = 0;
      /* Trying to write values TIME and IDENT to the screen */
      for(i=0; i<2; i++)
      {
             printf("ChrArrayAttr[%d]=%s\n",i,ChrArrayAttr[i]);
      }
}

thanx.

asilter 0 Junior Poster in Training

when i,

g++ -c Serdar.cc

compiler says:

Serdar.cc:3: error: semicolon missing after declaration of `Serdar'
Serdar.cc:4: error: ISO C++ forbids defining types within return type
Serdar.cc:4: error: two or more data types in declaration of `SetValue'
Serdar.cc:4: error: prototype for `Serdar Serdar::SetValue(const int&)' does 
   not match any in class `Serdar'
Serdar.hh:5: error: candidate is: void Serdar::SetValue(const int&)
Serdar.cc:4: error: `Serdar Serdar::SetValue(const int&)' and `void 
   Serdar::SetValue(const int&)' cannot be overloaded
Serdar.cc:4: error: semicolon missing after declaration of `class Serdar'
Serdar.cc: In member function `int& Serdar::GetValue()':
Serdar.cc:10: error: request for member `deger' in `this', which is of 
   non-class type `Serdar* const'

Serdar.hh file:

class Serdar
{
        public:
                void SetValue(const int &a);
                int & GetValue();
        private:
                int deger;
}

Serdar.cc file:

#include "Serdar.hh"
void Serdar::SetValue(const int &a)
{
        deger = a;
}
int & Serdar::GetValue()
{
        return & this.deger;
}
asilter 0 Junior Poster in Training
typedef map<string,Attr_info,less<string> > AttrMap;

i do not know what "map<", less and ">" represents?

What I little understood from the code is AttrMap is a collection of Attr_infos. If so how can i add a Attr_info into AttrMap?

could u plz help?
thanx

asilter 0 Junior Poster in Training
typedef  map<string, Attr_info, less<string> > AttrMap;

i do not know what "map<", less and ">" represents?

What I little understood from the code is AttrMap is a collection of Attr_infos. If so how can i add a Attr_info into AttrMap?

could u plz help?
thanx

asilter 0 Junior Poster in Training

i've attached the makefile.

asilter 0 Junior Poster in Training

Make 'all' depend on the executables, not the intermediate object files.

you should have meant the opposite I think. Because if there's a change in c file, or there is no object file, target is done. So it's object dependent. I think you mean that I can't do that with executables as if I can with object dependency in Makefile.

Am I right?

asilter 0 Junior Poster in Training

then why can't the compiler see
HVFUTILCFLAGS addition with CFLAGS?

Can't we assign variables in Makefile? Variables assigned in Makefile seems to be empty sometime. So for example compiler can't see the include folder assigned to the variable with -I.

asilter 0 Junior Poster in Training
CC       = gcc
CFLAGS   = -I ../include/
LD       = gcc
EmpCC    = empcc
HVFUTILLDFLAGS = -L/home/empress/HamVeriFeeder/hvfutil/lib -lhvfutil
 
all: BUFR_Decoder.o HVFD.o BUFR_Printer.o
clean:
        rm -f BUFR_Decoder.o HVFD.o BUFR_Printer.o ../lib/libhvfd.a ../bin/BUFR_Printer
BUFR_Decoder: BUFR_Decoder.o
        $(CC) -g -c BUFR_Decoder.c $(CFLAGS)
HVFD: HVFD.o
        $(CC) -g -c HVFD.c $(CFLAGS)
BUFR_Printer: BUFR_Printer.o ../bin/BUFR_Printer
        $(CC) -g -c BUFR_Printer.c $(CFLAGS)
BUFR_Printer.x: ../bin/BUFR_Printer
        $(CC) -o ../bin/BUFR_Printer BUFR_Printer.o BUFR_Decoder.o $(HVFUTILLDFLAGS)
cleanBP:
        rm -f BUFR_Printer.o BUFR_Printer.x

when i do make, it says:
gcc -I ../include/ -c -o BUFR_Decoder.o BUFR_Decoder.c
gcc -I ../include/ -c -o HVFD.o HVFD.c
gcc -I ../include/ -c -o BUFR_Printer.o BUFR_Printer.c


what i want to do is :
if BUFR_Encoder.o is compiled then make BUFR_Encoder executable.

but only BUFR_Encoder.o is compiled, it does not make the executable.

asilter 0 Junior Poster in Training

how to tell more than one include folders to the compiler

Makefile :

CC              = gcc
CFLAGS          = -I../include
HVFUTILCFLAGS   = -I/home/empress/HamVeriFeeder/hvfutil/include/
all: HVFE.o RdbMapping.o BUFR_Encoder.o
clean:
        rm -f BUFR_Encoder.o HVFE.o RdbMapping.o ../lib/libhvfe.a *.bufr *.dat
BUFRENCODER_CFLAGS = $(CFLAGS) $(HVFUTILCFLAGS)
BUFR_Encoder: BUFR_Encoder.o
        $(CC) -g -c BUFR_Encoder.c $(BUFRENCODER_CFLAGS)
HVFE: HVFE.o
        $(CC) -g -c HVFE.c $(CFLAGS)
RdbMapping: RdbMapping.o
        $(CC) -g -c RdbMapping.c $(CFLAGS)

when i make it says:
gcc -I../include -c -o BUFR_Encoder.o BUFR_Encoder.c
BUFR_Encoder.c:15:23: ArrayUtil.h: No such file or directory
make: *** [BUFR_Encoder.o] Error 1


BUFR_Encoder.c could not be compiled, because an included header(ArrayUtil.h) cannot be found.

why doesn't it see the HVFUTILCFLAGS?
thanx.

asilter 0 Junior Poster in Training

could u plz give 4 little files-sample (c and c++ code and header files) which shows how to call c++ class public member function.

thanx.

asilter 0 Junior Poster in Training

So how come your compiler didn't warn you of the mis-match?

Because CreateRawDataBUFR function is in a library, in the Makefile there is a linker flag.

Put the prototype in a header file, and include that file where you define the function, and wherever you use the function. Then you will get a warning if any declaration or use is mis-matched.

Simply writing extern void * CreateRawDataBUFR.... in the C file where you use it offers no prospect for error checking.

And thank you so much, I'll do that.

asilter 0 Junior Poster in Training

CreateRawDataBUFR function takes 3 parameters, but i send it 4. I saw it in my dream today in the morning :)

Thanx everyone.

asilter 0 Junior Poster in Training
void CreateRawDataBUFR(unsigned char ** ucppBUFR, int * bufrSize, RawDataInterface * rdi)
{
        MAIN_DATA MainData;
        /* Tam Dosya yolu */
        char sRawFilePath[120];
        char sTableBName[23];
        char sTableCName[23];
        char sTableNamesExtension[18];
        char sTableNamesExtensionAfterDot[4];
        int nDDWOR;
        int nDDWR;
        int nVWOR;
        int nAdditiveBufrBitCount = 0;
        int nVWR;
        long lBufrSize;
        char sPieceFileName[10];
        char sPieceBufrFileName[12];
        char sPieceFileNameNumber[3];
        int nMaxBufrMessageSize = 16777215;
        /* int nMaxBufrMessageSize = 99; */
        /* int nMaxBufrMessageSize = 1600; */
        int nMoreBufrMessageFileSize = 31;
        InitializeCharArray(sPieceFileName,10);
        InitializeCharArray(sPieceBufrFileName,12);
        InitializeCharArray(sPieceFileNameNumber,3);
        /* int * nFileSize = malloc(4); */
        /* int * nAdditiveBulkDataSize = malloc(4); */
        int i=0;
 
        InitializeCharArray(sTableBName,23);
        InitializeCharArray(sTableCName,23);
        InitializeCharArray(sTableNamesExtension,18);
        InitializeCharArray(sTableNamesExtensionAfterDot,4);
        strcpy(sTableNamesExtensionAfterDot,"TXT");
        /* MainData initialization */
        InitializeMainData(&MainData);
        InitializeCharArray(sRawFilePath,120);
        /* *nAdditiveBulkDataSize = 0; */
/********************************************************/
        CopyRawDataInterface(&MainData,&rdi);
/********************************************************/
        GetRdbType(&MainData.nRdbType,MainData.rdi.sIdentName);
        GetRdbSubtype(&MainData.nRdbSubtype,MainData.rdi.sIdentName);
        strcpy(sRawFilePath,MainData.rdi.sRawDataFilePath);
        sRawFilePath[strlen(sRawFilePath)] = '\0';
        strcpy(MainData.sFullFilePath,sRawFilePath);
        strcat(MainData.sFullFilePath,MainData.rdi.sRawDataFileName);
        MainData.sFullFilePath[strlen(MainData.sFullFilePath)] = '\0';
        printf("Aranacak dosya yolu : %s\n",MainData.sFullFilePath);
        printf("(HVFE.c) MainData.sFullFilePath(%d) = %s\n",strlen(MainData.sFullFilePath), MainData.sFullFilePath);
        /* File Size hesaplaniyor */
        CalculateFileSize1(MainData.sFullFilePath,&MainData.nFileSize);
        printf("(HVFE.c) MainData.nFileSize = %d\n",MainData.nFileSize);
        /* Creating B,C reference tables */
        CreateTableNamesExtension1(sTableNamesExtension);
        CreateTableBName1(sTableBName,sTableNamesExtension,sTableNamesExtensionAfterDot);
        CreateTableCName1(sTableCName,sTableNamesExtension,sTableNamesExtensionAfterDot);
        printf("(HVFE.c) sTableBName = %s\n",sTableBName);
        printf("(HVFE.c) sTableCName = %s\n",sTableCName);
        SetDataDescriptors(&MainData);
        printf("(HVFE.c) MainData.saDataDescriptor[0,1]=%s(%d),%s(%d)\n",MainData.saDataDescriptor[0],strlen(MainData.saDataDescriptor[0]),MainData.saDataDescriptor[1],strlen(MainData.saDataDescriptor[1]));
        SetTableElements(&MainData, sTableBName, sTableCName);
 
        CalculateChunkCount1(&MainData.nChunkCount,MainData.nFileSize,MainData.nMaxSize);
/*
        printf("md->nFileSize = %d\n",MainData.nFileSize);
        printf("md->nMaxSize = %d\n",MainData.nMaxSize);
        printf("md->nAddditiveChunkSize = %d\n",MainData.nAdditiveChunkSize);
        printf("md->nChunkCount = %d\n", MainData.nChunkCount);
*/
 
/*
        for(i=0;i<2;i++)
        printf("(HVFE.c) MainData.tBElem[%d] = %s,%s,%s,%s(%d),%s(%d),%s(%d)\n",i,MainData.tBElem[i].sDataDescriptor,MainData.tBElem[i].sElementName,MainData.tBElem[i].sElementType,MainData.tBElem[i].sExp,MainData.tBElem[i].nExp,MainData.tBElem[i].sMinus,MainData.tBElem[i].nMinus,MainData.tBElem[i].sBitsToRead,MainData.tBElem[i].nBitsToRead);
*/
        CalculateDataDescriptorByteSizeOtherThanRawData(&MainData,&nDDWOR);
        printf("nDDWOR = %d\n", nDDWOR);
        CalculateDataDescriptorByteSizeOfRawData(&MainData,&nDDWR);
        printf("nDDWR = %d\n", nDDWR);
        CalculateValueByteSizeOtherThanRawData(&MainData,&nVWOR);
        MainData.nValueByteSizeOtherThanRawData = nVWOR;
        printf("nVWOR = %d\n", nVWOR);
        printf("nAdditiveBufrBitCount = %d\n",nAdditiveBufrBitCount);
        CalculateValueByteSizeOfRawData(&MainData, &nVWR, MainData.nChunkCount);
        printf("nVWR = %d\n", nVWR);
        CalculateBufrSize(&lBufrSize,&MainData,nDDWOR,nVWOR,nDDWR,nVWR,nAdditiveBufrBitCount);
        printf("lbufrSize = %ld\n",lBufrSize);
/*
*/
        if(lBufrSize > nMaxBufrMessageSize)
        {
                /* Dosya parcalara bolunecek. */
                DivideFileIntoPieces(&MainData,lBufrSize,nMaxBufrMessageSize);
                printf("(HVFE.c) Total BUFR Message Count = %d\n",MainData.nTotalBufrMessageCount);
                for(i=0; i<MainData.nTotalBufrMessageCount; i++)
                {
                        strcat(sPieceFileName,"raw");
                        sprintf(sPieceFileNameNumber,"%d",(i+1));
                        strcat(sPieceFileName,sPieceFileNameNumber);
                        strcat(sPieceFileName,".dat");
                        strcpy(MainData.sFullFilePath,sPieceFileName);
                        CalculateFileSize1(MainData.sFullFilePath,&MainData.nFileSize);
                        CalculateChunkCount1(&MainData.nChunkCount,MainData.nFileSize,MainData.nMaxSize);
                        CalculateAdditive1(MainData.nFileSize,MainData.nMaxSize,&MainData.nAdditiveChunkSize);
 
                        CreateHVFEMachineBUFR(ucppBUFR,bufrSize,&MainData);
                        nMoreBufrMessageFileSize …
asilter 0 Junior Poster in Training

Variables in the data structure changes when they were sent to CreateRawDataBUFR at runtime. With gdb debugger, just one line before CreateRawDataBUFR, I print the value of rdi.sIdentName, rdi.sRawDataFilePath and rdi.sRawDataFileName to the screen, they are "IST" and "data/" "IST050906125859.RAWADM9" accordingly. They are true. But after i send the address of rdi to CreateRawDataBUFR, under CreateRawDataBUFR statement, i again print them with debugger. rdi.sIdentName is "IST0509061", rdi.sRawDataFilePath is "RAWADM9" , rdi.sRawDataFileName is something like "*\000\000\000\000ïÿ¿". why did they mixed each other.

Where did i do wrong?

Data structure is shown below.

typedef struct raw_data_interface
{
        char sIdentName[10];
        int nIdentCharLength;
        char sRawDataFilePath[200];
        int nFilePathCharLength;
        char sRawDataFileName[200];
        int nFileNameCharLength;
        int nYear;
        int nMonth;
        int nDay;
        int nHour;
        int nMinute;
        int nSecond;
        char sDataDescriptorIdentifier[7];
        char sDataDescriptorRawData[7];
}RawDataInterface;

Test.c function :

#include <stdio.h>
#include <stdlib.h>
#include <mscc.h>
#include "RawDataInterface.h"
extern void * CreateRawDataBUFR(unsigned char **, int *, char *, RawDataInterface *);
void AcquireBUFRData(RawDataInterface *, char *);
 
int main(int argc,char *argv[])
{
/********************************************************************************/
/*                      EMPRESS Initialization                                  */
/********************************************************************************/
        if(!msinit())
        {
                printf("msinit failed!\n");
                msend();
                return 1;
        }
        else
        {
                printf("msinit basarili\n");
        }
        int i = 0;
        int * bufrSize = malloc(4);
        unsigned char * ucBufrMessage = NULL;
        /* RawDataInterface * rdi = malloc(sizeof * rdi); */
        RawDataInterface rdi;
        unsigned char * ucBinary = NULL;
 
        char sFileName[24] = "IST050906125859.RAWADM9";
        sFileName[23] = '\0';
 
/********************************************************************************/
/*              Filling Raw Data Interface to be sent to HVFE                   */
/********************************************************************************/
        AcquireBUFRData(&rdi,sFileName);
 
/********************************************************************************/
/*                      HVFE Trigger to create bufr message                     */
/********************************************************************************/
        CreateRawDataBUFR(&ucBufrMessage, bufrSize, sFileName, &rdi);
 
        free(bufrSize);
        bufrSize = …
asilter 0 Junior Poster in Training

Header asks the question.

my include files are in /abc/bcc/include/ path which are used in programs like;

#include "radar.h"

I think, -? option will tell the compiler to do that. Could u plz give a little compiling sample with gcc.

Thanx.

asilter 0 Junior Poster in Training

Hi,

I want a char array to point the memory location, which is pointed by a char pointer, without memcpy. I mean I do not need to copy all the elements again to another location

typedef struct
{
     long size;
     char data[1000];
}bulk;
 
bulk blob;

this doesn't work :

blob.data = (char *) ucBufrMessage;

it says;
error: incompatible types in assignment

Note : ucBufrMessage is an unsigned char pointer, pointing to a memory location which is full of assigned values.

asilter 0 Junior Poster in Training
105(decimal) = 01101001(binary)

for example, i want to skip first 3 bits of binary and get the number to the right and assign it to an integer:

01001(binary) = 9 (decimal)

could you plz give a little code sample?

thanx.

asilter 0 Junior Poster in Training

p2 is just a reference into the allocated memory, it's not the controlling reference for the memory it points to...

if I;

free(p2);

what happens? don't I get segmentation fault when ;

printf("%d\n",p[1001]);
asilter 0 Junior Poster in Training

So you want a somewhat dynamic array. For really dynamic array you should implement a linked list, which is not very difficult either, but you must be careful because mistakes in implementing it can easily cause a memory leak. But it depends, how are you going to use your array. If you want to use it for example for fifo buffer, it's much easier to implement it with a normal static array, but of course don't forget to always check the array boundary. If you only need to delete elements from the beginning, you may consider writing array in reverse, which is not difficult, but is somewhat easier and more logical to handle. But if you may need to delete anywhere in the array, then again it's important how are you going to use it. Is the order of the elements important or not, how much your array is expected to grow, how much it would be changed. Based on that, in some cases, especially when the order of the elements is not important, you may just leave some empty places where you delete, and add new values there when necessary, this is also not difficult to implement, and has been used for example even in the indexes of certain databases. But of course when everything is expected to change too much, and has to be fast at that, you may consider using a linked list, though there would rarely be a need for that, especially considering that a lot …

asilter 0 Junior Poster in Training
unsigned char *p2 = p + 1000;

in this way, freeing p will cause freeing p2. i must free the memory of 1000 bytes after assigning to p2. i do not want any memory leaks.

asilter 0 Junior Poster in Training

let's say we have a unsigned char pointer with allocated memory of 1 million bytes. Could you give a little code sample which shows how to eliminate first 1000 bytes? the little similar situation is like below:

unsigned char array:
12,34,67,99,215,250,123,67

i want to make it:
215,250,123,67

I want to see the most efficient way to do this with no redundant memory and no memory leak or corruptions. do i have to use memcpy, if so i think it is not so efficient.

thank you.

asilter 0 Junior Poster in Training

I haven't programmed a lot of C but to the best of my knowledge:

unsigned char ucBufr = NULL;

1. This is not a null pointer, it's a char variable (1 byte set to 0).

>I know that after function statements local variables which are defined in the function are freed by the O/S

Not quite. Primitive types (int, char, etc) go on the stack and fall of when the function returns. Pointers and References however, live on the stack themselves, but the value they point to/reference is a memory address on the heap. These you must explicitly free before the function returns, otherwise when the pointer/reference falls of the stack allocated memory is lost or leeked as it's commonly known.

i wanted to write like this :

unsigned char * ucBufr = NULL;

what does fall of means? aren't primitive type memory allocations(not with pointer) freed by OS after function statements to make those memory locations possible to be used with other variable names further in the program?

is leaking a redundant lost memory which faces us with unrecognized segmentation faults?

asilter 0 Junior Poster in Training

i'm sending a NULL pointer of type unsigned char to a function. i allocate space for this pointer in the function and return nothing.

unsigned char ucBufr = NULL;
Allocation(ucBufr);
void Allocation(unsigned char * ucBufr)
{
        /* nDataSize and ucData is got from a file here. */
        ucBufr = malloc(nDataSize);
        ucBufr[0] = 'B';
        ucBufr[1] = 'U';
        memcpy(ucBufr+2,ucData,nDataSize);
        /* ... */
}

what i want to be sure is, after function statements does the allocation space dissapear? i do not get any errors, but i do not want to fall into segmentation faults further in my program. why i am asking this is because I know that after function statements local variables which are defined in the function are freed by the O/S. Could we expect the same bad event with malloc?

Thanx.

asilter 0 Junior Poster in Training

i have a char array of 4 elements :

00000000 00000000 00000010 00000111

how can i assign these bytes to 4 byte integer (this means 519), could you please give a modular code sample?

thanx.

asilter 0 Junior Poster in Training

Hi
Did that mean you want to assign any number that can be stored in 3 bytes storage space?

Yes.

ch[0]= d&0xFF;
        ch[1]= (d>>8)&0xFF;

let's say d = 1200;
what does these lines do? could u show the results in bitstreams like 0010101.......?

asilter 0 Junior Poster in Training

how do i assign -let's say number 12000- to 3-byte stream?
could u plz give a little code example?

thanx.

asilter 0 Junior Poster in Training

how can i convert a void array to unsigned char array?
could you plz give a little sample?

asilter 0 Junior Poster in Training
/* One file can be hundreds of MBs. So, 640 MB in maximum*/
int nMaxByteCount = 64000000;

I read files to get binary info in it. But I don't know the exact sizes before reading. So, I allocate nMaxByteCount for large files first and get support from fread return to get the exact size.

*nFileSize = fread(caFileContent,sizeof(char),nMaxByteCount,DataFile);

Then I use this *nFileSize -which contain the exact file size- for filling information in my data structures. When I create a char array or struct containing these variables, I mostly get segmentation faults after reading big files more than 10 MBs. I think the allocated size of memory which the system gives access to my program is not suitable or enough for my files and further programming. How much size of bytes or KBs or MBs are permitted for my Linux C program on a 64 bit processor? Can I change it? Or, is this the problem?

asilter 0 Junior Poster in Training

i've heard that type casting warnings are not so important.

asilter 0 Junior Poster in Training

They both allocate the same amount of bytes. The difference is that the first method is safer because at some future time you can change what p points to without having to change that malloc() line of code. Its just a way of making a programmer's life a little easier and causes fewer bugs to get introduced into the program.

Could u plz give a little example code of this kind of bug?

asilter 0 Junior Poster in Training

i've included stdlib.h but the warning is still there. maybe i need to upgrade to gcc 4.1.2.

asilter 0 Junior Poster in Training

Compiled without errors/warnings with my compiler -- VC++ 2005 Express as both a C and a C++ program. What compiler are you using ? Does your compiler complain about the below ?

char* foo1()
{
    return (char *)malloc(255);
}
 
int foo(void **vpSec00)
{
 
    *vpSec00 = foo1();
    return 0;
}

i'm using :
gcc (GCC) 3.3.3 (SuSE Linux)

it says :
warning: cast to pointer from integer of different size

for the line :

return (char *)malloc(255);
asilter 0 Junior Poster in Training

>>I think malloc allocates 8 bytes of unused space with this, beacuse size of *p is 8 bytes in my system

No it isn't. The asterisk tells sizeof operator to evaluate the size of the object to which the pointer points and not the size of a pointer itself. In your example it will get sizeof(BulkData)

If you look at BulkData structure, how many bytes does it allocate you can say? Is it 72?

if so, what is the difference between :

BulkData * p = malloc(sizeof * p);

and

BulkData * p = malloc(sizeof(BulkData));

don't they do the same thing?