Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. char ** problem (ANSI C)

char ** problem (ANSI C)

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingperformancehelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kfaday
    wrote on last edited by
    #1

    I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory: i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok??????????? then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code": fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; } i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!

    D J T 3 Replies Last reply
    0
    • K kfaday

      I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory: i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok??????????? then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code": fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; } i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Are you getting (compiler, linker, runtime) errors somewhere? What are you seeing in the debugger?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      K 1 Reply Last reply
      0
      • D David Crow

        Are you getting (compiler, linker, runtime) errors somewhere? What are you seeing in the debugger?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        K Offline
        K Offline
        kfaday
        wrote on last edited by
        #3

        i got it thanks anyways! the problem was this line: *palres = (char*) malloc ((sizeof(char))*i); it works with: palres[j] = (char*) malloc ((sizeof(char))*i);

        1 Reply Last reply
        0
        • K kfaday

          I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory: i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok??????????? then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code": fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; } i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          // this is wrong in C or C++ (array is 1 short - will overwrite memory)
          cantpalres=i-1;
          palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
          // this is right (add toss the exta braces)
          cantpalres=i;
          palres = (char**) malloc (sizeof(char*)*cantpalres);

          // 1st and 2nd part (simpified)

          i=0;
          while (!feof(archpalres))
          {
          if( !fgets (temp,MAX_PAL,archpalres) )
          break;
          ++i; // i++ ok in C but a bad habit in C++
          }
          cantpalres=i;
          // allocate 2 dimensional array of string/charater pointers
          palres = (char**) malloc (sizeof(char*)*cantpalres);
          if( !palres ) // always check if allocation succeded
          return; // or return some error value
          fseek (archpalres,0,SEEK_SET);
          i=0;
          while (!feof(archpalres))
          {
          if( !fgets (temp,MAX_PAL,archpalres) )
          break;
          // replace '\n' with '\0'
          pNewline = strchr(temp,'\n');
          if( pNewline ) // there might not be a newline after last word
          *pNewLine = '\0';
          len = strlen(temp) + 1; // +1 for '\0'
          // allocate array of charaters
          palres[i] = (char*) malloc (sizeof(char)*len);
          if( palres[i] ) // always check if allocation succeded
          strcpy(palres[i],temp);
          ++i;
          }

          Normaly I would not have given a complete solution, but there was to much wrong too explane it all. INTP

          K 1 Reply Last reply
          0
          • K kfaday

            I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory: i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok??????????? then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code": fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; } i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!

            T Offline
            T Offline
            toothless boots
            wrote on last edited by
            #5

            Try (*palres) = (char*) malloc ((sizeof(char))*i); parles[j] will also work as you have found out. The problem is in the order of precedence. The brackets will solve this problem. Dave

            1 Reply Last reply
            0
            • J John R Shaw

              // this is wrong in C or C++ (array is 1 short - will overwrite memory)
              cantpalres=i-1;
              palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
              // this is right (add toss the exta braces)
              cantpalres=i;
              palres = (char**) malloc (sizeof(char*)*cantpalres);

              // 1st and 2nd part (simpified)

              i=0;
              while (!feof(archpalres))
              {
              if( !fgets (temp,MAX_PAL,archpalres) )
              break;
              ++i; // i++ ok in C but a bad habit in C++
              }
              cantpalres=i;
              // allocate 2 dimensional array of string/charater pointers
              palres = (char**) malloc (sizeof(char*)*cantpalres);
              if( !palres ) // always check if allocation succeded
              return; // or return some error value
              fseek (archpalres,0,SEEK_SET);
              i=0;
              while (!feof(archpalres))
              {
              if( !fgets (temp,MAX_PAL,archpalres) )
              break;
              // replace '\n' with '\0'
              pNewline = strchr(temp,'\n');
              if( pNewline ) // there might not be a newline after last word
              *pNewLine = '\0';
              len = strlen(temp) + 1; // +1 for '\0'
              // allocate array of charaters
              palres[i] = (char*) malloc (sizeof(char)*len);
              if( palres[i] ) // always check if allocation succeded
              strcpy(palres[i],temp);
              ++i;
              }

              Normaly I would not have given a complete solution, but there was to much wrong too explane it all. INTP

              K Offline
              K Offline
              kfaday
              wrote on last edited by
              #6

              thanks mr Shaw! this line you wrote were the answer. i didn't add 1, that's why it didn;t work len = strlen(temp) + 1; //

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups