1
#define LINE_LEN        80
#define MAX_ARGS        64
#define MAX_ARG_LEN     64
#define MAX_PATHS       64
#define MAX_PATH_LEN    96

#ifndef NULL
#define NULL            0
#endif

...

struct command_t {
    char *name;
    int argc;
    char *argv[MAX_ARGS];
};

...

/* Command initialisation, allocate type command */
    struct command_t command;
    command.name = (char *) malloc(LINE_LEN);
    command.argc = 0;
    for(ii=0; ii < MAX_ARGS; ii++) {  /* i < 64 */
        command.argv[ii] = (char *) malloc(MAX_ARG_LEN); 
    }

...pass in some values in command.argv, I have something like command.argv[0] = "man", and command.argv[1] = "socket", command.argc = 2, command.name = "name" for example like this

command.name = "man"
command.argc = 2
command.argv[0] = "man"
command.argv[1] = "socket"
command.argv[2] = ""
command.argv[3] = ""
...
command.argv[63] = ""

then the question is when I did below:

        char *argTemp[command.argc+1];
        for(ii = 0; ii < command.argc+1; ii++){
            if(strlen(command.argv[ii])){
                argTemp[ii] = (char *)malloc(strlen(command.argv[ii])+1); /*added +1*/
                /*   argTemp[ii] = command.argv[ii];  */ 
                /*delete the line above, problem still exist*/
                strcpy(argTemp[ii], command.argv[ii]);
            } else {
                argTemp[ii] = (char*)NULL;
            }
        }

The memory allocation for command was successful, everything works for command. however, when using the same technique for argTemp[], in the debugger, I see argTemp is [0], there is nothing in it, nor it points to anything. I don't understand why? I'm expecting to see artTemp[] to be the same as command.argv[], only getting rid of the extra null memory. like this:

argTemp[0] = "man"
argTemp[1] = "socket"
argTemp[3] = "\000"

and that is, no more argTemp, then i will free command.argv, and point command.argv to argTemp. So you probably see all I'm trying to do here is to get rid of the memory with null value in command.argv.

2
  • 3
    You should not typecast the pointers returned by malloc. void* are implicitly cast to the type of the pointer they're assigned to. Commented Feb 15, 2011 at 0:31
  • In C NULL should be ((void *)0) for correctness on platforms where sizeof(int) != sizeof(void *) (it can cause problems when used in varargs functions, which don't know to implicitly cast the 0 to a NULL pointer). Commented Feb 15, 2011 at 1:15

2 Answers 2

1

= doesn't copy a string in C, it only copies the pointer. From your description, it sounds like you're probably leaking a bunch of memory from the

command.argv[0] = "man";

lines you mention. The most noticeable problem is with this assignment in your second code block:

argTemp[ii] = command.argv[ii];

That assignment leaks the allocation you just made on the previous line (which, incidentally, probably should have a +1 in it). On the line after, you are trying to do a strcpy(), but after this assignment, you're just doing a strcpy() from one string to the same string, and not actually copying any values.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Carl, but I didn't have argTemp[ii] = command.argv[ii]; line before, and the argTemp still is null. So I added that line in front of strcpy() to make the 2 pointers the same..?
If the two pointers are the same, there's no reason to strcpy(). It seems like you have a few problems with understanding pointers/strings and how to work with them. Maybe you should show us some more code?
I edited the question, and hope it is more clear. As I see in the debugger, my command.argv[0] and command.argv[1] are "man" and "socket" respectively, with argv[2] = "\000" and onwards are all "\000". So now I want to make argTemp[] to be the same as command.argv, except there is no argv[3] and onwards.
1

I ran the following program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_LEN        80
#define MAX_ARGS        64
#define MAX_ARG_LEN     64
#define MAX_PATHS       64
#define MAX_PATH_LEN    96

#ifndef NULL
#define NULL            0
#endif

struct command_t {
    char *name;
    int argc;
    char *argv[MAX_ARGS];
};

int main() {
    size_t ii;  // Assumed

    /* Command initialisation, allocate type command */
    struct command_t command;
    command.name = (char *) malloc(LINE_LEN);
    command.argc = 0;
    for(ii=0; ii < MAX_ARGS; ii++) {  /* i < 64 */
        command.argv[ii] = (char *) malloc(MAX_ARG_LEN);
    }

    command.name = "man";
    command.argc = 2;
    command.argv[0] = "man";
    command.argv[1] = "socket";

    size_t argc = command.argc; // Assumed

    char *argTemp[argc+1];
    for(ii = 0; ii < argc+1; ii++){
        if(strlen(command.argv[ii])){
            argTemp[ii] = (char *)malloc(strlen(command.argv[ii]));
            strcpy(argTemp[ii], command.argv[ii]);
        } else {
            argTemp[ii] = (char*)NULL;
        }
    }

    printf("argTemp[0] == %p (%s)\n", argTemp[0], argTemp[0]);

    return 0;
}

I get the following output:

argTemp[0] == 0x159bd470 (man)

Based on what you wrote, this is what I expect, but this isn't what you're getting. Your problem seems to be elsewhere. Can you post a full code example replicating the problem?

I did notice a few bugs and issues in your code:

  1. You're iterating to argTemp[3], which is probably not what you want. Remove the +1 from the last for loop.
  2. You aren't initializing command.argv[ii] with an empty string. Simply writing command.argv[ii][0] = '\0'; will fix that.
  3. You're calling strlen twice. You should instead cache the result of strlen.
  4. You're allocating memory for your command strings, yet you end up just reassigning the command strings. This may be for the sake of example, but if you are reassigning, you have a memory leak (as you're not freeing the malloced memory).
  5. You are only allocating enough memory for the string's contents, and not the null terminator at the end of the string. strcpy will thus write '\0' at argTemp[ii][strlen(command.argv[ii])], which is undefined behaviour.

1 Comment

Thank you, you gave really good advice, I'm posting the entire code here while debugging more. Seems the problem is else where if not in the code I posted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.