A quick buffer issue

This is a discussion on A quick buffer issue within the C Programming forums, part of the General Programming Boards category; Oh boy, I got nearly no sleep last night, woke up WAY too early, and hopped straight away into this ...

  1. #1
    Merld_One
    Guest

    A quick buffer issue

    Oh boy, I got nearly no sleep last night, woke up WAY too early, and hopped straight away into this coding project, so what I ask may be very simple, but this coffee has not kickstared by brain yet.. Those who know me know I've coded for quite a while now, so forgive my brain-absence today!
    ok, what is wrong with this loop?! Code snippits follow:

    int i; iter;
    i=atoi(argv<2>);
    char *sendbuf;
    ...
    for (iter=1; iter<(i=1); iter++)
    sendbuf = "A";

    ?? Looks fine to me, but when I compile, I get the warning (yes, only a warning) : Warning: Assignement makes integer from a pointer without a cast.
    The warning refers to the sendbuf line.. What the hell?? I don't see the problem here.. Then, whenever I run it, even with a tiny amount of data in the buffer (ie, not even half of the buffer size) I get a segmentation fault.. Grrr, I'm not overflowing any buffers that I can see, so why the seg fault?? Any idea's?

    On FreeBSD, btw..

    Thanks!

    The Merld One

  2. #2
    Merld_One
    Guest

    Heres more code..

    #include <stdio.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <netinet/in.h>

    #ifndef _BSD_SOURCE
    #define _BSD_SOURCE 1
    #endif

    void die(char *error) {
    perror(error);
    exit(1);
    }

    main(int argc, char **argv) {

    int sock, i;
    char *ip;
    unsigned short int port;
    struct sockaddr_in target;
    char *sendbuf;
    int iter;
    if (argc != 4) die("Usage is ./filename ip port size");
    if ((sock=socket(PF_INET, SOCK_RAW, 2)) < 0) die("No Socket");
    ip=argv[1];
    port=atoi(argv[2]);
    i=atoi(argv[3]);

    memset(&target, 0, sizeof(target));
    target.sin_family=AF_INET;
    target.sin_port=htons(port);
    target.sin_addr.s_addr=inet_addr(ip);

    for (iter=1; iter <= (i-1); iter++)
    strcpy(sendbuf[iter],"A");
    strcpy(sendbuf[i],NULL);

    if (connect(sock, (struct sockaddr *) &target, sizeof(target)) < 0) die("Connect()");
    if (send(sock, sendbuf, sizeof(sendbuf), 0) < 0) die("Could not send");


    }

  3. #3
    Unregistered
    Guest
    From what I can see, you have never allocated any memory space to *sendbuf.

    Also, sendbuf="A" doesn't work in C. What I think you meant was sendbuf[i]='A'.

  4. #4
    Merld_One
    Guest

    OMG

    lol!!!

    Thanks man.. Like I said, I aint quite woke up yet.. :-)

    -Merld

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >ip=argv[1];

    Realise that you haven't allocated memory for ip, if ip is meant to be a buffer of chars.

    >port=atoi(argv[2]);

    Function atoi() returns an int, port is declared as a short int, this may cause problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frame buffer not working
    By Noise in forum Game Programming
    Replies: 1
    Last Post: 02-15-2009, 11:05 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  4. Quick C Question - checking buffer for input
    By sean in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 11:23 AM
  5. fgets
    By stautze in forum C Programming
    Replies: 14
    Last Post: 03-17-2003, 07:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21