Strange problem regarding C and pointers?
-
I'm trying to write an encryption program that generates a random substitution map for the letters of the alphabet.The function that generates this substitution map returns a pointer and even though i'm sure the substitution map is created succesfully(by printing it inside the function) i can't use it because there's a runtime error when it's trying to loop through this new random array.But i can print the single array elements without a problem. But it doesn't work if i use a for or while loop. Here's relevant part of the program. First the function that generates the substitution map : char* generateRandomSubMap1(char *ptr) { char *ptrSub; char a[26]; int i,j,k,flag; srand(time(NULL)); for(j=0;j<=25;j++) { do { flag = 0; i = rand() % 26; for(k=0;k<j;k++) if(a[k] == *(ptr + i)) flag = 1; }while(flag); a[j] = *(ptr+i); printf(" %c",a[j]); } printf("\n"); ptrSub = a; return ptrSub; } Now in main i write ptrSubA = generateRandomSubMap1(ptrA); ( where ptrA points to the array holding the letters of the alphabet in order) while(*ptrSubA != '\0'){ printf(" %c",*ptrSubA); ptrSubA++;} or for(j=0;j<=25;j++) printf(" %c", *(ptrSubA+j)); (j is an integer) and it doesn't work. This works though : printf(" %c ",*(ptrSubA +1)); But it's of no use without a loop obviously. Any help would be really appreciated and sorry for the ugly wall of text :)
-
Answer:
Although you return a pointer to the your map, your map is allocated on the stack. The memory is only allocated while generateRandomSubMap1 is being called, when it returns the next function that gets call will reuse the memory for its own purposes, overwritting it. You need to do one of two things. If you only need one of these maps, make the "a" array static, either by simply inserting the word "static" before its definition, or by moving the whole definition outside of generateRandomSubMap1. The alternative is to allocate memory from the heap. That would change the line: char a[26]; to char* a = (char*)malloc(26); Nothing else needs to change, although ideally callers to your function would eventually call free(ptr); on the object they were returned in order to give the memory back.
Morbo at Yahoo! Answers Visit the source
Other answers
Wamsie is extremely correct, but don't do: char* a = (char*)malloc(26); instead, use the "sizeof" operator. The above will work (as long as chars are one byte) but you may find yourself in trouble later. The preferred (or "paranoid") syntax is: char* a = (char*)malloc(26 * sizeof(char)); malloc returns a (void *) or what is called a void pointer, and the (char *) before it simply casts it, it doesn't give malloc any information on the size. That said, the width of chars will probably never change, but float and double can be interesting.
Related Q & A:
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- What is the difference between C# and C#.NET?Best solution by Stack Overflow
- How to use GUI in c language?Which is best GUI for c?Best solution by Quora
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
- Regarding the apparent obesity problem?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.