I have the following function but sometimes it fails on the
malloc function call and I don't know the reason,I thought that it may be due to lack of heap size but I have monitored the heap and I understood that I have enough space available for memory allocation when malloc fails ,can any one suggest anything to me
char *substr(const char *pstr, int start, int numchars)
{
char *pnew;
pnew=malloc(numchars+1); //this line fails
if (pnew==0)
{
free(pnew);
pnew=malloc(numchars+1);
}
strncpy(pnew, pstr + start, numchars);
pnew[numchars] = '\0';
return pnew;
}
int32 SendData(char * dataBuffer, int CommandType)
{
struct sockaddr_in remoteServerAddr;
int tcpSocket;
int errorCode;
int counter;
int PacketsToSend;
int32 ret;
char msgbuf[16];
char *packet;
char * cmdIRes;
char RecPacket[BUF_SIZE];
div_t divresult;
counter = 0;
/* Specify struct sock address */
memset(&remoteServerAddr, 0, sizeof(remoteServerAddr));
remoteServerAddr.sin_len = sizeof(remoteServerAddr);
remoteServerAddr.sin_family = AF_INET;
remoteServerAddr.sin_port = htons(11000); // Net byte order required
remoteServerAddr.sin_addr.s_addr = inet_addr("10.252.85.26");
/* Create an TCP socket */
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (tcpSocket != -1)
{
/* Connect to server */
errorCode = connect(tcpSocket, (struct sockaddr*) &remoteServerAddr,
sizeof(remoteServerAddr));
if (errorCode == -1)
{
/* Connection failed */
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("connect:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
}
else
{
/* Send packets */
divresult=div(sizeof(dataBuffer), BUF_SIZE);
PacketsToSend=divresult.quot;
if (divresult.rem>0)
{
PacketsToSend=PacketsToSend+1;
}
while (counter < PacketsToSend)
{
packet= substr(dataBuffer, counter*BUF_SIZE, BUF_SIZE);
errorCode = send(tcpSocket, packet,strlen(packet) , 0);
if (errorCode == -1)
{
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("send:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
break;
}
counter++;
}
memset(RecPacket, 0, BUF_SIZE);
errorCode = recv(tcpSocket, RecPacket, BUF_SIZE,0);
if (errorCode == -1)
{
errorCode = socketerror();
}
switch (CommandType)
{
case CommandType_SendOneTrans:
case CommandType_SendOfflineData:
cmdIRes=substr(RecPacket, 14, 10);
ret= atoi(cmdIRes);
break;
case CommandType_TransConfirm:
cmdIRes=substr(RecPacket, 11, 2);
if (strcmp(cmdIRes, "ok")==0)
{
ret= 1;
}
else
{
ret= 0;
}
break;
case CommandType_VoucherList:
SaveVoucherList(RecPacket);
ret= 1;
break;
case CommandType_Identify:
cmdIRes= substr(RecPacket, 7, 2);
if (strcmp(cmdIRes, "ok")==0)
{
ret=1;
}
else
{
ret= 0;
}
break;
default:
break;
}
}
/* Close the socket */
close(tcpSocket);
free(RecPacket);
free(cmdIRes);
free(packet);
free(msgbuf);
return ret;
}
else
{
errorCode = socketerror();
sprintf(msgbuf, "Error %d", errorCode);
displayMsg("socket:", "Failed!!!", msgbuf, NULL, 0x0100FFFF);
}
return (errorCode);
}
uint32 SendOneTrans(fin trans)
{
int retVal=0;
int ret=0;
int retValCon=0;
char msg[100];
char * voucherId;
char * Amount;
char * TerminalNo;
char * isOnline;
char * ReturnedId;
TerminalNo=malloc(12);
voucherId=malloc(4);
Amount=malloc(7);
isOnline=malloc(1);
ReturnedId=malloc(3);
memset(TerminalNo, 0, sizeof(TerminalNo));
strcpy(TerminalNo, (char *)getTerminalNo());
memset(msg, 0, sizeof(msg));
if (trans.success==0)
{
memset(msg, 0, sizeof(msg));
memset(voucherId, 0, sizeof(voucherId));
sprintf(voucherId, "%d", trans.voucherId);
memset(Amount, 0, sizeof(Amount));
sprintf(Amount, "%d", trans.Amount);
memset(isOnline, 0, sizeof(isOnline));
sprintf(isOnline, "%d", trans.isOnline);
strcpy(msg, "<Req_fin>");
strcat(msg, TerminalNo);
strcat(msg, ",");
strcat(msg, voucherId);
strcat(msg, ",");
strcat(msg, trans.cardNo);
strcat(msg, ",");
strcat(msg, Amount);
strcat(msg, ",");
strcat(msg, trans.dateOf);
strcat(msg, ",");
strcat(msg, trans.TimeOf);
strcat(msg, ",1");
strcat(msg, "<EOF>");
retVal= SendData(msg, CommandType_SendOneTrans);
if (retVal>=1)
{
sprintf(ReturnedId, "%i", retVal);
memset(msg, 0, sizeof(msg));
strcpy(msg, "<Req_fin_c>");
strcat(msg, TerminalNo);
strcat(msg, ",");
strcat(msg, ReturnedId);
strcat(msg, "<EOF>");
trans.success=1;
retValCon= SendData(msg, CommandType_TransConfirm);
if (retValCon!=0)
{
trans.success=1;
ret=1;
}
}
free(msg);
free(TerminalNo);
free(Amount);
free(voucherId);
return ret;
//free(ReturnedId);
}
}
I'm no Apple dev, but I've never seen EXC_BAD_ACCESS on malloc so I had to Google it. This entry from the Apple technical FAQ looks relevant:
This kind of problem is usually the result of over-releasing an object. It can be very confusing, since the failure tends to occur well after the mistake is made. The crash can also occur while the program is deep in framework code, often with none of your own code visible in the stack.
Your problem is deeper: EXC_BAD_ACCESS basically means that you are over-freeing zones of memory. In a debugger, you'd see something like this
*** malloc[705]: error for object 0×8c5b00:
Incorrect checksum for freed object - object was probably modified after being freed; break at func_name.
What's your platform? Is Guard Malloc available to you? If not, here's what you can do, besides scrutinizing your source code, of course:
Write a wrapper for malloc() which will allocate a single vm page for every request and place the requested buffer at its end. That way, reads or writes past it will cause a bus error. Also, when memory is free()'d, deallocate your vm page(s), so that whenever you read or write to a free()'d are you get an immediate bus error. It's going to take a loooong time, so be prepared!
char *substr(const char *pstr, int start, int numchars)
{
char *pnew;
pnew=malloc(numchars+1); //this line fails
the following makes no sense, what is it you are trying to do? if malloc failed why try again and above all why free? You should exit instead and return null
if (pnew==0)
{
free(pnew);
pnew=malloc(numchars+1);
}
I think the error is somewhere else, maybe you should check
the in-parameters, make sure pstr is not NULL and numchars > 0
You probably corrupted your malloc heap somewhere earlier in the code but the problem does not show up until you call malloc or free - you should run the code under valgrind or similar to narrow down the root cause.
This mite help clear things:
You will get EXC_BAD_ACCESS in 3 cases:
1. An object is not initialized
2. An object is already released
3. Something else that is not very likely to happen
So please evaluate the state of other variables in the function or you can paste the calling function here to get better solutions.
EDIT: code continued after getting information from the comments.
NULL check avoided intentionally.
char a[][100] = {"<Req_fin>1","<Req_fin>1","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1<EOF>","<Req_fin>1","<Req_fin>1<EOF>"};
char *b= "<EOF>";
char *substr(char *buff,int start,int bytes)
{
char *ptr;
ptr = malloc(bytes+1);
strncpy(ptr,buff+start,bytes);
ptr[bytes]='\0';
return ptr;
}
int main()
{
char buff[100];
int i;
char *ptr;
strcpy(buff,"Abcdef");
for(i=0;i<10;i++)
{
ptr = substr(buff,0,512);
printf("String is %s \n",ptr);
memset(buff,0,sizeof(buff));
strcpy(buff,a[i]);
strcat(buff,b);
free(ptr);
}
return 0;
}
The above code works fine. So please use this as reference if you cant paste your function here. I can't just guess the error.
Related
I'm doing an exercise about UDP sockets in C. When the client sends a specific message (e.g hi) the server has to send "Nice to meet you". If no standard reply is found the server sends "No suitable reply". My problem is that memset fails if I try to return the reply like this:
return "No suitable reply";
and it doesn't if I return the reply in this way:
char* foo = malloc(sizeof(char*));
memset(foo, 0, strlen(ses));
memcpy(foo, "No suitable reply", 17);
return foo;
I tried to google a solution to this and I found this and this, but they don't seem to address my problem (I first thought that memset doesn't work on a string declared like char string[] = "something" but in the second example they use memset on a static string).
Here is the whole code (the memset I'm talking about is right at the end):
/*
Alessandro Dussin 5AI
2018-17-11
Write a program to handle a single UDP "connection"
*/
//Standard libraries
#include <stdio.h>
#include <stdlib.h>
//Sockets libraries and connection ahndling
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
//Read/write ops on file descriptors
#include <unistd.h>
//String ops
#include <string.h>
#include <assert.h>
void chopN(char *str, size_t n)
{
assert(n != 0 && str != 0);
size_t len = strlen(str);
if (n > len)
return; // Or: n = len;
memmove(str, str+n, len - n + 1);
}
//Required by the exercise. Given a certain word or phrase, reply with a specific string
char* switchreply(char* str){
//Extracts the word or phrase (Basically removes the "/command " word)
chopN(str, strlen("/stdreply "));
int i = 0;
for(; i < strlen(str); i++){
if(str[i] == '\n'){
str[i] = '\0';
break;
}
}
if(strcmp(str, "ciao") == 0){
return "ciao anche a te!";
}
else if(strcmp(str, "I hate you") == 0){
return "I hate you too!";
}
return "";
}
char* stdreply(char *str){
char* tmp = malloc(sizeof(char)*128);
int i = 0;
//printf("Entered stdreply... str at the start of the func: %s\n", str);
for(; i < strlen(str); i++){
tmp[i] = str[i];
//printf("tmp: %s\n", tmp); //DEBUG
if(strcmp(tmp, "/echo ") == 0){ // if(strcmp() == 0) is necessary because
//otherwise 0 would be interpreted as FALSE
//printf("Echo detected\n"); //DEBUG
chopN(str, strlen("/echo "));
str[strlen(str)] = '\0';
return str;
}
else if(strcmp(tmp, "/stdreply ") == 0){
//printf("I got into the else if\n"); //DEBUG
char* tmpreply = calloc(strlen(str), sizeof(char*));
tmpreply = switchreply(str);
//printf("tmpreply: %s\n", tmpreply);
str = malloc(sizeof(char*)*strlen(tmpreply));
memcpy(str, tmpreply, strlen(tmpreply));
//str[strlen(str)] = '\0'; //DEBUG
//printf("str: %s\n", str); //DEBUG
return str;
}
else if(strcmp(tmp, "/TODO") == 0){
char* ses = malloc(sizeof(char*));
memset(ses, 0, strlen(ses));
memcpy(ses, "work in progress", 17);
return ses;
}
}
return "No suitable reply";
}
int main(int argc, char **argv){
if(argc < 2){
printf("Usage: ./server port");
exit(0);
}
int serverfd;
serverfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server;
server.sin_port = htons(atoi(argv[1]));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
if(bind(serverfd, (struct sockaddr *)&server, sizeof(server)) < 0){
perror("Bind() error: ");
fflush(stderr);
}
//"UDP message receiver" variables declarations
int bytes; //Reads how many bytes the funcion recvfrom has read
struct sockaddr_in from;
char* buffer = malloc(sizeof(char*)); //String to which save the client message
memset(buffer, 0, strlen(buffer)); //and set it to zero
socklen_t fromlen = sizeof(struct sockaddr_in);
const char stdrep[] = "Message Received: "; //This string will always be
//printed upon receiving a message
char* reply = malloc(sizeof(char*)); //This is where the return value of
//stdreply() will be stored
memset(reply, 0, strlen(reply)); //and set it zero
//This while will keep "listening" for udp messages
while((bytes = recvfrom(serverfd, buffer, 1024, 0, (struct sockaddr *)&from, &fromlen)) > 0){
//From teacher's example. Write to stdout
write(1, stdrep, strlen(stdrep));
write(1, buffer, bytes);
//Detect a basically empty string (if the client has pressed only enter)
if(buffer[0] == '\n'){
bytes = sendto(serverfd, "You pressed only enter!\n", 18, 0, (struct sockaddr *)&from, fromlen);
}
//Act according to the client message
reply = stdreply(buffer);
bytes = sendto(serverfd, reply, strlen(reply), 0, (struct sockaddr *)&from, fromlen);
if (bytes < 0){
perror("sendto: ");
fflush(stderr);
}
memset(buffer, 0, 1024);
memset(reply, 0, strlen(reply)); //The seg fault happens right here
fflush(stdout);
}
return 0;
}
There are plenty of problems in the code you are posting.
As #JonBolinger already noted, sizeof(char*) returns the size in bytes of a pointer to a char. On Intel platforms this will be either 4 or 8, depending on whether you are running on 32-bit or 64-bit. (So you end up allocating buffers of 4 or 8 bytes)
You consistently try to clear your dynamically-allocated buffers with memset(). malloc() will return memory filled with garbage, and you indicate how many bytes to clear by using strlen() on the returned buffer. strlen() will scan the buffer until it finds the first 0 character to compute the string's length. Since the buffer is filled with garbage, this can easily give you a value outside of the boundaries of your memory block and you will end up corrupting memory.
Every call to malloc() should be matched to a free() call, otherwise you will leak memory. This is especially important if your program is long-running.
When you are working with temporary local strings (strings that are not returned to the caller), it is very common practice to use a local char array instead of malloc(). This way, the buffer gets allocated on the stack and will be released automatically when your function exits scope. Just be sure to use 'safe' string functions like strncpy() that will accept as a parameter the length of the buffer, to avoid overwrites.
void Example(char* anotherString ) {
char tmpString[256]; // this will create a local buffer with capacity of 256 bytes
strncpy(tmpString, anotherString, sizeof(tmpString)); // copy string, without risk of overflowing the buffer
}
Warning: NEVER attempt to return a local temporary buffer as a result, remember that it will no longer exist when the function exits and although the returned value may initially have meaningful results, they will certainly be destroyed as soon as you call another function. Instead of this, another common practice when you need a string return value, instead of returning a string allocated with malloc() -that would need to be released with free() - you pass a local buffer that will hold the result as a parameter, like this:
void func1() {
char result[256];
func2(result, 256);
// after calling, result will carry "a returned string"
}
void func2(char* result, size_t bufferLen) {
strncpy(result, "a returned string", bufferLen);
}
I think your code would benefit greatly if you can transform it to using this style where applicable.
reply = stdreply(buffer);
This does not copy a string. It overwrites the pointer with a different one, losing the original pointer.
memset(reply, 0, strlen(reply));
This would clear the string if it were allocated with malloc. If it is a constant string like "No suitable reply" then it may be read-only, so it generates a segfault.
I am writing a small code to read ADS-B data from a Socket. The data is in following format
MSG,6,1,1,4242D7,1,2017/12/25,12:04:05.695,2017/12/25,12:04:05.695,,,,,,,,6142,0,0,0,
MSG,4,1,1,80068D,1,2017/12/25,12:04:05.695,2017/12/25,12:04:05.695,,,183,268,,,-576,,,,,0
MSG,2,1,1,800B6F,1,2017/12/25,12:04:05.695,2017/12/25,12:04:05.695,,,7,165,28.53949,77.10886,,,,,,-1
MSG,5,1,1,4242D7,1,2017/12/25,12:04:05.695,2017/12/25,12:04:05.696,KAR2440
,32025,,,,,,,0,,0
The strings are terminated by CRLF
I am presently using a readline function to read the data from the socket line by line
int readline(int fd, char ** out)
{
/* Keep reading till header "MSG" is received
* store it in buffer
* keep reading till newline is encountered
* exit function
*/
int buf_size = 512;
int bytesloaded = 0;
int ret;
char buf;
char * buffer = malloc(buf_size * sizeof(*buffer));
if (NULL == buffer)
{
free(buffer);
return -1;
}
//loop till header is received
while (1)
{
bzero(buffer,buf_size);
ret = read(fd, &buf, 1);
if (ret < 0)
{
free(buffer);
return -1;
}
if (buf=='M') //Got M; now wait for S
{
bytesloaded=0;
buffer[bytesloaded] = buf;
bytesloaded++;
ret = read(fd, &buf, 1);
if (ret < 1)
{
free(buffer);
return -1;
}
if (buf=='S') //Got S; now wait for G
{
buffer[bytesloaded] = buf;
bytesloaded++;
ret = read(fd, &buf, 1);
if (ret < 1)
{
free(buffer);
return -1;
}
if (buf=='G') //Got G; now wait for CR-LF
{
buffer[bytesloaded] = buf;
bytesloaded++;
while(1)
{
ret = read(fd, &buf, 1);
if (ret < 1)
{
free(buffer);
return -1;
}
if (buf=='\n')
break;
buffer[bytesloaded] = buf;
bytesloaded++;
}
buffer[bytesloaded] = '\0';
*out = buffer; // complete line
//free(buffer);
return bytesloaded;
}
}
}
}
}
The above function is called from main using the following routine
bytesread=readline(netfd, &netbuf);
The problem is the code footprint in the memory increases gradually indicating a memory leak. Valgrind also points to the readline function.
There is no memory leak if I uncomment the second last line (free(buffer)). But doing so gives me no data at output.
What am I missing here.
Your design indicates that the function readline allocates the memory required and returns a pointer to the calling function.
Using this design, the calling function MUST call free to avoid a memory leak.
I should point out that this design is often problematic and increases the chances for memory leaks.
A better design keeps memory management concerns together (the allocating function should free the memory. When this is impossible or impractical, the function name should clearly indicate the allocation and a destructor should be written (i.e., line_alloc and line_free), minimizing any confusion.
Creating a tcp client <-> server program in c for my home exam. Been running into some problems with reciev and sending data between the server and client. I'm only able to receive one byte, feks if i send "abcd" i receive "a".
(This is happening both ways since i'm using same methods for server and client)
Dont know if it is the sending part thats the problem or receiving
This is my code:
#define BUFFER_SIZE 1024
char* recived_message;
int send_data(int socket, char* data){
int offset = 0, len = strlen(data);
while (offset != len) {
int nb = send(socket, data + offset, len - offset, 0);
if (nb < 0){
perror("send");
return -1;
}
offset += nb;
}
return 0;
}
int recive(int socket){
int offset = 0;
recived_message = malloc(BUFFER_SIZE);
memset(recived_message, 0, BUFFER_SIZE);
while (offset != BUFFER_SIZE) {
int nb = recv(socket, received_message + offset, BUFFER_SIZE - offset, 0);
if(nb == -1){
perror("read");
return -1;
}else if(nb == 0){
return -1;
}
offset += nb;
}
printf("%d\n", offset);
return 0;
}
char* get_data(){
return recived_message;
}
Server side
int recive_data(int socket){
char* buffer;
if(recive(socket) != 0){
return -1;
}
*buffer = *get_data();
printf("socket %d: %s\nlength: %lu%", fd, buffer, strlen(buffer));
return 0;
}
Part of client
char* test = "abcd";
for(i=0; i<10; i++) {
send_data(sock, test);
sleep(1);
}
The problem is here
*buffer = *get_data();
You dereferences the pointer returned by get_data() to get only the first element pointer to by the pointer.
And it's worse than that, because you dereference the uninitialized variable buffer to write that single character. This will lead to undefined behavior. Also, the later functions calls using this uninitialized variable also leads to undefined behavior.
The simple solution to (almost) all your problems: Assign to the actual variable:
buffer = get_data();
And I say that the above solves almost all your problems, because what if the terminating zero of the string isn't transmitted? That will also lead to UB (Undefined Behavior). If the data you receive is always a string, then you should make sure it's terminated, preferably in the receive function:
int recive(int socket){
...
received_message[offset] = '\0';
return 0;
}
I'm learning C almost one year and it's my first time that I got that warnings.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#include <string.h>
int socket_creation(FILE* fp){
int s;
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET){
printf("Error occurred while creating socket: %d\n", WSAGetLastError());
fprintf(fp, "Error occurred while creating socket: %d\n", WSAGetLastError());
}
else{
printf("Socket creation was successful.\n");
fprintf(fp, "Socket creation was successful.\n");
}
return s;
}
void connect_to_server(int s, struct sockaddr_in ClientService, FILE* fp){
int cResult;
cResult = connect(s, (struct sockaddr*) &ClientService, sizeof(ClientService));
if (cResult == SOCKET_ERROR){
printf("connection to the server has been failed: %d\n", WSAGetLastError());
fprintf(fp, "connection to the server has been failed: %d\n", WSAGetLastError());
cResult = closesocket(s);
if (cResult == SOCKET_ERROR){
printf("error occurred while trying to close socket. \n");
fprintf(fp, "error occurred while trying to close socket. \n");
}
WSACleanup();
}
else{
printf("Connection to serevr has been made successfully. \n");
fprintf(fp, "Connection to serevr has been made successfully. \n");
}
}
int send_to_serv(char buffer[], int s){
int sendto;
sendto = send(s, buffer, 1024, 0);
if (sendto == -1)
printf("\nError: couldn't send the Code.\n", buffer);
else printf("\nCode: <%s> SENT.\n", buffer);
return sendto;
}
int recv_from_serv(int s, int* numberLines, FILE *fp){
int recvfrom;
char buffer[1024] = "";
recvfrom = recv(s, buffer, 1024, 0);
if (recvfrom == -1)
printf("\nError: couldn't receive Code. !\n");
else printf("\nRespond: <%s>, RECEIVED. \n", buffer);
fprintf(fp, "\n");
fprintf(fp, buffer);
*numberLines = atoi(buffer + 3);
return recvfrom;
}
int main() {
WSADATA info;
int error, s;
int sResults, sendError, recvError,convert2;
char buffer[1024] = "";
char recvbuf[1024] = "";
int numberLines, i, temp, convert;
char converted_num[1024] = "";
struct sockaddr_in ClientService;
FILE *fp = fopen("stored_data.txt", "w");
char* lines_array;
error = WSAStartup(MAKEWORD(2, 0), &info);
if (error != 0){
printf("WSAstartup failed with error: %d\n", error);
exit(1);
}
for (i=0; i <=numberLines; i++) {
lines_array[i]=NULL;
}
s = socket_creation(fp);
ClientService.sin_family = AF_INET;
ClientService.sin_addr.s_addr = inet_addr("54.209.143.42");
ClientService.sin_port = htons(6714);
connect_to_server(s, ClientService, fp);
strcpy(buffer, "100");
sendError = send_to_serv(buffer, s);
recvError = recv_from_serv(s, &numberLines, fp);
strcpy(buffer, "400");
sendError = send_to_serv(buffer, s);
recvError = recv_from_serv(s, &numberLines, fp);
printf("\nNumber of Lines are: %d\n", numberLines);
lines_array = malloc(sizeof(char*)*numberLines);
temp = numberLines;
for (i = 0; i < temp; i++){
convert = 5000001 + i;
_itoa(convert, converted_num, 10);
sendError = send_to_serv(converted_num, s);
convert2=atoi(convert);
convert=convert%10000;
if(convert2==0) {
for(i=0; i<=1024; i++) {
buffer[i]=0;
}
lines_array= (convert);
recv_from_serv(s, &numberLines, fp);
}
else{
for(i=0; i<=1024; i++) {
buffer[i]=0;
}
}
}
close(fp);
//system("PkAUSE>nul");
system("PAUSE");
return 0;
}
This is that output that my compiler (MinGW) giving to me:
finalproject1.c: In function main': finalproject1.c:87: warning:
assignment makes integer from pointer without a cast
finalproject1.c:113: warning: passing arg 1 ofatoi' makes pointer
from integer without a cast finalproject1.c:119: warning: assignment
makes pointer from integer without a cast finalproject1.c:133:3:
warning: no newline at end of file
Please help me I don't know what to do and my guid told me to "Google it" when I asked him to help me.
The line that triggers the warning is:
lines_array[i]=NULL;
and this variable is declared like so:
char* lines_array;
so you're cramming a pointer (the NULL macro expands to a pointer-type value on your system) into a single char, which won't fit and is a very strange thing to be doing.
Probably you wanted an array of character pointers, i.e.
char **lines_array;
but then you must of course allocate the array once you know how many lines you're going to be dealing with.
First of all, i can see you are using an integer numberLines as a condition in your for loop without initializing it first:
int numberLines;
for (i=0; i <=numberLines; i++) {
lines_array[i]=NULL;
}
Simply if you want to initialize your array to null you can initialize it as:
char* lines_array = null;
Try this and let me know if the warnings still exist.
I am trying to pass parts of a struct to the client and having a hard time with it. I tested my case 1 condition with the following print statement:
printf("%d %s", data[i].course, data[i].Dept);
This worked with no problems. Now what I am trying to do is send it through the socket. My send statement is:
send(connected, send_data, strlen(send_data), 0);
Now I tried the following statements of:
send_data = data[i].couurse;
strcpy(send_data, data[i].course);
send_data = atoi(data[i].course);
and to know avail, neither worked. I know there has to be a way and am hoping someone can show me how.
I have included the relevant parts of the code:
int switchInput;
int i = 0;
int connected;
int sock;
int bytes_received;
int sin_size;
int true = 1;
int tempCourse = 0;
char send_data[BUF];
char recv_data[BUF];
char tempDept[5];
char tempDay[1];
char tempTime[1];
FILE *filePointer;
sched_record data[MAX_RECORD];
filePointer = fopen (BINFILE, "rb");
and:
while(1) {
bytes_received = recv(connected, recv_data, BUF, 0);
recv_data[bytes_received] = '\0';
switchInput = atoi(recv_data);
switch(switchInput) {
case 1:
fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
fclose(filePointer);
char send_data[] = "Enter Department Name";
send(connected, send_data, strlen(send_data), 0);
bytes_received = recv(connected, recv_data, BUF, 0);
recv_data[bytes_received] = '\0';
strcpy(tempDept, recv_data);
for (i=0; i<MAX_RECORD; i++){
if ((strcmp(tempDept, data[i].Dept)==0) && tempCourse != data[i].course){
send(connected, &data[i].Dept, sizeof(data[i].Dept), 0);
tempCourse = data[i].course;
send(connected, &tempCourse, sizeof(tempCourse), 0);
}
}
break;
In this particular case I need to send the course and department. Also is there a better way to do this? This seems kinda messy. If so, can you show me a more simplified version?
You must have got compilation errors all over the place.
You don't want atoi. you want itoa or sprintf
but this may be what you really want:
tempCourse = data[i].course;
send(connected, &tempCourse, sizeof(tempCourse), 0);
(you provide the address of the tempCourse variable and it's bytes size in memory. see send)
(although you might need to worry about network byte order)
Alternatively, if you wish to convert the .course struct member into a string, then try this:
sprintf(send_data, "%d", data[i].course);
send(connected, send_data, strlen(send_data), 0);